isl_tab_basic_set_non_trivial_lexmin: drop explicit conversion to isl_basic_map
[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(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(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
449         aff = isl_aff_zero(isl_local_space_from_space(isl_pw_aff_get_space(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_space *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_space(isl_space_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_space_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_space_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_space_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_space_free(dim);
539         return res;
540 error:
541         isl_space_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_space *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_space *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_space_wrap(isl_map_get_space(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_space(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_space *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_space_unwrap(isl_pw_aff_get_space(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_space_wrap(isl_map_get_space(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_space *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_space_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_space_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_space_free(dim);
774         return list;
775 error:
776         isl_space_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_set *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         return isl_map_wrap(map);
861 error:
862         isl_map_free(map);
863         return NULL;
864 }
865
866 static __isl_give isl_map *read_tuple(struct isl_stream *s,
867         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
868 {
869         struct isl_token *tok;
870         char *name = NULL;
871
872         tok = isl_stream_next_token(s);
873         if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
874                 name = strdup(tok->u.s);
875                 if (!name)
876                         goto error;
877                 isl_token_free(tok);
878                 tok = isl_stream_next_token(s);
879         }
880         if (!tok || tok->type != '[') {
881                 isl_stream_error(s, tok, "expecting '['");
882                 goto error;
883         }
884         isl_token_free(tok);
885         if (type != isl_dim_param && next_is_tuple(s)) {
886                 isl_space *dim = isl_map_get_space(map);
887                 int nparam = isl_space_dim(dim, isl_dim_param);
888                 int n_in = isl_space_dim(dim, isl_dim_in);
889                 isl_set *nested;
890                 if (type == isl_dim_out) {
891                         dim = isl_space_move_dims(dim, isl_dim_param, nparam,
892                                                 isl_dim_in, 0, n_in);
893                         dim = isl_space_params(dim);
894                 }
895                 nested = read_nested_tuple(s, isl_map_universe(dim), 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_set_get_space(nested);
902                         dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
903                         dim = isl_space_join(isl_map_get_space(map), dim);
904                         set = isl_map_domain(map);
905                         nested = isl_map_reset_space(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_space(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_space(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_space_wrap(isl_map_get_space(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_space *dim = isl_map_get_space(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_space *dim = isl_map_get_space(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_space(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_space(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_space(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_space(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_space(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_space(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_set *set, 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
1672         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1673                 return obj_read_poly(s, set, v, n);
1674
1675         if (isl_stream_eat(s, '('))
1676                 goto error;
1677
1678         pwqp = read_term(s, set, v);
1679         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1680
1681         while (isl_stream_eat_if_available(s, ',')) {
1682                 isl_pw_qpolynomial_fold *pwf_i;
1683                 pwqp = read_term(s, set, v);
1684                 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1685                                                                         pwqp);
1686                 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1687         }
1688
1689         if (isl_stream_eat(s, ')'))
1690                 goto error;
1691
1692         set = read_optional_disjuncts(s, set, v);
1693         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1694
1695         vars_drop(v, v->n - n);
1696
1697         obj.v = pwf;
1698         return obj;
1699 error:
1700         isl_set_free(set);
1701         isl_pw_qpolynomial_fold_free(pwf);
1702         obj.type = isl_obj_none;
1703         return obj;
1704 }
1705
1706 static int is_rational(struct isl_stream *s)
1707 {
1708         struct isl_token *tok;
1709
1710         tok = isl_stream_next_token(s);
1711         if (!tok)
1712                 return 0;
1713         if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1714                 isl_token_free(tok);
1715                 isl_stream_eat(s, ':');
1716                 return 1;
1717         }
1718
1719         isl_stream_push_token(s, tok);
1720
1721         return 0;
1722 }
1723
1724 static struct isl_obj obj_read_body(struct isl_stream *s,
1725         __isl_take isl_map *map, struct vars *v)
1726 {
1727         struct isl_token *tok;
1728         struct isl_obj obj = { isl_obj_set, NULL };
1729         int n = v->n;
1730
1731         if (is_rational(s))
1732                 map = isl_map_set_rational(map);
1733
1734         if (isl_stream_next_token_is(s, ':')) {
1735                 obj.type = isl_obj_set;
1736                 obj.v = read_optional_disjuncts(s, map, v);
1737                 return obj;
1738         }
1739
1740         if (!next_is_tuple(s))
1741                 return obj_read_poly_or_fold(s, map, v, n);
1742
1743         map = read_tuple(s, map, isl_dim_in, v);
1744         if (!map)
1745                 goto error;
1746         tok = isl_stream_next_token(s);
1747         if (tok && tok->type == ISL_TOKEN_TO) {
1748                 obj.type = isl_obj_map;
1749                 isl_token_free(tok);
1750                 if (!next_is_tuple(s)) {
1751                         isl_set *set = isl_map_domain(map);
1752                         return obj_read_poly_or_fold(s, set, v, n);
1753                 }
1754                 map = read_tuple(s, map, isl_dim_out, v);
1755                 if (!map)
1756                         goto error;
1757         } else {
1758                 map = isl_map_reverse(map);
1759                 if (tok)
1760                         isl_stream_push_token(s, tok);
1761         }
1762
1763         map = read_optional_disjuncts(s, map, v);
1764
1765         vars_drop(v, v->n - n);
1766
1767         obj.v = map;
1768         return obj;
1769 error:
1770         isl_map_free(map);
1771         obj.type = isl_obj_none;
1772         return obj;
1773 }
1774
1775 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1776 {
1777         if (obj.type == isl_obj_map) {
1778                 obj.v = isl_union_map_from_map(obj.v);
1779                 obj.type = isl_obj_union_map;
1780         } else if (obj.type == isl_obj_set) {
1781                 obj.v = isl_union_set_from_set(obj.v);
1782                 obj.type = isl_obj_union_set;
1783         } else if (obj.type == isl_obj_pw_qpolynomial) {
1784                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1785                 obj.type = isl_obj_union_pw_qpolynomial;
1786         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1787                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1788                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1789         } else
1790                 isl_assert(ctx, 0, goto error);
1791         return obj;
1792 error:
1793         obj.type->free(obj.v);
1794         obj.type = isl_obj_none;
1795         return obj;
1796 }
1797
1798 static struct isl_obj obj_add(struct isl_ctx *ctx,
1799         struct isl_obj obj1, struct isl_obj obj2)
1800 {
1801         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1802                 obj1 = to_union(ctx, obj1);
1803         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1804                 obj2 = to_union(ctx, obj2);
1805         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1806                 obj1 = to_union(ctx, obj1);
1807         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1808                 obj2 = to_union(ctx, obj2);
1809         if (obj1.type == isl_obj_pw_qpolynomial &&
1810             obj2.type == isl_obj_union_pw_qpolynomial)
1811                 obj1 = to_union(ctx, obj1);
1812         if (obj1.type == isl_obj_union_pw_qpolynomial &&
1813             obj2.type == isl_obj_pw_qpolynomial)
1814                 obj2 = to_union(ctx, obj2);
1815         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1816             obj2.type == isl_obj_union_pw_qpolynomial_fold)
1817                 obj1 = to_union(ctx, obj1);
1818         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1819             obj2.type == isl_obj_pw_qpolynomial_fold)
1820                 obj2 = to_union(ctx, obj2);
1821         isl_assert(ctx, obj1.type == obj2.type, goto error);
1822         if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
1823                 obj1 = to_union(ctx, obj1);
1824                 obj2 = to_union(ctx, obj2);
1825         }
1826         if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
1827                 obj1 = to_union(ctx, obj1);
1828                 obj2 = to_union(ctx, obj2);
1829         }
1830         if (obj1.type == isl_obj_pw_qpolynomial &&
1831             !isl_pw_qpolynomial_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_fold &&
1836             !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
1837                 obj1 = to_union(ctx, obj1);
1838                 obj2 = to_union(ctx, obj2);
1839         }
1840         obj1.v = obj1.type->add(obj1.v, obj2.v);
1841         return obj1;
1842 error:
1843         obj1.type->free(obj1.v);
1844         obj2.type->free(obj2.v);
1845         obj1.type = isl_obj_none;
1846         obj1.v = NULL;
1847         return obj1;
1848 }
1849
1850 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1851 {
1852         isl_map *map = NULL;
1853         struct isl_token *tok;
1854         struct vars *v = NULL;
1855         struct isl_obj obj = { isl_obj_set, NULL };
1856
1857         tok = next_token(s);
1858         if (!tok) {
1859                 isl_stream_error(s, NULL, "unexpected EOF");
1860                 goto error;
1861         }
1862         if (tok->type == ISL_TOKEN_VALUE) {
1863                 struct isl_token *tok2;
1864                 struct isl_map *map;
1865
1866                 tok2 = isl_stream_next_token(s);
1867                 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1868                     isl_int_is_neg(tok2->u.v)) {
1869                         if (tok2)
1870                                 isl_stream_push_token(s, tok2);
1871                         obj.type = isl_obj_int;
1872                         obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1873                         isl_token_free(tok);
1874                         return obj;
1875                 }
1876                 isl_stream_push_token(s, tok2);
1877                 isl_stream_push_token(s, tok);
1878                 map = map_read_polylib(s, nparam);
1879                 if (!map)
1880                         goto error;
1881                 if (isl_map_may_be_set(map))
1882                         obj.v = isl_map_range(map);
1883                 else {
1884                         obj.type = isl_obj_map;
1885                         obj.v = map;
1886                 }
1887                 return obj;
1888         }
1889         v = vars_new(s->ctx);
1890         if (!v) {
1891                 isl_stream_push_token(s, tok);
1892                 goto error;
1893         }
1894         map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
1895         if (tok->type == '[') {
1896                 isl_stream_push_token(s, tok);
1897                 map = read_tuple(s, map, isl_dim_param, v);
1898                 if (!map)
1899                         goto error;
1900                 if (nparam >= 0)
1901                         isl_assert(s->ctx, nparam == v->n, goto error);
1902                 tok = isl_stream_next_token(s);
1903                 if (!tok || tok->type != ISL_TOKEN_TO) {
1904                         isl_stream_error(s, tok, "expecting '->'");
1905                         if (tok)
1906                                 isl_stream_push_token(s, tok);
1907                         goto error;
1908                 }
1909                 isl_token_free(tok);
1910                 tok = isl_stream_next_token(s);
1911         } else if (nparam > 0)
1912                 map = isl_map_add_dims(map, isl_dim_param, nparam);
1913         if (!tok || tok->type != '{') {
1914                 isl_stream_error(s, tok, "expecting '{'");
1915                 if (tok)
1916                         isl_stream_push_token(s, tok);
1917                 goto error;
1918         }
1919         isl_token_free(tok);
1920
1921         tok = isl_stream_next_token(s);
1922         if (!tok)
1923                 ;
1924         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1925                 isl_token_free(tok);
1926                 if (isl_stream_eat(s, '='))
1927                         goto error;
1928                 map = read_tuple(s, map, isl_dim_param, v);
1929                 if (!map)
1930                         goto error;
1931                 if (nparam >= 0)
1932                         isl_assert(s->ctx, nparam == v->n, goto error);
1933         } else if (tok->type == '}') {
1934                 obj.type = isl_obj_union_set;
1935                 obj.v = isl_union_set_empty(isl_map_get_space(map));
1936                 isl_token_free(tok);
1937                 goto done;
1938         } else
1939                 isl_stream_push_token(s, tok);
1940
1941         for (;;) {
1942                 struct isl_obj o;
1943                 tok = NULL;
1944                 o = obj_read_body(s, isl_map_copy(map), v);
1945                 if (o.type == isl_obj_none || !o.v)
1946                         goto error;
1947                 if (!obj.v)
1948                         obj = o;
1949                 else {
1950                         obj = obj_add(s->ctx, obj, o);
1951                         if (obj.type == isl_obj_none || !obj.v)
1952                                 goto error;
1953                 }
1954                 tok = isl_stream_next_token(s);
1955                 if (!tok || tok->type != ';')
1956                         break;
1957                 isl_token_free(tok);
1958                 if (isl_stream_next_token_is(s, '}')) {
1959                         tok = isl_stream_next_token(s);
1960                         break;
1961                 }
1962         }
1963
1964         if (tok && tok->type == '}') {
1965                 isl_token_free(tok);
1966         } else {
1967                 isl_stream_error(s, tok, "unexpected isl_token");
1968                 if (tok)
1969                         isl_token_free(tok);
1970                 goto error;
1971         }
1972 done:
1973         vars_free(v);
1974         isl_map_free(map);
1975
1976         return obj;
1977 error:
1978         isl_map_free(map);
1979         obj.type->free(obj.v);
1980         if (v)
1981                 vars_free(v);
1982         obj.v = NULL;
1983         return obj;
1984 }
1985
1986 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1987 {
1988         return obj_read(s, -1);
1989 }
1990
1991 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1992 {
1993         struct isl_obj obj;
1994
1995         obj = obj_read(s, nparam);
1996         if (obj.v)
1997                 isl_assert(s->ctx, obj.type == isl_obj_map ||
1998                                    obj.type == isl_obj_set, goto error);
1999
2000         return obj.v;
2001 error:
2002         obj.type->free(obj.v);
2003         return NULL;
2004 }
2005
2006 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2007 {
2008         struct isl_obj obj;
2009
2010         obj = obj_read(s, nparam);
2011         if (obj.v) {
2012                 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2013                         obj.v = isl_map_range(obj.v);
2014                         obj.type = isl_obj_set;
2015                 }
2016                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2017         }
2018
2019         return obj.v;
2020 error:
2021         obj.type->free(obj.v);
2022         return NULL;
2023 }
2024
2025 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2026 {
2027         struct isl_obj obj;
2028
2029         obj = obj_read(s, -1);
2030         if (obj.type == isl_obj_map) {
2031                 obj.type = isl_obj_union_map;
2032                 obj.v = isl_union_map_from_map(obj.v);
2033         }
2034         if (obj.type == isl_obj_set) {
2035                 obj.type = isl_obj_union_set;
2036                 obj.v = isl_union_set_from_set(obj.v);
2037         }
2038         if (obj.v)
2039                 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2040                                    obj.type == isl_obj_union_set, goto error);
2041
2042         return obj.v;
2043 error:
2044         obj.type->free(obj.v);
2045         return NULL;
2046 }
2047
2048 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2049 {
2050         struct isl_obj obj;
2051
2052         obj = obj_read(s, -1);
2053         if (obj.type == isl_obj_set) {
2054                 obj.type = isl_obj_union_set;
2055                 obj.v = isl_union_set_from_set(obj.v);
2056         }
2057         if (obj.v)
2058                 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2059
2060         return obj.v;
2061 error:
2062         obj.type->free(obj.v);
2063         return NULL;
2064 }
2065
2066 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2067 {
2068         struct isl_obj obj;
2069         struct isl_map *map;
2070         struct isl_basic_map *bmap;
2071
2072         obj = obj_read(s, nparam);
2073         map = obj.v;
2074         if (!map)
2075                 return NULL;
2076
2077         isl_assert(map->ctx, map->n <= 1, goto error);
2078
2079         if (map->n == 0)
2080                 bmap = isl_basic_map_empty_like_map(map);
2081         else
2082                 bmap = isl_basic_map_copy(map->p[0]);
2083
2084         isl_map_free(map);
2085
2086         return bmap;
2087 error:
2088         isl_map_free(map);
2089         return NULL;
2090 }
2091
2092 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s,
2093         int nparam)
2094 {
2095         isl_basic_map *bmap;
2096         bmap = basic_map_read(s, nparam);
2097         if (!bmap)
2098                 return NULL;
2099         if (!isl_basic_map_may_be_set(bmap))
2100                 isl_die(s->ctx, isl_error_invalid,
2101                         "input is not a set", goto error);
2102         return isl_basic_map_range(bmap);
2103 error:
2104         isl_basic_map_free(bmap);
2105         return NULL;
2106 }
2107
2108 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2109                 FILE *input, int nparam)
2110 {
2111         struct isl_basic_map *bmap;
2112         struct isl_stream *s = isl_stream_new_file(ctx, input);
2113         if (!s)
2114                 return NULL;
2115         bmap = basic_map_read(s, nparam);
2116         isl_stream_free(s);
2117         return bmap;
2118 }
2119
2120 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2121                 FILE *input, int nparam)
2122 {
2123         isl_basic_set *bset;
2124         struct isl_stream *s = isl_stream_new_file(ctx, input);
2125         if (!s)
2126                 return NULL;
2127         bset = basic_set_read(s, nparam);
2128         isl_stream_free(s);
2129         return bset;
2130 }
2131
2132 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2133                 const char *str, int nparam)
2134 {
2135         struct isl_basic_map *bmap;
2136         struct isl_stream *s = isl_stream_new_str(ctx, str);
2137         if (!s)
2138                 return NULL;
2139         bmap = basic_map_read(s, nparam);
2140         isl_stream_free(s);
2141         return bmap;
2142 }
2143
2144 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2145                 const char *str, int nparam)
2146 {
2147         isl_basic_set *bset;
2148         struct isl_stream *s = isl_stream_new_str(ctx, str);
2149         if (!s)
2150                 return NULL;
2151         bset = basic_set_read(s, nparam);
2152         isl_stream_free(s);
2153         return bset;
2154 }
2155
2156 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2157                 FILE *input, int nparam)
2158 {
2159         struct isl_map *map;
2160         struct isl_stream *s = isl_stream_new_file(ctx, input);
2161         if (!s)
2162                 return NULL;
2163         map = isl_stream_read_map(s, nparam);
2164         isl_stream_free(s);
2165         return map;
2166 }
2167
2168 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2169                 const char *str, int nparam)
2170 {
2171         struct isl_map *map;
2172         struct isl_stream *s = isl_stream_new_str(ctx, str);
2173         if (!s)
2174                 return NULL;
2175         map = isl_stream_read_map(s, nparam);
2176         isl_stream_free(s);
2177         return map;
2178 }
2179
2180 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2181                 FILE *input, int nparam)
2182 {
2183         isl_set *set;
2184         struct isl_stream *s = isl_stream_new_file(ctx, input);
2185         if (!s)
2186                 return NULL;
2187         set = isl_stream_read_set(s, nparam);
2188         isl_stream_free(s);
2189         return set;
2190 }
2191
2192 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2193                 const char *str, int nparam)
2194 {
2195         isl_set *set;
2196         struct isl_stream *s = isl_stream_new_str(ctx, str);
2197         if (!s)
2198                 return NULL;
2199         set = isl_stream_read_set(s, nparam);
2200         isl_stream_free(s);
2201         return set;
2202 }
2203
2204 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2205         FILE *input)
2206 {
2207         isl_union_map *umap;
2208         struct isl_stream *s = isl_stream_new_file(ctx, input);
2209         if (!s)
2210                 return NULL;
2211         umap = isl_stream_read_union_map(s);
2212         isl_stream_free(s);
2213         return umap;
2214 }
2215
2216 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2217                 const char *str)
2218 {
2219         isl_union_map *umap;
2220         struct isl_stream *s = isl_stream_new_str(ctx, str);
2221         if (!s)
2222                 return NULL;
2223         umap = isl_stream_read_union_map(s);
2224         isl_stream_free(s);
2225         return umap;
2226 }
2227
2228 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2229         FILE *input)
2230 {
2231         isl_union_set *uset;
2232         struct isl_stream *s = isl_stream_new_file(ctx, input);
2233         if (!s)
2234                 return NULL;
2235         uset = isl_stream_read_union_set(s);
2236         isl_stream_free(s);
2237         return uset;
2238 }
2239
2240 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2241                 const char *str)
2242 {
2243         isl_union_set *uset;
2244         struct isl_stream *s = isl_stream_new_str(ctx, str);
2245         if (!s)
2246                 return NULL;
2247         uset = isl_stream_read_union_set(s);
2248         isl_stream_free(s);
2249         return uset;
2250 }
2251
2252 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2253 {
2254         struct isl_vec *vec = NULL;
2255         struct isl_token *tok;
2256         unsigned size;
2257         int j;
2258
2259         tok = isl_stream_next_token(s);
2260         if (!tok || tok->type != ISL_TOKEN_VALUE) {
2261                 isl_stream_error(s, tok, "expecting vector length");
2262                 goto error;
2263         }
2264
2265         size = isl_int_get_si(tok->u.v);
2266         isl_token_free(tok);
2267
2268         vec = isl_vec_alloc(s->ctx, size);
2269
2270         for (j = 0; j < size; ++j) {
2271                 tok = isl_stream_next_token(s);
2272                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2273                         isl_stream_error(s, tok, "expecting constant value");
2274                         goto error;
2275                 }
2276                 isl_int_set(vec->el[j], tok->u.v);
2277                 isl_token_free(tok);
2278         }
2279
2280         return vec;
2281 error:
2282         isl_token_free(tok);
2283         isl_vec_free(vec);
2284         return NULL;
2285 }
2286
2287 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2288 {
2289         return isl_vec_read_polylib(s);
2290 }
2291
2292 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2293 {
2294         isl_vec *v;
2295         struct isl_stream *s = isl_stream_new_file(ctx, input);
2296         if (!s)
2297                 return NULL;
2298         v = vec_read(s);
2299         isl_stream_free(s);
2300         return v;
2301 }
2302
2303 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2304         struct isl_stream *s)
2305 {
2306         struct isl_obj obj;
2307
2308         obj = obj_read(s, -1);
2309         if (obj.v)
2310                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2311                            goto error);
2312
2313         return obj.v;
2314 error:
2315         obj.type->free(obj.v);
2316         return NULL;
2317 }
2318
2319 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2320                 const char *str)
2321 {
2322         isl_pw_qpolynomial *pwqp;
2323         struct isl_stream *s = isl_stream_new_str(ctx, str);
2324         if (!s)
2325                 return NULL;
2326         pwqp = isl_stream_read_pw_qpolynomial(s);
2327         isl_stream_free(s);
2328         return pwqp;
2329 }
2330
2331 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2332                 FILE *input)
2333 {
2334         isl_pw_qpolynomial *pwqp;
2335         struct isl_stream *s = isl_stream_new_file(ctx, input);
2336         if (!s)
2337                 return NULL;
2338         pwqp = isl_stream_read_pw_qpolynomial(s);
2339         isl_stream_free(s);
2340         return pwqp;
2341 }