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