isl_dim_get_tuple_id: complain if tuple has no id
[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_private.h>
22 #include <isl/obj.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
26 #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
362         if (tok->type == ISL_TOKEN_AFF) {
363                 res = isl_pw_aff_copy(tok->u.pwaff);
364                 isl_token_free(tok);
365         } else if (tok->type == ISL_TOKEN_IDENT) {
366                 int n = v->n;
367                 int pos = vars_pos(v, tok->u.s, -1);
368                 isl_aff *aff;
369
370                 if (pos < 0)
371                         goto error;
372                 if (pos >= n) {
373                         isl_stream_error(s, tok, "unknown identifier");
374                         goto error;
375                 }
376
377                 aff = isl_aff_zero(isl_local_space_from_dim(isl_dim_copy(dim)));
378                 if (!aff)
379                         goto error;
380                 isl_int_set_si(aff->v->el[2 + pos], 1);
381                 res = isl_pw_aff_from_aff(aff);
382                 isl_token_free(tok);
383         } else if (tok->type == ISL_TOKEN_VALUE) {
384                 if (isl_stream_eat_if_available(s, '*')) {
385                         res = accept_affine_factor(s, isl_dim_copy(dim), v);
386                         res = isl_pw_aff_scale(res, tok->u.v);
387                 } else {
388                         isl_local_space *ls;
389                         isl_aff *aff;
390                         ls = isl_local_space_from_dim(isl_dim_copy(dim));
391                         aff = isl_aff_zero(ls);
392                         aff = isl_aff_add_constant(aff, tok->u.v);
393                         res = isl_pw_aff_from_aff(aff);
394                 }
395                 isl_token_free(tok);
396         } else if (tok->type == '(') {
397                 isl_token_free(tok);
398                 tok = NULL;
399                 res = accept_affine(s, isl_dim_copy(dim), v);
400                 if (!res)
401                         goto error;
402                 if (isl_stream_eat(s, ')'))
403                         goto error;
404         } else if (tok->type == '[' ||
405                     tok->type == ISL_TOKEN_FLOORD ||
406                     tok->type == ISL_TOKEN_CEILD) {
407                 isl_stream_push_token(s, tok);
408                 tok = NULL;
409                 res = accept_div(s, isl_dim_copy(dim), v);
410         } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
411                 isl_stream_push_token(s, tok);
412                 tok = NULL;
413                 res = accept_minmax(s, isl_dim_copy(dim), v);
414         } else {
415                 isl_stream_error(s, tok, "expecting factor");
416                 goto error;
417         }
418         if (isl_stream_eat_if_available(s, '%') ||
419             isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
420                 isl_dim_free(dim);
421                 return affine_mod(s, v, res);
422         }
423         if (isl_stream_eat_if_available(s, '*')) {
424                 isl_int f;
425                 isl_int_init(f);
426                 isl_int_set_si(f, 1);
427                 if (accept_cst_factor(s, &f) < 0) {
428                         isl_int_clear(f);
429                         goto error2;
430                 }
431                 res = isl_pw_aff_scale(res, f);
432                 isl_int_clear(f);
433         }
434
435         isl_dim_free(dim);
436         return res;
437 error:
438         isl_token_free(tok);
439 error2:
440         isl_pw_aff_free(res);
441         isl_dim_free(dim);
442         return NULL;
443 }
444
445 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
446 {
447         isl_aff *aff;
448
449         aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(pwaff)));
450         aff = isl_aff_add_constant(aff, v);
451
452         return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
453 }
454
455 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
456         __isl_take isl_dim *dim, struct vars *v)
457 {
458         struct isl_token *tok = NULL;
459         isl_local_space *ls;
460         isl_pw_aff *res;
461         int sign = 1;
462
463         ls = isl_local_space_from_dim(isl_dim_copy(dim));
464         res = isl_pw_aff_from_aff(isl_aff_zero(ls));
465         if (!res)
466                 goto error;
467
468         for (;;) {
469                 tok = next_token(s);
470                 if (!tok) {
471                         isl_stream_error(s, NULL, "unexpected EOF");
472                         goto error;
473                 }
474                 if (tok->type == '-') {
475                         sign = -sign;
476                         isl_token_free(tok);
477                         continue;
478                 }
479                 if (tok->type == '(' || tok->type == '[' ||
480                     tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
481                     tok->type == ISL_TOKEN_FLOORD ||
482                     tok->type == ISL_TOKEN_CEILD ||
483                     tok->type == ISL_TOKEN_IDENT ||
484                     tok->type == ISL_TOKEN_AFF) {
485                         isl_pw_aff *term;
486                         isl_stream_push_token(s, tok);
487                         tok = NULL;
488                         term = accept_affine_factor(s, isl_dim_copy(dim), v);
489                         if (sign < 0)
490                                 res = isl_pw_aff_sub(res, term);
491                         else
492                                 res = isl_pw_aff_add(res, term);
493                         if (!res)
494                                 goto error;
495                         sign = 1;
496                 } else if (tok->type == ISL_TOKEN_VALUE) {
497                         if (sign < 0)
498                                 isl_int_neg(tok->u.v, tok->u.v);
499                         if (isl_stream_eat_if_available(s, '*') ||
500                             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
501                                 isl_pw_aff *term;
502                                 term = accept_affine_factor(s,
503                                                         isl_dim_copy(dim), v);
504                                 term = isl_pw_aff_scale(term, tok->u.v);
505                                 res = isl_pw_aff_add(res, term);
506                                 if (!res)
507                                         goto error;
508                         } else {
509                                 res = add_cst(res, tok->u.v);
510                         }
511                         sign = 1;
512                 } else {
513                         isl_stream_error(s, tok, "unexpected isl_token");
514                         isl_stream_push_token(s, tok);
515                         isl_pw_aff_free(res);
516                         isl_dim_free(dim);
517                         return NULL;
518                 }
519                 isl_token_free(tok);
520
521                 tok = next_token(s);
522                 if (tok && tok->type == '-') {
523                         sign = -sign;
524                         isl_token_free(tok);
525                 } else if (tok && tok->type == '+') {
526                         /* nothing */
527                         isl_token_free(tok);
528                 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
529                            isl_int_is_neg(tok->u.v)) {
530                         isl_stream_push_token(s, tok);
531                 } else {
532                         if (tok)
533                                 isl_stream_push_token(s, tok);
534                         break;
535                 }
536         }
537
538         isl_dim_free(dim);
539         return res;
540 error:
541         isl_dim_free(dim);
542         isl_token_free(tok);
543         isl_pw_aff_free(res);
544         return NULL;
545 }
546
547 static int is_comparator(struct isl_token *tok)
548 {
549         if (!tok)
550                 return 0;
551
552         switch (tok->type) {
553         case ISL_TOKEN_LT:
554         case ISL_TOKEN_GT:
555         case ISL_TOKEN_LE:
556         case ISL_TOKEN_GE:
557         case ISL_TOKEN_NE:
558         case '=':
559                 return 1;
560         default:
561                 return 0;
562         }
563 }
564
565 static struct isl_map *read_disjuncts(struct isl_stream *s,
566         struct vars *v, __isl_take isl_map *map);
567 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
568         __isl_take isl_dim *dim, struct vars *v);
569
570 /* Accept a ternary operator, given the first argument.
571  */
572 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
573         __isl_take isl_map *cond, struct vars *v)
574 {
575         isl_dim *dim;
576         isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL;
577
578         if (!cond)
579                 return NULL;
580
581         if (isl_stream_eat(s, '?'))
582                 goto error;
583
584         dim = isl_dim_wrap(isl_map_get_dim(cond));
585         pwaff1 = accept_extended_affine(s, dim, v);
586         if (!pwaff1)
587                 goto error;
588
589         if (isl_stream_eat(s, ':'))
590                 goto error;
591
592         pwaff2 = accept_extended_affine(s, isl_pw_aff_get_dim(pwaff1), v);
593         if (!pwaff1)
594                 goto error;
595
596         return isl_pw_aff_cond(isl_map_wrap(cond), pwaff1, pwaff2);
597 error:
598         isl_map_free(cond);
599         isl_pw_aff_free(pwaff1);
600         isl_pw_aff_free(pwaff2);
601         return NULL;
602 }
603
604 /* Accept an affine expression that may involve ternary operators.
605  * We first read an affine expression.
606  * If it is not followed by a comparison operator, we simply return it.
607  * Otherwise, we assume the affine epxression is part of the first
608  * argument of a ternary operator and try to parse that.
609  */
610 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
611         __isl_take isl_dim *dim, struct vars *v)
612 {
613         isl_map *cond;
614         isl_pw_aff *pwaff;
615         struct isl_token *tok;
616         int line = -1, col = -1;
617         int is_comp;
618
619         tok = isl_stream_next_token(s);
620         if (tok) {
621                 line = tok->line;
622                 col = tok->col;
623                 isl_stream_push_token(s, tok);
624         }
625
626         pwaff = accept_affine(s, dim, v);
627         if (!pwaff)
628                 return NULL;
629
630         tok = isl_stream_next_token(s);
631         if (!tok)
632                 return isl_pw_aff_free(pwaff);
633
634         is_comp = is_comparator(tok);
635         isl_stream_push_token(s, tok);
636         if (!is_comp)
637                 return pwaff;
638
639         tok = isl_token_new(s->ctx, line, col, 0);
640         if (!tok)
641                 return isl_pw_aff_free(pwaff);
642         tok->type = ISL_TOKEN_AFF;
643         tok->u.pwaff = pwaff;
644
645         cond = isl_map_universe(isl_dim_unwrap(isl_pw_aff_get_dim(pwaff)));
646
647         isl_stream_push_token(s, tok);
648
649         cond = read_disjuncts(s, v, cond);
650
651         return accept_ternary(s, cond, v);
652 }
653
654 static __isl_give isl_map *read_var_def(struct isl_stream *s,
655         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
656 {
657         isl_pw_aff *def;
658         int pos;
659         isl_map *def_map;
660
661         if (type == isl_dim_param)
662                 pos = isl_map_dim(map, isl_dim_param);
663         else {
664                 pos = isl_map_dim(map, isl_dim_in);
665                 if (type == isl_dim_out)
666                         pos += isl_map_dim(map, isl_dim_out);
667                 type = isl_dim_in;
668         }
669         --pos;
670
671         def = accept_extended_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
672         def_map = isl_map_from_pw_aff(def);
673         def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
674         def_map = isl_set_unwrap(isl_map_domain(def_map));
675
676         map = isl_map_intersect(map, def_map);
677
678         return map;
679 }
680
681 static __isl_give isl_map *read_var_list(struct isl_stream *s,
682         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
683 {
684         int i = 0;
685         struct isl_token *tok;
686
687         if (isl_stream_next_token_is(s, ']'))
688                 return map;
689
690         while ((tok = next_token(s)) != NULL) {
691                 int new_name = 0;
692
693                 if (tok->type == ISL_TOKEN_IDENT) {
694                         int n = v->n;
695                         int p = vars_pos(v, tok->u.s, -1);
696                         if (p < 0)
697                                 goto error;
698                         new_name = p >= n;
699                 }
700
701                 if (new_name) {
702                         map = isl_map_add_dims(map, type, 1);
703                         map = set_name(map, type, i, v->v->name);
704                         isl_token_free(tok);
705                         if (isl_stream_eat_if_available(s, '='))
706                                 map = read_var_def(s, map, type, v);
707                 } else {
708                         if (type == isl_dim_param) {
709                                 isl_stream_error(s, tok,
710                                                 "expecting unique identifier");
711                                 goto error;
712                         }
713                         isl_stream_push_token(s, tok);
714                         tok = NULL;
715                         if (vars_add_anon(v) < 0)
716                                 goto error;
717                         map = isl_map_add_dims(map, type, 1);
718                         map = read_var_def(s, map, type, v);
719                 }
720
721                 tok = isl_stream_next_token(s);
722                 if (tok && tok->type == ']' &&
723                     isl_stream_next_token_is(s, '[')) {
724                         isl_token_free(tok);
725                         tok = isl_stream_next_token(s);
726                 } else if (!tok || tok->type != ',')
727                         break;
728
729                 isl_token_free(tok);
730                 i++;
731         }
732         if (tok)
733                 isl_stream_push_token(s, tok);
734
735         return map;
736 error:
737         isl_token_free(tok);
738         isl_map_free(map);
739         return NULL;
740 }
741
742 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
743         __isl_take isl_dim *dim, struct vars *v)
744 {
745         isl_pw_aff *pwaff;
746         isl_pw_aff_list *list;
747         struct isl_token *tok = NULL;
748
749         pwaff = accept_affine(s, isl_dim_copy(dim), v);
750         list = isl_pw_aff_list_from_pw_aff(pwaff);
751         if (!list)
752                 goto error;
753
754         for (;;) {
755                 tok = isl_stream_next_token(s);
756                 if (!tok) {
757                         isl_stream_error(s, NULL, "unexpected EOF");
758                         goto error;
759                 }
760                 if (tok->type != ',') {
761                         isl_stream_push_token(s, tok);
762                         break;
763                 }
764                 isl_token_free(tok);
765
766                 pwaff = accept_affine(s, isl_dim_copy(dim), v);
767                 list = isl_pw_aff_list_concat(list,
768                                 isl_pw_aff_list_from_pw_aff(pwaff));
769                 if (!list)
770                         return NULL;
771         }
772
773         isl_dim_free(dim);
774         return list;
775 error:
776         isl_dim_free(dim);
777         isl_pw_aff_list_free(list);
778         return NULL;
779 }
780
781 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
782         struct vars *v, __isl_take isl_map *map)
783 {
784         struct isl_token *tok;
785
786         while ((tok = isl_stream_next_token(s)) != NULL) {
787                 int p;
788                 int n = v->n;
789
790                 if (tok->type != ISL_TOKEN_IDENT)
791                         break;
792
793                 p = vars_pos(v, tok->u.s, -1);
794                 if (p < 0)
795                         goto error;
796                 if (p < n) {
797                         isl_stream_error(s, tok, "expecting unique identifier");
798                         goto error;
799                 }
800
801                 map = isl_map_add_dims(map, isl_dim_out, 1);
802
803                 isl_token_free(tok);
804                 tok = isl_stream_next_token(s);
805                 if (tok && tok->type == '=') {
806                         isl_token_free(tok);
807                         map = read_var_def(s, map, isl_dim_out, v);
808                         tok = isl_stream_next_token(s);
809                 }
810
811                 if (!tok || tok->type != ',')
812                         break;
813
814                 isl_token_free(tok);
815         }
816         if (tok)
817                 isl_stream_push_token(s, tok);
818
819         return map;
820 error:
821         isl_token_free(tok);
822         isl_map_free(map);
823         return NULL;
824 }
825
826 static int next_is_tuple(struct isl_stream *s)
827 {
828         struct isl_token *tok;
829         int is_tuple;
830
831         tok = isl_stream_next_token(s);
832         if (!tok)
833                 return 0;
834         if (tok->type == '[') {
835                 isl_stream_push_token(s, tok);
836                 return 1;
837         }
838         if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
839                 isl_stream_push_token(s, tok);
840                 return 0;
841         }
842
843         is_tuple = isl_stream_next_token_is(s, '[');
844
845         isl_stream_push_token(s, tok);
846
847         return is_tuple;
848 }
849
850 static __isl_give isl_map *read_tuple(struct isl_stream *s,
851         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
852
853 static __isl_give isl_map *read_nested_tuple(struct isl_stream *s,
854         __isl_take isl_map *map, struct vars *v)
855 {
856         map = read_tuple(s, map, isl_dim_in, v);
857         if (isl_stream_eat(s, ISL_TOKEN_TO))
858                 goto error;
859         map = read_tuple(s, map, isl_dim_out, v);
860         map = isl_map_from_range(isl_map_wrap(map));
861         return map;
862 error:
863         isl_map_free(map);
864         return NULL;
865 }
866
867 static __isl_give isl_map *read_tuple(struct isl_stream *s,
868         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
869 {
870         struct isl_token *tok;
871         char *name = NULL;
872
873         tok = isl_stream_next_token(s);
874         if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
875                 name = strdup(tok->u.s);
876                 if (!name)
877                         goto error;
878                 isl_token_free(tok);
879                 tok = isl_stream_next_token(s);
880         }
881         if (!tok || tok->type != '[') {
882                 isl_stream_error(s, tok, "expecting '['");
883                 goto error;
884         }
885         isl_token_free(tok);
886         if (type != isl_dim_param && next_is_tuple(s)) {
887                 isl_dim *dim = isl_map_get_dim(map);
888                 int nparam = isl_dim_size(dim, isl_dim_param);
889                 int n_in = isl_dim_size(dim, isl_dim_in);
890                 isl_map *nested;
891                 if (type == isl_dim_out)
892                         dim = isl_dim_move(dim, isl_dim_param, nparam,
893                                                 isl_dim_in, 0, n_in);
894                 nested = isl_map_universe(dim);
895                 nested = read_nested_tuple(s, nested, v);
896                 if (type == isl_dim_in) {
897                         nested = isl_map_reverse(nested);
898                         map = isl_map_intersect(nested, map);
899                 } else {
900                         isl_set *set;
901                         dim = isl_dim_range(isl_map_get_dim(nested));
902                         dim = isl_dim_drop(dim, isl_dim_param, nparam, n_in);
903                         dim = isl_dim_join(isl_map_get_dim(map), dim);
904                         set = isl_map_domain(map);
905                         nested = isl_map_reset_dim(nested, dim);
906                         map = isl_map_intersect_domain(nested, set);
907                 }
908         } else
909                 map = read_var_list(s, map, type, v);
910         tok = isl_stream_next_token(s);
911         if (!tok || tok->type != ']') {
912                 isl_stream_error(s, tok, "expecting ']'");
913                 goto error;
914         }
915         isl_token_free(tok);
916
917         if (name) {
918                 map = isl_map_set_tuple_name(map, type, name);
919                 free(name);
920         }
921
922         return map;
923 error:
924         if (tok)
925                 isl_token_free(tok);
926         isl_map_free(map);
927         return NULL;
928 }
929
930 static __isl_give isl_set *construct_constraints(
931         __isl_take isl_set *set, enum isl_token_type type,
932         __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
933 {
934         isl_set *cond;
935
936         if (type == ISL_TOKEN_LE)
937                 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
938                                               isl_pw_aff_list_copy(right));
939         else if (type == ISL_TOKEN_GE)
940                 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
941                                               isl_pw_aff_list_copy(right));
942         else if (type == ISL_TOKEN_LT)
943                 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
944                                               isl_pw_aff_list_copy(right));
945         else if (type == ISL_TOKEN_GT)
946                 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
947                                               isl_pw_aff_list_copy(right));
948         else if (type == ISL_TOKEN_NE)
949                 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
950                                               isl_pw_aff_list_copy(right));
951         else
952                 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
953                                               isl_pw_aff_list_copy(right));
954
955         return isl_set_intersect(set, cond);
956 }
957
958 static __isl_give isl_map *add_constraint(struct isl_stream *s,
959         struct vars *v, __isl_take isl_map *map)
960 {
961         struct isl_token *tok = NULL;
962         isl_pw_aff_list *list1 = NULL, *list2 = NULL;
963         isl_set *set;
964
965         set = isl_map_wrap(map);
966         list1 = accept_affine_list(s, isl_set_get_dim(set), v);
967         if (!list1)
968                 goto error;
969         tok = isl_stream_next_token(s);
970         if (!is_comparator(tok)) {
971                 isl_stream_error(s, tok, "missing operator");
972                 if (tok)
973                         isl_stream_push_token(s, tok);
974                 tok = NULL;
975                 goto error;
976         }
977         for (;;) {
978                 list2 = accept_affine_list(s, isl_set_get_dim(set), v);
979                 if (!list2)
980                         goto error;
981
982                 set = construct_constraints(set, tok->type, list1, list2);
983                 isl_token_free(tok);
984                 isl_pw_aff_list_free(list1);
985                 list1 = list2;
986
987                 tok = isl_stream_next_token(s);
988                 if (!is_comparator(tok)) {
989                         if (tok)
990                                 isl_stream_push_token(s, tok);
991                         break;
992                 }
993         }
994         isl_pw_aff_list_free(list1);
995
996         return isl_set_unwrap(set);
997 error:
998         if (tok)
999                 isl_token_free(tok);
1000         isl_pw_aff_list_free(list1);
1001         isl_pw_aff_list_free(list2);
1002         isl_set_free(set);
1003         return NULL;
1004 }
1005
1006 static __isl_give isl_map *read_exists(struct isl_stream *s,
1007         struct vars *v, __isl_take isl_map *map)
1008 {
1009         int n = v->n;
1010         int seen_paren = isl_stream_eat_if_available(s, '(');
1011
1012         map = isl_map_from_domain(isl_map_wrap(map));
1013         map = read_defined_var_list(s, v, map);
1014
1015         if (isl_stream_eat(s, ':'))
1016                 goto error;
1017
1018         map = read_disjuncts(s, v, map);
1019         map = isl_set_unwrap(isl_map_domain(map));
1020
1021         vars_drop(v, v->n - n);
1022         if (seen_paren && isl_stream_eat(s, ')'))
1023                 goto error;
1024
1025         return map;
1026 error:
1027         isl_map_free(map);
1028         return NULL;
1029 }
1030
1031 /* Parse an expression between parentheses and push the result
1032  * back on the stream.
1033  *
1034  * The parsed expression may be either an affine expression
1035  * or a condition.  The first type is pushed onto the stream
1036  * as an isl_pw_aff, while the second is pushed as an isl_map.
1037  *
1038  * If the initial token indicates the start of a condition,
1039  * we parse it as such.
1040  * Otherwise, we first parse an affine expression and push
1041  * that onto the stream.  If the affine expression covers the
1042  * entire expression between parentheses, we return.
1043  * Otherwise, we assume that the affine expression is the
1044  * start of a condition and continue parsing.
1045  */
1046 static int resolve_paren_expr(struct isl_stream *s,
1047         struct vars *v, __isl_take isl_map *map)
1048 {
1049         struct isl_token *tok, *tok2;
1050         int line, col;
1051         isl_pw_aff *pwaff;
1052
1053         tok = isl_stream_next_token(s);
1054         if (!tok || tok->type != '(')
1055                 goto error;
1056
1057         if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1058             isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1059             isl_stream_next_token_is(s, ISL_TOKEN_FALSE)) {
1060                 map = read_disjuncts(s, v, map);
1061                 if (isl_stream_eat(s, ')'))
1062                         goto error;
1063                 tok->type = ISL_TOKEN_MAP;
1064                 tok->u.map = map;
1065                 isl_stream_push_token(s, tok);
1066                 return 0;
1067         }
1068
1069         tok2 = isl_stream_next_token(s);
1070         if (!tok2)
1071                 goto error;
1072         line = tok2->line;
1073         col = tok2->col;
1074         isl_stream_push_token(s, tok2);
1075
1076         pwaff = accept_affine(s, isl_dim_wrap(isl_map_get_dim(map)), v);
1077         if (!pwaff)
1078                 goto error;
1079
1080         tok2 = isl_token_new(s->ctx, line, col, 0);
1081         if (!tok2)
1082                 goto error2;
1083         tok2->type = ISL_TOKEN_AFF;
1084         tok2->u.pwaff = pwaff;
1085
1086         if (isl_stream_eat_if_available(s, ')')) {
1087                 isl_stream_push_token(s, tok2);
1088                 isl_token_free(tok);
1089                 isl_map_free(map);
1090                 return 0;
1091         }
1092
1093         isl_stream_push_token(s, tok2);
1094
1095         map = read_disjuncts(s, v, map);
1096         if (isl_stream_eat(s, ')'))
1097                 goto error;
1098
1099         tok->type = ISL_TOKEN_MAP;
1100         tok->u.map = map;
1101         isl_stream_push_token(s, tok);
1102
1103         return 0;
1104 error2:
1105         isl_pw_aff_free(pwaff);
1106 error:
1107         isl_token_free(tok);
1108         isl_map_free(map);
1109         return -1;
1110 }
1111
1112 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1113         struct vars *v, __isl_take isl_map *map)
1114 {
1115         if (isl_stream_next_token_is(s, '('))
1116                 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1117                         goto error;
1118
1119         if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1120                 struct isl_token *tok;
1121                 tok = isl_stream_next_token(s);
1122                 if (!tok)
1123                         goto error;
1124                 isl_map_free(map);
1125                 map = isl_map_copy(tok->u.map);
1126                 isl_token_free(tok);
1127                 return map;
1128         }
1129
1130         if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1131                 return read_exists(s, v, map);
1132
1133         if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1134                 return map;
1135
1136         if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1137                 isl_dim *dim = isl_map_get_dim(map);
1138                 isl_map_free(map);
1139                 return isl_map_empty(dim);
1140         }
1141                 
1142         return add_constraint(s, v, map);
1143 error:
1144         isl_map_free(map);
1145         return NULL;
1146 }
1147
1148 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1149         struct vars *v, __isl_take isl_map *map)
1150 {
1151         isl_map *res;
1152         int negate;
1153
1154         negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1155         res = read_conjunct(s, v, isl_map_copy(map));
1156         if (negate)
1157                 res = isl_map_subtract(isl_map_copy(map), res);
1158
1159         while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1160                 isl_map *res_i;
1161
1162                 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1163                 res_i = read_conjunct(s, v, isl_map_copy(map));
1164                 if (negate)
1165                         res = isl_map_subtract(res, res_i);
1166                 else
1167                         res = isl_map_intersect(res, res_i);
1168         }
1169
1170         isl_map_free(map);
1171         return res;
1172 }
1173
1174 static struct isl_map *read_disjuncts(struct isl_stream *s,
1175         struct vars *v, __isl_take isl_map *map)
1176 {
1177         isl_map *res;
1178
1179         if (isl_stream_next_token_is(s, '}')) {
1180                 isl_dim *dim = isl_map_get_dim(map);
1181                 isl_map_free(map);
1182                 return isl_map_universe(dim);
1183         }
1184
1185         res = read_conjuncts(s, v, isl_map_copy(map));
1186         while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1187                 isl_map *res_i;
1188
1189                 res_i = read_conjuncts(s, v, isl_map_copy(map));
1190                 res = isl_map_union(res, res_i);
1191         }
1192
1193         isl_map_free(map);
1194         return res;
1195 }
1196
1197 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1198 {
1199         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1200                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1201                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1202         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1203
1204         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1205                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1206         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1207
1208         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1209                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1210                            isl_basic_map_dim(bmap, isl_dim_in) +
1211                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1212         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1213
1214         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1215                 return 1 + pos;
1216
1217         return 0;
1218 }
1219
1220 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1221         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1222 {
1223         int j;
1224         struct isl_token *tok;
1225         int type;
1226         int k;
1227         isl_int *c;
1228         unsigned nparam;
1229         unsigned dim;
1230
1231         if (!bmap)
1232                 return NULL;
1233
1234         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1235         dim = isl_basic_map_dim(bmap, isl_dim_out);
1236
1237         tok = isl_stream_next_token(s);
1238         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1239                 isl_stream_error(s, tok, "expecting coefficient");
1240                 if (tok)
1241                         isl_stream_push_token(s, tok);
1242                 goto error;
1243         }
1244         if (!tok->on_new_line) {
1245                 isl_stream_error(s, tok, "coefficient should appear on new line");
1246                 isl_stream_push_token(s, tok);
1247                 goto error;
1248         }
1249
1250         type = isl_int_get_si(tok->u.v);
1251         isl_token_free(tok);
1252
1253         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1254         if (type == 0) {
1255                 k = isl_basic_map_alloc_equality(bmap);
1256                 c = bmap->eq[k];
1257         } else {
1258                 k = isl_basic_map_alloc_inequality(bmap);
1259                 c = bmap->ineq[k];
1260         }
1261         if (k < 0)
1262                 goto error;
1263
1264         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1265                 int pos;
1266                 tok = isl_stream_next_token(s);
1267                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1268                         isl_stream_error(s, tok, "expecting coefficient");
1269                         if (tok)
1270                                 isl_stream_push_token(s, tok);
1271                         goto error;
1272                 }
1273                 if (tok->on_new_line) {
1274                         isl_stream_error(s, tok,
1275                                 "coefficient should not appear on new line");
1276                         isl_stream_push_token(s, tok);
1277                         goto error;
1278                 }
1279                 pos = polylib_pos_to_isl_pos(bmap, j);
1280                 isl_int_set(c[pos], tok->u.v);
1281                 isl_token_free(tok);
1282         }
1283
1284         return bmap;
1285 error:
1286         isl_basic_map_free(bmap);
1287         return NULL;
1288 }
1289
1290 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1291         int nparam)
1292 {
1293         int i;
1294         struct isl_token *tok;
1295         struct isl_token *tok2;
1296         int n_row, n_col;
1297         int on_new_line;
1298         unsigned in = 0, out, local = 0;
1299         struct isl_basic_map *bmap = NULL;
1300
1301         if (nparam < 0)
1302                 nparam = 0;
1303
1304         tok = isl_stream_next_token(s);
1305         if (!tok) {
1306                 isl_stream_error(s, NULL, "unexpected EOF");
1307                 return NULL;
1308         }
1309         tok2 = isl_stream_next_token(s);
1310         if (!tok2) {
1311                 isl_token_free(tok);
1312                 isl_stream_error(s, NULL, "unexpected EOF");
1313                 return NULL;
1314         }
1315         if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1316                 isl_stream_push_token(s, tok2);
1317                 isl_stream_push_token(s, tok);
1318                 isl_stream_error(s, NULL,
1319                                  "expecting constraint matrix dimensions");
1320                 return NULL;
1321         }
1322         n_row = isl_int_get_si(tok->u.v);
1323         n_col = isl_int_get_si(tok2->u.v);
1324         on_new_line = tok2->on_new_line;
1325         isl_token_free(tok2);
1326         isl_token_free(tok);
1327         isl_assert(s->ctx, !on_new_line, return NULL);
1328         isl_assert(s->ctx, n_row >= 0, return NULL);
1329         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1330         tok = isl_stream_next_token_on_same_line(s);
1331         if (tok) {
1332                 if (tok->type != ISL_TOKEN_VALUE) {
1333                         isl_stream_error(s, tok,
1334                                     "expecting number of output dimensions");
1335                         isl_stream_push_token(s, tok);
1336                         goto error;
1337                 }
1338                 out = isl_int_get_si(tok->u.v);
1339                 isl_token_free(tok);
1340
1341                 tok = isl_stream_next_token_on_same_line(s);
1342                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1343                         isl_stream_error(s, tok,
1344                                     "expecting number of input dimensions");
1345                         if (tok)
1346                                 isl_stream_push_token(s, tok);
1347                         goto error;
1348                 }
1349                 in = isl_int_get_si(tok->u.v);
1350                 isl_token_free(tok);
1351
1352                 tok = isl_stream_next_token_on_same_line(s);
1353                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1354                         isl_stream_error(s, tok,
1355                                     "expecting number of existentials");
1356                         if (tok)
1357                                 isl_stream_push_token(s, tok);
1358                         goto error;
1359                 }
1360                 local = isl_int_get_si(tok->u.v);
1361                 isl_token_free(tok);
1362
1363                 tok = isl_stream_next_token_on_same_line(s);
1364                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1365                         isl_stream_error(s, tok,
1366                                     "expecting number of parameters");
1367                         if (tok)
1368                                 isl_stream_push_token(s, tok);
1369                         goto error;
1370                 }
1371                 nparam = isl_int_get_si(tok->u.v);
1372                 isl_token_free(tok);
1373                 if (n_col != 1 + out + in + local + nparam + 1) {
1374                         isl_stream_error(s, NULL,
1375                                     "dimensions don't match");
1376                         goto error;
1377                 }
1378         } else
1379                 out = n_col - 2 - nparam;
1380         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1381         if (!bmap)
1382                 return NULL;
1383
1384         for (i = 0; i < local; ++i) {
1385                 int k = isl_basic_map_alloc_div(bmap);
1386                 if (k < 0)
1387                         goto error;
1388                 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1389         }
1390
1391         for (i = 0; i < n_row; ++i)
1392                 bmap = basic_map_read_polylib_constraint(s, bmap);
1393
1394         tok = isl_stream_next_token_on_same_line(s);
1395         if (tok) {
1396                 isl_stream_error(s, tok, "unexpected extra token on line");
1397                 isl_stream_push_token(s, tok);
1398                 goto error;
1399         }
1400
1401         bmap = isl_basic_map_simplify(bmap);
1402         bmap = isl_basic_map_finalize(bmap);
1403         return bmap;
1404 error:
1405         isl_basic_map_free(bmap);
1406         return NULL;
1407 }
1408
1409 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1410 {
1411         struct isl_token *tok;
1412         struct isl_token *tok2;
1413         int i, n;
1414         struct isl_map *map;
1415
1416         tok = isl_stream_next_token(s);
1417         if (!tok) {
1418                 isl_stream_error(s, NULL, "unexpected EOF");
1419                 return NULL;
1420         }
1421         tok2 = isl_stream_next_token_on_same_line(s);
1422         if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1423                 isl_stream_push_token(s, tok2);
1424                 isl_stream_push_token(s, tok);
1425                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1426         }
1427         if (tok2) {
1428                 isl_stream_error(s, tok2, "unexpected token");
1429                 isl_stream_push_token(s, tok2);
1430                 isl_stream_push_token(s, tok);
1431                 return NULL;
1432         }
1433         n = isl_int_get_si(tok->u.v);
1434         isl_token_free(tok);
1435
1436         isl_assert(s->ctx, n >= 1, return NULL);
1437
1438         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1439
1440         for (i = 1; map && i < n; ++i)
1441                 map = isl_map_union(map,
1442                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1443
1444         return map;
1445 }
1446
1447 static int optional_power(struct isl_stream *s)
1448 {
1449         int pow;
1450         struct isl_token *tok;
1451
1452         tok = isl_stream_next_token(s);
1453         if (!tok)
1454                 return 1;
1455         if (tok->type != '^') {
1456                 isl_stream_push_token(s, tok);
1457                 return 1;
1458         }
1459         isl_token_free(tok);
1460         tok = isl_stream_next_token(s);
1461         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1462                 isl_stream_error(s, tok, "expecting exponent");
1463                 if (tok)
1464                         isl_stream_push_token(s, tok);
1465                 return 1;
1466         }
1467         pow = isl_int_get_si(tok->u.v);
1468         isl_token_free(tok);
1469         return pow;
1470 }
1471
1472 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1473         __isl_keep isl_map *map, struct vars *v);
1474
1475 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1476         __isl_keep isl_map *map, struct vars *v)
1477 {
1478         isl_pw_qpolynomial *pwqp;
1479         struct isl_token *tok;
1480
1481         tok = next_token(s);
1482         if (!tok) {
1483                 isl_stream_error(s, NULL, "unexpected EOF");
1484                 return NULL;
1485         }
1486         if (tok->type == '(') {
1487                 int pow;
1488
1489                 isl_token_free(tok);
1490                 pwqp = read_term(s, map, v);
1491                 if (!pwqp)
1492                         return NULL;
1493                 if (isl_stream_eat(s, ')'))
1494                         goto error;
1495                 pow = optional_power(s);
1496                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1497         } else if (tok->type == ISL_TOKEN_VALUE) {
1498                 struct isl_token *tok2;
1499                 tok2 = isl_stream_next_token(s);
1500                 isl_qpolynomial *qp;
1501                 if (tok2 && tok2->type == '/') {
1502                         isl_token_free(tok2);
1503                         tok2 = next_token(s);
1504                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1505                                 isl_stream_error(s, tok2, "expected denominator");
1506                                 isl_token_free(tok);
1507                                 isl_token_free(tok2);
1508                                 return NULL;
1509                         }
1510                         qp = isl_qpolynomial_rat_cst(isl_map_get_dim(map),
1511                                                     tok->u.v, tok2->u.v);
1512                         isl_token_free(tok2);
1513                 } else {
1514                         isl_stream_push_token(s, tok2);
1515                         qp = isl_qpolynomial_cst(isl_map_get_dim(map),
1516                                                 tok->u.v);
1517                 }
1518                 isl_token_free(tok);
1519                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1520         } else if (tok->type == ISL_TOKEN_INFTY) {
1521                 isl_qpolynomial *qp;
1522                 isl_token_free(tok);
1523                 qp = isl_qpolynomial_infty(isl_map_get_dim(map));
1524                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1525         } else if (tok->type == ISL_TOKEN_NAN) {
1526                 isl_qpolynomial *qp;
1527                 isl_token_free(tok);
1528                 qp = isl_qpolynomial_nan(isl_map_get_dim(map));
1529                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1530         } else if (tok->type == ISL_TOKEN_IDENT) {
1531                 int n = v->n;
1532                 int pos = vars_pos(v, tok->u.s, -1);
1533                 int pow;
1534                 isl_qpolynomial *qp;
1535                 if (pos < 0) {
1536                         isl_token_free(tok);
1537                         return NULL;
1538                 }
1539                 if (pos >= n) {
1540                         vars_drop(v, v->n - n);
1541                         isl_stream_error(s, tok, "unknown identifier");
1542                         isl_token_free(tok);
1543                         return NULL;
1544                 }
1545                 isl_token_free(tok);
1546                 pow = optional_power(s);
1547                 qp = isl_qpolynomial_var_pow(isl_map_get_dim(map), pos, pow);
1548                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1549         } else if (tok->type == '[') {
1550                 isl_pw_aff *pwaff;
1551                 int pow;
1552
1553                 isl_stream_push_token(s, tok);
1554                 pwaff = accept_affine(s, isl_map_get_dim(map), v);
1555                 pow = optional_power(s);
1556                 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1557                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1558         } else if (tok->type == '-') {
1559                 isl_token_free(tok);
1560                 pwqp = read_factor(s, map, v);
1561                 pwqp = isl_pw_qpolynomial_neg(pwqp);
1562         } else {
1563                 isl_stream_error(s, tok, "unexpected isl_token");
1564                 isl_stream_push_token(s, tok);
1565                 return NULL;
1566         }
1567
1568         if (isl_stream_eat_if_available(s, '*') ||
1569             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1570                 isl_pw_qpolynomial *pwqp2;
1571
1572                 pwqp2 = read_factor(s, map, v);
1573                 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1574         }
1575
1576         return pwqp;
1577 error:
1578         isl_pw_qpolynomial_free(pwqp);
1579         return NULL;
1580 }
1581
1582 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1583         __isl_keep isl_map *map, struct vars *v)
1584 {
1585         struct isl_token *tok;
1586         isl_pw_qpolynomial *pwqp;
1587
1588         pwqp = read_factor(s, map, v);
1589
1590         for (;;) {
1591                 tok = next_token(s);
1592                 if (!tok)
1593                         return pwqp;
1594
1595                 if (tok->type == '+') {
1596                         isl_pw_qpolynomial *pwqp2;
1597
1598                         isl_token_free(tok);
1599                         pwqp2 = read_factor(s, map, v);
1600                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1601                 } else if (tok->type == '-') {
1602                         isl_pw_qpolynomial *pwqp2;
1603
1604                         isl_token_free(tok);
1605                         pwqp2 = read_factor(s, map, v);
1606                         pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1607                 } else if (tok->type == ISL_TOKEN_VALUE &&
1608                             isl_int_is_neg(tok->u.v)) {
1609                         isl_pw_qpolynomial *pwqp2;
1610
1611                         isl_stream_push_token(s, tok);
1612                         pwqp2 = read_factor(s, map, v);
1613                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1614                 } else {
1615                         isl_stream_push_token(s, tok);
1616                         break;
1617                 }
1618         }
1619
1620         return pwqp;
1621 }
1622
1623 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1624         __isl_take isl_map *map, struct vars *v)
1625 {
1626         struct isl_token *tok;
1627
1628         tok = isl_stream_next_token(s);
1629         if (!tok) {
1630                 isl_stream_error(s, NULL, "unexpected EOF");
1631                 goto error;
1632         }
1633         if (tok->type == ':' ||
1634             (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1635                 isl_token_free(tok);
1636                 map = read_disjuncts(s, v, map);
1637         } else
1638                 isl_stream_push_token(s, tok);
1639
1640         return map;
1641 error:
1642         isl_map_free(map);
1643         return NULL;
1644 }
1645
1646 static struct isl_obj obj_read_poly(struct isl_stream *s,
1647         __isl_take isl_map *map, struct vars *v, int n)
1648 {
1649         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1650         isl_pw_qpolynomial *pwqp;
1651         struct isl_set *set;
1652
1653         pwqp = read_term(s, map, v);
1654         map = read_optional_disjuncts(s, map, v);
1655         set = isl_map_range(map);
1656
1657         pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1658
1659         vars_drop(v, v->n - n);
1660
1661         obj.v = pwqp;
1662         return obj;
1663 }
1664
1665 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1666         __isl_take isl_map *map, struct vars *v, int n)
1667 {
1668         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1669         isl_pw_qpolynomial *pwqp;
1670         isl_pw_qpolynomial_fold *pwf = NULL;
1671         isl_set *set;
1672
1673         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1674                 return obj_read_poly(s, map, v, n);
1675
1676         if (isl_stream_eat(s, '('))
1677                 goto error;
1678
1679         pwqp = read_term(s, map, v);
1680         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1681
1682         while (isl_stream_eat_if_available(s, ',')) {
1683                 isl_pw_qpolynomial_fold *pwf_i;
1684                 pwqp = read_term(s, map, v);
1685                 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1686                                                                         pwqp);
1687                 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1688         }
1689
1690         if (isl_stream_eat(s, ')'))
1691                 goto error;
1692
1693         map = read_optional_disjuncts(s, map, v);
1694         set = isl_map_range(map);
1695         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1696
1697         vars_drop(v, v->n - n);
1698
1699         obj.v = pwf;
1700         return obj;
1701 error:
1702         isl_map_free(map);
1703         isl_pw_qpolynomial_fold_free(pwf);
1704         obj.type = isl_obj_none;
1705         return obj;
1706 }
1707
1708 static int is_rational(struct isl_stream *s)
1709 {
1710         struct isl_token *tok;
1711
1712         tok = isl_stream_next_token(s);
1713         if (!tok)
1714                 return 0;
1715         if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1716                 isl_token_free(tok);
1717                 isl_stream_eat(s, ':');
1718                 return 1;
1719         }
1720
1721         isl_stream_push_token(s, tok);
1722
1723         return 0;
1724 }
1725
1726 static struct isl_obj obj_read_body(struct isl_stream *s,
1727         __isl_take isl_map *map, struct vars *v)
1728 {
1729         struct isl_token *tok;
1730         struct isl_obj obj = { isl_obj_set, NULL };
1731         int n = v->n;
1732
1733         if (is_rational(s))
1734                 map = isl_map_set_rational(map);
1735
1736         if (!next_is_tuple(s))
1737                 return obj_read_poly_or_fold(s, map, v, n);
1738
1739         map = read_tuple(s, map, isl_dim_in, v);
1740         if (!map)
1741                 goto error;
1742         tok = isl_stream_next_token(s);
1743         if (tok && tok->type == ISL_TOKEN_TO) {
1744                 obj.type = isl_obj_map;
1745                 isl_token_free(tok);
1746                 if (!next_is_tuple(s)) {
1747                         map = isl_map_reverse(map);
1748                         return obj_read_poly_or_fold(s, map, v, n);
1749                 }
1750                 map = read_tuple(s, map, isl_dim_out, v);
1751                 if (!map)
1752                         goto error;
1753         } else {
1754                 map = isl_map_reverse(map);
1755                 if (tok)
1756                         isl_stream_push_token(s, tok);
1757         }
1758
1759         map = read_optional_disjuncts(s, map, v);
1760
1761         vars_drop(v, v->n - n);
1762
1763         obj.v = map;
1764         return obj;
1765 error:
1766         isl_map_free(map);
1767         obj.type = isl_obj_none;
1768         return obj;
1769 }
1770
1771 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1772 {
1773         if (obj.type == isl_obj_map) {
1774                 obj.v = isl_union_map_from_map(obj.v);
1775                 obj.type = isl_obj_union_map;
1776         } else if (obj.type == isl_obj_set) {
1777                 obj.v = isl_union_set_from_set(obj.v);
1778                 obj.type = isl_obj_union_set;
1779         } else if (obj.type == isl_obj_pw_qpolynomial) {
1780                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1781                 obj.type = isl_obj_union_pw_qpolynomial;
1782         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1783                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1784                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1785         } else
1786                 isl_assert(ctx, 0, goto error);
1787         return obj;
1788 error:
1789         obj.type->free(obj.v);
1790         obj.type = isl_obj_none;
1791         return obj;
1792 }
1793
1794 static struct isl_obj obj_add(struct isl_ctx *ctx,
1795         struct isl_obj obj1, struct isl_obj obj2)
1796 {
1797         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1798                 obj1 = to_union(ctx, obj1);
1799         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1800                 obj2 = to_union(ctx, obj2);
1801         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1802                 obj1 = to_union(ctx, obj1);
1803         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1804                 obj2 = to_union(ctx, obj2);
1805         if (obj1.type == isl_obj_pw_qpolynomial &&
1806             obj2.type == isl_obj_union_pw_qpolynomial)
1807                 obj1 = to_union(ctx, obj1);
1808         if (obj1.type == isl_obj_union_pw_qpolynomial &&
1809             obj2.type == isl_obj_pw_qpolynomial)
1810                 obj2 = to_union(ctx, obj2);
1811         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1812             obj2.type == isl_obj_union_pw_qpolynomial_fold)
1813                 obj1 = to_union(ctx, obj1);
1814         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1815             obj2.type == isl_obj_pw_qpolynomial_fold)
1816                 obj2 = to_union(ctx, obj2);
1817         isl_assert(ctx, obj1.type == obj2.type, goto error);
1818         if (obj1.type == isl_obj_map && !isl_map_has_equal_dim(obj1.v, obj2.v)) {
1819                 obj1 = to_union(ctx, obj1);
1820                 obj2 = to_union(ctx, obj2);
1821         }
1822         if (obj1.type == isl_obj_set && !isl_set_has_equal_dim(obj1.v, obj2.v)) {
1823                 obj1 = to_union(ctx, obj1);
1824                 obj2 = to_union(ctx, obj2);
1825         }
1826         if (obj1.type == isl_obj_pw_qpolynomial &&
1827             !isl_pw_qpolynomial_has_equal_dim(obj1.v, obj2.v)) {
1828                 obj1 = to_union(ctx, obj1);
1829                 obj2 = to_union(ctx, obj2);
1830         }
1831         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1832             !isl_pw_qpolynomial_fold_has_equal_dim(obj1.v, obj2.v)) {
1833                 obj1 = to_union(ctx, obj1);
1834                 obj2 = to_union(ctx, obj2);
1835         }
1836         obj1.v = obj1.type->add(obj1.v, obj2.v);
1837         return obj1;
1838 error:
1839         obj1.type->free(obj1.v);
1840         obj2.type->free(obj2.v);
1841         obj1.type = isl_obj_none;
1842         obj1.v = NULL;
1843         return obj1;
1844 }
1845
1846 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1847 {
1848         isl_map *map = NULL;
1849         struct isl_token *tok;
1850         struct vars *v = NULL;
1851         struct isl_obj obj = { isl_obj_set, NULL };
1852
1853         tok = next_token(s);
1854         if (!tok) {
1855                 isl_stream_error(s, NULL, "unexpected EOF");
1856                 goto error;
1857         }
1858         if (tok->type == ISL_TOKEN_VALUE) {
1859                 struct isl_token *tok2;
1860                 struct isl_map *map;
1861
1862                 tok2 = isl_stream_next_token(s);
1863                 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1864                     isl_int_is_neg(tok2->u.v)) {
1865                         if (tok2)
1866                                 isl_stream_push_token(s, tok2);
1867                         obj.type = isl_obj_int;
1868                         obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1869                         isl_token_free(tok);
1870                         return obj;
1871                 }
1872                 isl_stream_push_token(s, tok2);
1873                 isl_stream_push_token(s, tok);
1874                 map = map_read_polylib(s, nparam);
1875                 if (!map)
1876                         goto error;
1877                 if (isl_map_dim(map, isl_dim_in) > 0)
1878                         obj.type = isl_obj_map;
1879                 obj.v = map;
1880                 return obj;
1881         }
1882         v = vars_new(s->ctx);
1883         if (!v) {
1884                 isl_stream_push_token(s, tok);
1885                 goto error;
1886         }
1887         map = isl_map_universe(isl_dim_alloc(s->ctx, 0, 0, 0));
1888         if (tok->type == '[') {
1889                 isl_stream_push_token(s, tok);
1890                 map = read_tuple(s, map, isl_dim_param, v);
1891                 if (!map)
1892                         goto error;
1893                 if (nparam >= 0)
1894                         isl_assert(s->ctx, nparam == v->n, goto error);
1895                 tok = isl_stream_next_token(s);
1896                 if (!tok || tok->type != ISL_TOKEN_TO) {
1897                         isl_stream_error(s, tok, "expecting '->'");
1898                         if (tok)
1899                                 isl_stream_push_token(s, tok);
1900                         goto error;
1901                 }
1902                 isl_token_free(tok);
1903                 tok = isl_stream_next_token(s);
1904         } else if (nparam > 0)
1905                 map = isl_map_add_dims(map, isl_dim_param, nparam);
1906         if (!tok || tok->type != '{') {
1907                 isl_stream_error(s, tok, "expecting '{'");
1908                 if (tok)
1909                         isl_stream_push_token(s, tok);
1910                 goto error;
1911         }
1912         isl_token_free(tok);
1913
1914         tok = isl_stream_next_token(s);
1915         if (!tok)
1916                 ;
1917         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1918                 isl_token_free(tok);
1919                 if (isl_stream_eat(s, '='))
1920                         goto error;
1921                 map = read_tuple(s, map, isl_dim_param, v);
1922                 if (!map)
1923                         goto error;
1924                 if (nparam >= 0)
1925                         isl_assert(s->ctx, nparam == v->n, goto error);
1926         } else if (tok->type == '}') {
1927                 obj.type = isl_obj_union_set;
1928                 obj.v = isl_union_set_empty(isl_map_get_dim(map));
1929                 isl_token_free(tok);
1930                 goto done;
1931         } else
1932                 isl_stream_push_token(s, tok);
1933
1934         for (;;) {
1935                 struct isl_obj o;
1936                 tok = NULL;
1937                 o = obj_read_body(s, isl_map_copy(map), v);
1938                 if (o.type == isl_obj_none || !o.v)
1939                         goto error;
1940                 if (!obj.v)
1941                         obj = o;
1942                 else {
1943                         obj = obj_add(s->ctx, obj, o);
1944                         if (obj.type == isl_obj_none || !obj.v)
1945                                 goto error;
1946                 }
1947                 tok = isl_stream_next_token(s);
1948                 if (!tok || tok->type != ';')
1949                         break;
1950                 isl_token_free(tok);
1951                 if (isl_stream_next_token_is(s, '}')) {
1952                         tok = isl_stream_next_token(s);
1953                         break;
1954                 }
1955         }
1956
1957         if (tok && tok->type == '}') {
1958                 isl_token_free(tok);
1959         } else {
1960                 isl_stream_error(s, tok, "unexpected isl_token");
1961                 if (tok)
1962                         isl_token_free(tok);
1963                 goto error;
1964         }
1965 done:
1966         vars_free(v);
1967         isl_map_free(map);
1968
1969         return obj;
1970 error:
1971         isl_map_free(map);
1972         obj.type->free(obj.v);
1973         if (v)
1974                 vars_free(v);
1975         obj.v = NULL;
1976         return obj;
1977 }
1978
1979 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1980 {
1981         return obj_read(s, -1);
1982 }
1983
1984 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1985 {
1986         struct isl_obj obj;
1987
1988         obj = obj_read(s, nparam);
1989         if (obj.v)
1990                 isl_assert(s->ctx, obj.type == isl_obj_map ||
1991                                    obj.type == isl_obj_set, goto error);
1992
1993         return obj.v;
1994 error:
1995         obj.type->free(obj.v);
1996         return NULL;
1997 }
1998
1999 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2000 {
2001         struct isl_obj obj;
2002
2003         obj = obj_read(s, nparam);
2004         if (obj.v)
2005                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2006
2007         return obj.v;
2008 error:
2009         obj.type->free(obj.v);
2010         return NULL;
2011 }
2012
2013 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2014 {
2015         struct isl_obj obj;
2016
2017         obj = obj_read(s, -1);
2018         if (obj.type == isl_obj_map) {
2019                 obj.type = isl_obj_union_map;
2020                 obj.v = isl_union_map_from_map(obj.v);
2021         }
2022         if (obj.type == isl_obj_set) {
2023                 obj.type = isl_obj_union_set;
2024                 obj.v = isl_union_set_from_set(obj.v);
2025         }
2026         if (obj.v)
2027                 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2028                                    obj.type == isl_obj_union_set, goto error);
2029
2030         return obj.v;
2031 error:
2032         obj.type->free(obj.v);
2033         return NULL;
2034 }
2035
2036 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2037 {
2038         struct isl_obj obj;
2039
2040         obj = obj_read(s, -1);
2041         if (obj.type == isl_obj_set) {
2042                 obj.type = isl_obj_union_set;
2043                 obj.v = isl_union_set_from_set(obj.v);
2044         }
2045         if (obj.v)
2046                 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2047
2048         return obj.v;
2049 error:
2050         obj.type->free(obj.v);
2051         return NULL;
2052 }
2053
2054 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2055 {
2056         struct isl_obj obj;
2057         struct isl_map *map;
2058         struct isl_basic_map *bmap;
2059
2060         obj = obj_read(s, nparam);
2061         map = obj.v;
2062         if (!map)
2063                 return NULL;
2064
2065         isl_assert(map->ctx, map->n <= 1, goto error);
2066
2067         if (map->n == 0)
2068                 bmap = isl_basic_map_empty_like_map(map);
2069         else
2070                 bmap = isl_basic_map_copy(map->p[0]);
2071
2072         isl_map_free(map);
2073
2074         return bmap;
2075 error:
2076         isl_map_free(map);
2077         return NULL;
2078 }
2079
2080 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2081                 FILE *input, int nparam)
2082 {
2083         struct isl_basic_map *bmap;
2084         struct isl_stream *s = isl_stream_new_file(ctx, input);
2085         if (!s)
2086                 return NULL;
2087         bmap = basic_map_read(s, nparam);
2088         isl_stream_free(s);
2089         return bmap;
2090 }
2091
2092 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2093                 FILE *input, int nparam)
2094 {
2095         struct isl_basic_map *bmap;
2096         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
2097         if (!bmap)
2098                 return NULL;
2099         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2100         return (struct isl_basic_set *)bmap;
2101 error:
2102         isl_basic_map_free(bmap);
2103         return NULL;
2104 }
2105
2106 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2107                 const char *str, int nparam)
2108 {
2109         struct isl_basic_map *bmap;
2110         struct isl_stream *s = isl_stream_new_str(ctx, str);
2111         if (!s)
2112                 return NULL;
2113         bmap = basic_map_read(s, nparam);
2114         isl_stream_free(s);
2115         return bmap;
2116 }
2117
2118 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2119                 const char *str, int nparam)
2120 {
2121         struct isl_basic_map *bmap;
2122         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
2123         if (!bmap)
2124                 return NULL;
2125         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
2126         return (struct isl_basic_set *)bmap;
2127 error:
2128         isl_basic_map_free(bmap);
2129         return NULL;
2130 }
2131
2132 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2133                 FILE *input, int nparam)
2134 {
2135         struct isl_map *map;
2136         struct isl_stream *s = isl_stream_new_file(ctx, input);
2137         if (!s)
2138                 return NULL;
2139         map = isl_stream_read_map(s, nparam);
2140         isl_stream_free(s);
2141         return map;
2142 }
2143
2144 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2145                 const char *str, int nparam)
2146 {
2147         struct isl_map *map;
2148         struct isl_stream *s = isl_stream_new_str(ctx, str);
2149         if (!s)
2150                 return NULL;
2151         map = isl_stream_read_map(s, nparam);
2152         isl_stream_free(s);
2153         return map;
2154 }
2155
2156 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2157                 FILE *input, int nparam)
2158 {
2159         struct isl_map *map;
2160         map = isl_map_read_from_file(ctx, input, nparam);
2161         if (!map)
2162                 return NULL;
2163         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2164         return (struct isl_set *)map;
2165 error:
2166         isl_map_free(map);
2167         return NULL;
2168 }
2169
2170 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2171                 const char *str, int nparam)
2172 {
2173         struct isl_map *map;
2174         map = isl_map_read_from_str(ctx, str, nparam);
2175         if (!map)
2176                 return NULL;
2177         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
2178         return (struct isl_set *)map;
2179 error:
2180         isl_map_free(map);
2181         return NULL;
2182 }
2183
2184 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2185         FILE *input)
2186 {
2187         isl_union_map *umap;
2188         struct isl_stream *s = isl_stream_new_file(ctx, input);
2189         if (!s)
2190                 return NULL;
2191         umap = isl_stream_read_union_map(s);
2192         isl_stream_free(s);
2193         return umap;
2194 }
2195
2196 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2197                 const char *str)
2198 {
2199         isl_union_map *umap;
2200         struct isl_stream *s = isl_stream_new_str(ctx, str);
2201         if (!s)
2202                 return NULL;
2203         umap = isl_stream_read_union_map(s);
2204         isl_stream_free(s);
2205         return umap;
2206 }
2207
2208 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2209         FILE *input)
2210 {
2211         isl_union_set *uset;
2212         struct isl_stream *s = isl_stream_new_file(ctx, input);
2213         if (!s)
2214                 return NULL;
2215         uset = isl_stream_read_union_set(s);
2216         isl_stream_free(s);
2217         return uset;
2218 }
2219
2220 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2221                 const char *str)
2222 {
2223         isl_union_set *uset;
2224         struct isl_stream *s = isl_stream_new_str(ctx, str);
2225         if (!s)
2226                 return NULL;
2227         uset = isl_stream_read_union_set(s);
2228         isl_stream_free(s);
2229         return uset;
2230 }
2231
2232 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2233 {
2234         struct isl_vec *vec = NULL;
2235         struct isl_token *tok;
2236         unsigned size;
2237         int j;
2238
2239         tok = isl_stream_next_token(s);
2240         if (!tok || tok->type != ISL_TOKEN_VALUE) {
2241                 isl_stream_error(s, tok, "expecting vector length");
2242                 goto error;
2243         }
2244
2245         size = isl_int_get_si(tok->u.v);
2246         isl_token_free(tok);
2247
2248         vec = isl_vec_alloc(s->ctx, size);
2249
2250         for (j = 0; j < size; ++j) {
2251                 tok = isl_stream_next_token(s);
2252                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2253                         isl_stream_error(s, tok, "expecting constant value");
2254                         goto error;
2255                 }
2256                 isl_int_set(vec->el[j], tok->u.v);
2257                 isl_token_free(tok);
2258         }
2259
2260         return vec;
2261 error:
2262         isl_token_free(tok);
2263         isl_vec_free(vec);
2264         return NULL;
2265 }
2266
2267 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2268 {
2269         return isl_vec_read_polylib(s);
2270 }
2271
2272 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2273 {
2274         isl_vec *v;
2275         struct isl_stream *s = isl_stream_new_file(ctx, input);
2276         if (!s)
2277                 return NULL;
2278         v = vec_read(s);
2279         isl_stream_free(s);
2280         return v;
2281 }
2282
2283 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2284         struct isl_stream *s)
2285 {
2286         struct isl_obj obj;
2287
2288         obj = obj_read(s, -1);
2289         if (obj.v)
2290                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2291                            goto error);
2292
2293         return obj.v;
2294 error:
2295         obj.type->free(obj.v);
2296         return NULL;
2297 }
2298
2299 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2300                 const char *str)
2301 {
2302         isl_pw_qpolynomial *pwqp;
2303         struct isl_stream *s = isl_stream_new_str(ctx, str);
2304         if (!s)
2305                 return NULL;
2306         pwqp = isl_stream_read_pw_qpolynomial(s);
2307         isl_stream_free(s);
2308         return pwqp;
2309 }
2310
2311 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2312                 FILE *input)
2313 {
2314         isl_pw_qpolynomial *pwqp;
2315         struct isl_stream *s = isl_stream_new_file(ctx, input);
2316         if (!s)
2317                 return NULL;
2318         pwqp = isl_stream_read_pw_qpolynomial(s);
2319         isl_stream_free(s);
2320         return pwqp;
2321 }