initial isl packaging
[platform/upstream/cloog-isl.git] / isl / isl_input.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  * Copyright 2012      Ecole Normale Superieure
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, K.U.Leuven, Departement
9  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
12  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13  */
14
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl/set.h>
21 #include <isl/seq.h>
22 #include <isl_stream_private.h>
23 #include <isl/obj.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl/list.h>
29
30 struct variable {
31         char                    *name;
32         int                      pos;
33         struct variable         *next;
34 };
35
36 struct vars {
37         struct isl_ctx  *ctx;
38         int              n;
39         struct variable *v;
40 };
41
42 static struct vars *vars_new(struct isl_ctx *ctx)
43 {
44         struct vars *v;
45         v = isl_alloc_type(ctx, struct vars);
46         if (!v)
47                 return NULL;
48         v->ctx = ctx;
49         v->n = 0;
50         v->v = NULL;
51         return v;
52 }
53
54 static void variable_free(struct variable *var)
55 {
56         while (var) {
57                 struct variable *next = var->next;
58                 free(var->name);
59                 free(var);
60                 var = next;
61         }
62 }
63
64 static void vars_free(struct vars *v)
65 {
66         if (!v)
67                 return;
68         variable_free(v->v);
69         free(v);
70 }
71
72 static void vars_drop(struct vars *v, int n)
73 {
74         struct variable *var;
75
76         if (!v || !v->v)
77                 return;
78
79         v->n -= n;
80
81         var = v->v;
82         while (--n >= 0) {
83                 struct variable *next = var->next;
84                 free(var->name);
85                 free(var);
86                 var = next;
87         }
88         v->v = var;
89 }
90
91 static struct variable *variable_new(struct vars *v, const char *name, int len,
92                                 int pos)
93 {
94         struct variable *var;
95         var = isl_calloc_type(v->ctx, struct variable);
96         if (!var)
97                 goto error;
98         var->name = strdup(name);
99         var->name[len] = '\0';
100         var->pos = pos;
101         var->next = v->v;
102         return var;
103 error:
104         variable_free(v->v);
105         return NULL;
106 }
107
108 static int vars_pos(struct vars *v, const char *s, int len)
109 {
110         int pos;
111         struct variable *q;
112
113         if (len == -1)
114                 len = strlen(s);
115         for (q = v->v; q; q = q->next) {
116                 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
117                         break;
118         }
119         if (q)
120                 pos = q->pos;
121         else {
122                 pos = v->n;
123                 v->v = variable_new(v, s, len, v->n);
124                 if (!v->v)
125                         return -1;
126                 v->n++;
127         }
128         return pos;
129 }
130
131 static int vars_add_anon(struct vars *v)
132 {
133         v->v = variable_new(v, "", 0, v->n);
134
135         if (!v->v)
136                 return -1;
137         v->n++;
138
139         return 0;
140 }
141
142 /* Obtain next token, with some preprocessing.
143  * In particular, evaluate expressions of the form x^y,
144  * with x and y values.
145  */
146 static struct isl_token *next_token(struct isl_stream *s)
147 {
148         struct isl_token *tok, *tok2;
149
150         tok = isl_stream_next_token(s);
151         if (!tok || tok->type != ISL_TOKEN_VALUE)
152                 return tok;
153         if (!isl_stream_eat_if_available(s, '^'))
154                 return tok;
155         tok2 = isl_stream_next_token(s);
156         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
157                 isl_stream_error(s, tok2, "expecting constant value");
158                 goto error;
159         }
160
161         isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
162
163         isl_token_free(tok2);
164         return tok;
165 error:
166         isl_token_free(tok);
167         isl_token_free(tok2);
168         return NULL;
169 }
170
171 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
172 {
173         struct isl_token *tok;
174
175         tok = next_token(s);
176         if (!tok || tok->type != ISL_TOKEN_VALUE) {
177                 isl_stream_error(s, tok, "expecting constant value");
178                 goto error;
179         }
180
181         isl_int_mul(*f, *f, tok->u.v);
182
183         isl_token_free(tok);
184
185         if (isl_stream_eat_if_available(s, '*'))
186                 return accept_cst_factor(s, f);
187
188         return 0;
189 error:
190         isl_token_free(tok);
191         return -1;
192 }
193
194 /* Given an affine expression aff, return an affine expression
195  * for aff % d, with d the next token on the stream, which is
196  * assumed to be a constant.
197  *
198  * We introduce an integer division q = [aff/d] and the result
199  * is set to aff - d q.
200  */
201 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
202         struct vars *v, __isl_take isl_pw_aff *aff)
203 {
204         struct isl_token *tok;
205         isl_pw_aff *q;
206
207         tok = next_token(s);
208         if (!tok || tok->type != ISL_TOKEN_VALUE) {
209                 isl_stream_error(s, tok, "expecting constant value");
210                 goto error;
211         }
212
213         q = isl_pw_aff_copy(aff);
214         q = isl_pw_aff_scale_down(q, tok->u.v);
215         q = isl_pw_aff_floor(q);
216         q = isl_pw_aff_scale(q, tok->u.v);
217
218         aff = isl_pw_aff_sub(aff, q);
219
220         isl_token_free(tok);
221         return aff;
222 error:
223         isl_pw_aff_free(aff);
224         isl_token_free(tok);
225         return NULL;
226 }
227
228 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
229         __isl_take isl_space *dim, struct vars *v);
230 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
231         __isl_take isl_space *dim, struct vars *v);
232
233 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
234         __isl_take isl_space *dim, struct vars *v)
235 {
236         struct isl_token *tok;
237         isl_pw_aff_list *list = NULL;
238         int min;
239
240         tok = isl_stream_next_token(s);
241         if (!tok)
242                 goto error;
243         min = tok->type == ISL_TOKEN_MIN;
244         isl_token_free(tok);
245
246         if (isl_stream_eat(s, '('))
247                 goto error;
248
249         list = accept_affine_list(s, isl_space_copy(dim), v);
250         if (!list)
251                 goto error;
252
253         if (isl_stream_eat(s, ')'))
254                 goto error;
255
256         isl_space_free(dim);
257         return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
258 error:
259         isl_space_free(dim);
260         isl_pw_aff_list_free(list);
261         return NULL;
262 }
263
264 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
265         __isl_take isl_space *dim, struct vars *v)
266 {
267         struct isl_token *tok;
268         int f = 0;
269         int c = 0;
270         isl_pw_aff *pwaff = NULL;
271
272         if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
273                 f = 1;
274         else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
275                 c = 1;
276         if (f || c) {
277                 if (isl_stream_eat(s, '('))
278                         goto error;
279         } else {
280                 if (isl_stream_eat(s, '['))
281                         goto error;
282         }
283
284         pwaff = accept_affine(s, isl_space_copy(dim), v);
285
286         if (f || c) {
287                 if (isl_stream_eat(s, ','))
288                         goto error;
289
290                 tok = next_token(s);
291                 if (!tok)
292                         goto error;
293                 if (tok->type != ISL_TOKEN_VALUE) {
294                         isl_stream_error(s, tok, "expected denominator");
295                         isl_stream_push_token(s, tok);
296                         goto error;
297                 }
298                 isl_pw_aff_scale_down(pwaff,  tok->u.v);
299                 isl_token_free(tok);
300         }
301
302         if (c)
303                 pwaff = isl_pw_aff_ceil(pwaff);
304         else
305                 pwaff = isl_pw_aff_floor(pwaff);
306
307         if (f || c) {
308                 if (isl_stream_eat(s, ')'))
309                         goto error;
310         } else {
311                 if (isl_stream_eat(s, ']'))
312                         goto error;
313         }
314
315         isl_space_free(dim);
316         return pwaff;
317 error:
318         isl_space_free(dim);
319         isl_pw_aff_free(pwaff);
320         return NULL;
321 }
322
323 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
324         __isl_take isl_space *dim, struct vars *v)
325 {
326         struct isl_token *tok = NULL;
327         isl_pw_aff *res = NULL;
328
329         tok = next_token(s);
330         if (!tok) {
331                 isl_stream_error(s, NULL, "unexpected EOF");
332                 goto error;
333         }
334
335         if (tok->type == ISL_TOKEN_AFF) {
336                 res = isl_pw_aff_copy(tok->u.pwaff);
337                 isl_token_free(tok);
338         } else if (tok->type == ISL_TOKEN_IDENT) {
339                 int n = v->n;
340                 int pos = vars_pos(v, tok->u.s, -1);
341                 isl_aff *aff;
342
343                 if (pos < 0)
344                         goto error;
345                 if (pos >= n) {
346                         isl_stream_error(s, tok, "unknown identifier");
347                         goto error;
348                 }
349
350                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
351                 if (!aff)
352                         goto error;
353                 isl_int_set_si(aff->v->el[2 + pos], 1);
354                 res = isl_pw_aff_from_aff(aff);
355                 isl_token_free(tok);
356         } else if (tok->type == ISL_TOKEN_VALUE) {
357                 if (isl_stream_eat_if_available(s, '*')) {
358                         res = accept_affine_factor(s, isl_space_copy(dim), v);
359                         res = isl_pw_aff_scale(res, tok->u.v);
360                 } else {
361                         isl_local_space *ls;
362                         isl_aff *aff;
363                         ls = isl_local_space_from_space(isl_space_copy(dim));
364                         aff = isl_aff_zero_on_domain(ls);
365                         aff = isl_aff_add_constant(aff, tok->u.v);
366                         res = isl_pw_aff_from_aff(aff);
367                 }
368                 isl_token_free(tok);
369         } else if (tok->type == '(') {
370                 isl_token_free(tok);
371                 tok = NULL;
372                 res = accept_affine(s, isl_space_copy(dim), v);
373                 if (!res)
374                         goto error;
375                 if (isl_stream_eat(s, ')'))
376                         goto error;
377         } else if (tok->type == '[' ||
378                     tok->type == ISL_TOKEN_FLOORD ||
379                     tok->type == ISL_TOKEN_CEILD) {
380                 isl_stream_push_token(s, tok);
381                 tok = NULL;
382                 res = accept_div(s, isl_space_copy(dim), v);
383         } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
384                 isl_stream_push_token(s, tok);
385                 tok = NULL;
386                 res = accept_minmax(s, isl_space_copy(dim), v);
387         } else {
388                 isl_stream_error(s, tok, "expecting factor");
389                 goto error;
390         }
391         if (isl_stream_eat_if_available(s, '%') ||
392             isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
393                 isl_space_free(dim);
394                 return affine_mod(s, v, res);
395         }
396         if (isl_stream_eat_if_available(s, '*')) {
397                 isl_int f;
398                 isl_int_init(f);
399                 isl_int_set_si(f, 1);
400                 if (accept_cst_factor(s, &f) < 0) {
401                         isl_int_clear(f);
402                         goto error2;
403                 }
404                 res = isl_pw_aff_scale(res, f);
405                 isl_int_clear(f);
406         }
407         if (isl_stream_eat_if_available(s, '/')) {
408                 isl_int f;
409                 isl_int_init(f);
410                 isl_int_set_si(f, 1);
411                 if (accept_cst_factor(s, &f) < 0) {
412                         isl_int_clear(f);
413                         goto error2;
414                 }
415                 res = isl_pw_aff_scale_down(res, f);
416                 isl_int_clear(f);
417         }
418
419         isl_space_free(dim);
420         return res;
421 error:
422         isl_token_free(tok);
423 error2:
424         isl_pw_aff_free(res);
425         isl_space_free(dim);
426         return NULL;
427 }
428
429 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
430 {
431         isl_aff *aff;
432         isl_space *space;
433
434         space = isl_pw_aff_get_domain_space(pwaff);
435         aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
436         aff = isl_aff_add_constant(aff, v);
437
438         return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
439 }
440
441 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
442         __isl_take isl_space *dim, struct vars *v)
443 {
444         struct isl_token *tok = NULL;
445         isl_local_space *ls;
446         isl_pw_aff *res;
447         int sign = 1;
448
449         ls = isl_local_space_from_space(isl_space_copy(dim));
450         res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
451         if (!res)
452                 goto error;
453
454         for (;;) {
455                 tok = next_token(s);
456                 if (!tok) {
457                         isl_stream_error(s, NULL, "unexpected EOF");
458                         goto error;
459                 }
460                 if (tok->type == '-') {
461                         sign = -sign;
462                         isl_token_free(tok);
463                         continue;
464                 }
465                 if (tok->type == '(' || tok->type == '[' ||
466                     tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
467                     tok->type == ISL_TOKEN_FLOORD ||
468                     tok->type == ISL_TOKEN_CEILD ||
469                     tok->type == ISL_TOKEN_IDENT ||
470                     tok->type == ISL_TOKEN_AFF) {
471                         isl_pw_aff *term;
472                         isl_stream_push_token(s, tok);
473                         tok = NULL;
474                         term = accept_affine_factor(s, isl_space_copy(dim), v);
475                         if (sign < 0)
476                                 res = isl_pw_aff_sub(res, term);
477                         else
478                                 res = isl_pw_aff_add(res, term);
479                         if (!res)
480                                 goto error;
481                         sign = 1;
482                 } else if (tok->type == ISL_TOKEN_VALUE) {
483                         if (sign < 0)
484                                 isl_int_neg(tok->u.v, tok->u.v);
485                         if (isl_stream_eat_if_available(s, '*') ||
486                             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
487                                 isl_pw_aff *term;
488                                 term = accept_affine_factor(s,
489                                                         isl_space_copy(dim), v);
490                                 term = isl_pw_aff_scale(term, tok->u.v);
491                                 res = isl_pw_aff_add(res, term);
492                                 if (!res)
493                                         goto error;
494                         } else {
495                                 res = add_cst(res, tok->u.v);
496                         }
497                         sign = 1;
498                 } else {
499                         isl_stream_error(s, tok, "unexpected isl_token");
500                         isl_stream_push_token(s, tok);
501                         isl_pw_aff_free(res);
502                         isl_space_free(dim);
503                         return NULL;
504                 }
505                 isl_token_free(tok);
506
507                 tok = next_token(s);
508                 if (tok && tok->type == '-') {
509                         sign = -sign;
510                         isl_token_free(tok);
511                 } else if (tok && tok->type == '+') {
512                         /* nothing */
513                         isl_token_free(tok);
514                 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
515                            isl_int_is_neg(tok->u.v)) {
516                         isl_stream_push_token(s, tok);
517                 } else {
518                         if (tok)
519                                 isl_stream_push_token(s, tok);
520                         break;
521                 }
522         }
523
524         isl_space_free(dim);
525         return res;
526 error:
527         isl_space_free(dim);
528         isl_token_free(tok);
529         isl_pw_aff_free(res);
530         return NULL;
531 }
532
533 static int is_comparator(struct isl_token *tok)
534 {
535         if (!tok)
536                 return 0;
537
538         switch (tok->type) {
539         case ISL_TOKEN_LT:
540         case ISL_TOKEN_GT:
541         case ISL_TOKEN_LE:
542         case ISL_TOKEN_GE:
543         case ISL_TOKEN_NE:
544         case '=':
545                 return 1;
546         default:
547                 return 0;
548         }
549 }
550
551 static struct isl_map *read_disjuncts(struct isl_stream *s,
552         struct vars *v, __isl_take isl_map *map, int rational);
553 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
554         __isl_take isl_space *dim, struct vars *v, int rational);
555
556 /* Accept a ternary operator, given the first argument.
557  */
558 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
559         __isl_take isl_map *cond, struct vars *v, int rational)
560 {
561         isl_space *dim;
562         isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
563
564         if (!cond)
565                 return NULL;
566
567         if (isl_stream_eat(s, '?'))
568                 goto error;
569
570         dim = isl_space_wrap(isl_map_get_space(cond));
571         pwaff1 = accept_extended_affine(s, dim, v, rational);
572         if (!pwaff1)
573                 goto error;
574
575         if (isl_stream_eat(s, ':'))
576                 goto error;
577
578         dim = isl_pw_aff_get_domain_space(pwaff1);
579         pwaff2 = accept_extended_affine(s, dim, v, rational);
580         if (!pwaff1)
581                 goto error;
582
583         pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
584         return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
585 error:
586         isl_map_free(cond);
587         isl_pw_aff_free(pwaff1);
588         isl_pw_aff_free(pwaff2);
589         return NULL;
590 }
591
592 /* Accept an affine expression that may involve ternary operators.
593  * We first read an affine expression.
594  * If it is not followed by a comparison operator, we simply return it.
595  * Otherwise, we assume the affine epxression is part of the first
596  * argument of a ternary operator and try to parse that.
597  */
598 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
599         __isl_take isl_space *dim, struct vars *v, int rational)
600 {
601         isl_space *space;
602         isl_map *cond;
603         isl_pw_aff *pwaff;
604         struct isl_token *tok;
605         int line = -1, col = -1;
606         int is_comp;
607
608         tok = isl_stream_next_token(s);
609         if (tok) {
610                 line = tok->line;
611                 col = tok->col;
612                 isl_stream_push_token(s, tok);
613         }
614
615         pwaff = accept_affine(s, dim, v);
616         if (rational)
617                 pwaff = isl_pw_aff_set_rational(pwaff);
618         if (!pwaff)
619                 return NULL;
620
621         tok = isl_stream_next_token(s);
622         if (!tok)
623                 return isl_pw_aff_free(pwaff);
624
625         is_comp = is_comparator(tok);
626         isl_stream_push_token(s, tok);
627         if (!is_comp)
628                 return pwaff;
629
630         tok = isl_token_new(s->ctx, line, col, 0);
631         if (!tok)
632                 return isl_pw_aff_free(pwaff);
633         tok->type = ISL_TOKEN_AFF;
634         tok->u.pwaff = pwaff;
635
636         space = isl_pw_aff_get_domain_space(pwaff);
637         cond = isl_map_universe(isl_space_unwrap(space));
638
639         isl_stream_push_token(s, tok);
640
641         cond = read_disjuncts(s, v, cond, rational);
642
643         return accept_ternary(s, cond, v, rational);
644 }
645
646 static __isl_give isl_map *read_var_def(struct isl_stream *s,
647         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
648         int rational)
649 {
650         isl_pw_aff *def;
651         int pos;
652         isl_map *def_map;
653
654         if (type == isl_dim_param)
655                 pos = isl_map_dim(map, isl_dim_param);
656         else {
657                 pos = isl_map_dim(map, isl_dim_in);
658                 if (type == isl_dim_out)
659                         pos += isl_map_dim(map, isl_dim_out);
660                 type = isl_dim_in;
661         }
662         --pos;
663
664         def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
665                                         v, rational);
666         def_map = isl_map_from_pw_aff(def);
667         def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
668         def_map = isl_set_unwrap(isl_map_domain(def_map));
669
670         map = isl_map_intersect(map, def_map);
671
672         return map;
673 }
674
675 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
676         __isl_take isl_space *dim, struct vars *v)
677 {
678         isl_pw_aff *pwaff;
679         isl_pw_aff_list *list;
680         struct isl_token *tok = NULL;
681
682         pwaff = accept_affine(s, isl_space_copy(dim), v);
683         list = isl_pw_aff_list_from_pw_aff(pwaff);
684         if (!list)
685                 goto error;
686
687         for (;;) {
688                 tok = isl_stream_next_token(s);
689                 if (!tok) {
690                         isl_stream_error(s, NULL, "unexpected EOF");
691                         goto error;
692                 }
693                 if (tok->type != ',') {
694                         isl_stream_push_token(s, tok);
695                         break;
696                 }
697                 isl_token_free(tok);
698
699                 pwaff = accept_affine(s, isl_space_copy(dim), v);
700                 list = isl_pw_aff_list_concat(list,
701                                 isl_pw_aff_list_from_pw_aff(pwaff));
702                 if (!list)
703                         goto error;
704         }
705
706         isl_space_free(dim);
707         return list;
708 error:
709         isl_space_free(dim);
710         isl_pw_aff_list_free(list);
711         return NULL;
712 }
713
714 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
715         struct vars *v, __isl_take isl_map *map, int rational)
716 {
717         struct isl_token *tok;
718
719         while ((tok = isl_stream_next_token(s)) != NULL) {
720                 int p;
721                 int n = v->n;
722
723                 if (tok->type != ISL_TOKEN_IDENT)
724                         break;
725
726                 p = vars_pos(v, tok->u.s, -1);
727                 if (p < 0)
728                         goto error;
729                 if (p < n) {
730                         isl_stream_error(s, tok, "expecting unique identifier");
731                         goto error;
732                 }
733
734                 map = isl_map_add_dims(map, isl_dim_out, 1);
735
736                 isl_token_free(tok);
737                 tok = isl_stream_next_token(s);
738                 if (tok && tok->type == '=') {
739                         isl_token_free(tok);
740                         map = read_var_def(s, map, isl_dim_out, v, rational);
741                         tok = isl_stream_next_token(s);
742                 }
743
744                 if (!tok || tok->type != ',')
745                         break;
746
747                 isl_token_free(tok);
748         }
749         if (tok)
750                 isl_stream_push_token(s, tok);
751
752         return map;
753 error:
754         isl_token_free(tok);
755         isl_map_free(map);
756         return NULL;
757 }
758
759 static int next_is_tuple(struct isl_stream *s)
760 {
761         struct isl_token *tok;
762         int is_tuple;
763
764         tok = isl_stream_next_token(s);
765         if (!tok)
766                 return 0;
767         if (tok->type == '[') {
768                 isl_stream_push_token(s, tok);
769                 return 1;
770         }
771         if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
772                 isl_stream_push_token(s, tok);
773                 return 0;
774         }
775
776         is_tuple = isl_stream_next_token_is(s, '[');
777
778         isl_stream_push_token(s, tok);
779
780         return is_tuple;
781 }
782
783 /* Allocate an initial tuple with zero dimensions and an anonymous,
784  * unstructured space.
785  * A tuple is represented as an isl_multi_pw_aff.
786  * The range space is the space of the tuple.
787  * The domain space is an anonymous space
788  * with a dimension for each variable in the set of variables in "v".
789  * If a given dimension is not defined in terms of earlier dimensions in
790  * the input, then the corresponding isl_pw_aff is set equal to one time
791  * the variable corresponding to the dimension being defined.
792  */
793 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
794 {
795         return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
796 }
797
798 /* Is "pa" an expression in term of earlier dimensions?
799  * The alternative is that the dimension is defined to be equal to itself,
800  * meaning that it has a universe domain and an expression that depends
801  * on itself.  "i" is the position of the expression in a sequence
802  * of "n" expressions.  The final dimensions of "pa" correspond to
803  * these "n" expressions.
804  */
805 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
806 {
807         isl_aff *aff;
808
809         if (!pa)
810                 return -1;
811         if (pa->n != 1)
812                 return 1;
813         if (!isl_set_plain_is_universe(pa->p[0].set))
814                 return 1;
815
816         aff = pa->p[0].aff;
817         if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
818                 return 1;
819         return 0;
820 }
821
822 /* Does the tuple contain any dimensions that are defined
823  * in terms of earlier dimensions?
824  */
825 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
826 {
827         int i, n;
828         int has_expr = 0;
829         isl_pw_aff *pa;
830
831         if (!tuple)
832                 return -1;
833         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
834         for (i = 0; i < n; ++i) {
835                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
836                 has_expr = pw_aff_is_expr(pa, i, n);
837                 isl_pw_aff_free(pa);
838                 if (has_expr < 0 || has_expr)
839                         break;
840         }
841
842         return has_expr;
843 }
844
845 /* Add a dimension to the given tuple.
846  * The dimension is initially undefined, so it is encoded
847  * as one times itself.
848  */
849 static __isl_give isl_multi_pw_aff *tuple_add_dim(
850         __isl_take isl_multi_pw_aff *tuple, struct vars *v)
851 {
852         isl_space *space;
853         isl_aff *aff;
854         isl_pw_aff *pa;
855
856         tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
857         space = isl_multi_pw_aff_get_domain_space(tuple);
858         aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
859         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
860         pa = isl_pw_aff_from_aff(aff);
861         tuple = isl_multi_pw_aff_flat_range_product(tuple,
862                                             isl_multi_pw_aff_from_pw_aff(pa));
863
864         return tuple;
865 }
866
867 /* Set the name of dimension "pos" in "tuple" to "name".
868  * During printing, we add primes if the same name appears more than once
869  * to distinguish the occurrences.  Here, we remove those primes from "name"
870  * before setting the name of the dimension.
871  */
872 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
873         __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
874 {
875         char *prime;
876
877         if (!name)
878                 return tuple;
879
880         prime = strchr(name, '\'');
881         if (prime)
882                 *prime = '\0';
883         tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
884         if (prime)
885                 *prime = '\'';
886
887         return tuple;
888 }
889
890 /* Read an affine expression from "s" and replace the definition
891  * of dimension "pos" in "tuple" by this expression.
892  *
893  * accept_extended_affine requires a wrapped space as input.
894  * The domain space of "tuple", on the other hand is an anonymous space,
895  * so we have to adjust the space of the isl_pw_aff before adding it
896  * to "tuple".
897  */
898 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
899         __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
900         int rational)
901 {
902         isl_space *space;
903         isl_pw_aff *def;
904
905         space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
906         def = accept_extended_affine(s, space, v, rational);
907         space = isl_space_set_alloc(s->ctx, 0, v->n);
908         def = isl_pw_aff_reset_domain_space(def, space);
909         tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
910
911         return tuple;
912 }
913
914 /* Read a list of variables and/or affine expressions and return the list
915  * as an isl_multi_pw_aff.
916  * The elements in the list are separated by either "," or "][".
917  * If "comma" is set then only "," is allowed.
918  */
919 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
920         struct vars *v, int rational, int comma)
921 {
922         int i = 0;
923         struct isl_token *tok;
924         isl_multi_pw_aff *res;
925
926         res = tuple_alloc(v);
927
928         if (isl_stream_next_token_is(s, ']'))
929                 return res;
930
931         while ((tok = next_token(s)) != NULL) {
932                 int new_name = 0;
933
934                 res = tuple_add_dim(res, v);
935
936                 if (tok->type == ISL_TOKEN_IDENT) {
937                         int n = v->n;
938                         int p = vars_pos(v, tok->u.s, -1);
939                         if (p < 0)
940                                 goto error;
941                         new_name = p >= n;
942                 }
943
944                 if (new_name) {
945                         res = tuple_set_dim_name(res, i, v->v->name);
946                         isl_token_free(tok);
947                         if (isl_stream_eat_if_available(s, '='))
948                                 res = read_tuple_var_def(s, res, i, v,
949                                                         rational);
950                 } else {
951                         isl_stream_push_token(s, tok);
952                         tok = NULL;
953                         if (vars_add_anon(v) < 0)
954                                 goto error;
955                         res = read_tuple_var_def(s, res, i, v, rational);
956                 }
957
958                 tok = isl_stream_next_token(s);
959                 if (!comma && tok && tok->type == ']' &&
960                     isl_stream_next_token_is(s, '[')) {
961                         isl_token_free(tok);
962                         tok = isl_stream_next_token(s);
963                 } else if (!tok || tok->type != ',')
964                         break;
965
966                 isl_token_free(tok);
967                 i++;
968         }
969         if (tok)
970                 isl_stream_push_token(s, tok);
971
972         return res;
973 error:
974         isl_token_free(tok);
975         return isl_multi_pw_aff_free(res);
976 }
977
978 /* Read a tuple and represent it as an isl_multi_pw_aff.  See tuple_alloc.
979  */
980 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
981         struct vars *v, int rational, int comma)
982 {
983         struct isl_token *tok;
984         char *name = NULL;
985         isl_multi_pw_aff *res = NULL;
986
987         tok = isl_stream_next_token(s);
988         if (!tok)
989                 goto error;
990         if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
991                 name = strdup(tok->u.s);
992                 isl_token_free(tok);
993                 if (!name)
994                         goto error;
995         } else
996                 isl_stream_push_token(s, tok);
997         if (isl_stream_eat(s, '['))
998                 goto error;
999         if (next_is_tuple(s)) {
1000                 isl_multi_pw_aff *out;
1001                 int n;
1002                 res = read_tuple(s, v, rational, comma);
1003                 if (isl_stream_eat(s, ISL_TOKEN_TO))
1004                         goto error;
1005                 out = read_tuple(s, v, rational, comma);
1006                 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1007                 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1008                 res = isl_multi_pw_aff_range_product(res, out);
1009         } else
1010                 res = read_tuple_var_list(s, v, rational, comma);
1011         if (isl_stream_eat(s, ']'))
1012                 goto error;
1013
1014         if (name) {
1015                 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1016                 free(name);
1017         }
1018
1019         return res;
1020 error:
1021         free(name);
1022         return isl_multi_pw_aff_free(res);
1023 }
1024
1025 /* Read a tuple from "s" and add it to "map".
1026  * The tuple is initially represented as an isl_multi_pw_aff.
1027  * We first create the appropriate space in "map" based on the range
1028  * space of this isl_multi_pw_aff.  Then, we add equalities based
1029  * on the affine expressions.  These live in an anonymous space,
1030  * however, so we first need to reset the space to that of "map".
1031  */
1032 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1033         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1034         int rational, int comma)
1035 {
1036         int i, n;
1037         isl_multi_pw_aff *tuple;
1038         isl_space *space = NULL;
1039
1040         tuple = read_tuple(s, v, rational, comma);
1041         if (!tuple)
1042                 goto error;
1043
1044         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1045         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1046         if (!space)
1047                 goto error;
1048
1049         if (type == isl_dim_param) {
1050                 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1051                     isl_space_is_wrapping(space)) {
1052                         isl_die(s->ctx, isl_error_invalid,
1053                                 "parameter tuples cannot be named or nested",
1054                                 goto error);
1055                 }
1056                 map = isl_map_add_dims(map, type, n);
1057                 for (i = 0; i < n; ++i) {
1058                         isl_id *id;
1059                         if (!isl_space_has_dim_name(space, isl_dim_set, i))
1060                                 isl_die(s->ctx, isl_error_invalid,
1061                                         "parameters must be named",
1062                                         goto error);
1063                         id = isl_space_get_dim_id(space, isl_dim_set, i);
1064                         map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1065                 }
1066         } else if (type == isl_dim_in) {
1067                 isl_set *set;
1068
1069                 set = isl_set_universe(isl_space_copy(space));
1070                 if (rational)
1071                         set = isl_set_set_rational(set);
1072                 set = isl_set_intersect_params(set, isl_map_params(map));
1073                 map = isl_map_from_domain(set);
1074         } else {
1075                 isl_set *set;
1076
1077                 set = isl_set_universe(isl_space_copy(space));
1078                 if (rational)
1079                         set = isl_set_set_rational(set);
1080                 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1081         }
1082
1083         for (i = 0; i < n; ++i) {
1084                 isl_pw_aff *pa;
1085                 isl_space *space;
1086                 isl_aff *aff;
1087                 isl_set *set;
1088                 isl_map *map_i;
1089
1090                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1091                 space = isl_pw_aff_get_domain_space(pa);
1092                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1093                 aff = isl_aff_add_coefficient_si(aff,
1094                                                 isl_dim_in, v->n - n + i, -1);
1095                 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1096                 if (rational)
1097                         pa = isl_pw_aff_set_rational(pa);
1098                 set = isl_pw_aff_zero_set(pa);
1099                 map_i = isl_map_from_range(set);
1100                 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1101                 map = isl_map_intersect(map, map_i);
1102         }
1103
1104         isl_space_free(space);
1105         isl_multi_pw_aff_free(tuple);
1106         return map;
1107 error:
1108         isl_space_free(space);
1109         isl_multi_pw_aff_free(tuple);
1110         isl_map_free(map);
1111         return NULL;
1112 }
1113
1114 static __isl_give isl_set *construct_constraints(
1115         __isl_take isl_set *set, int type,
1116         __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1117         int rational)
1118 {
1119         isl_set *cond;
1120
1121         left = isl_pw_aff_list_copy(left);
1122         right = isl_pw_aff_list_copy(right);
1123         if (rational) {
1124                 left = isl_pw_aff_list_set_rational(left);
1125                 right = isl_pw_aff_list_set_rational(right);
1126         }
1127         if (type == ISL_TOKEN_LE)
1128                 cond = isl_pw_aff_list_le_set(left, right);
1129         else if (type == ISL_TOKEN_GE)
1130                 cond = isl_pw_aff_list_ge_set(left, right);
1131         else if (type == ISL_TOKEN_LT)
1132                 cond = isl_pw_aff_list_lt_set(left, right);
1133         else if (type == ISL_TOKEN_GT)
1134                 cond = isl_pw_aff_list_gt_set(left, right);
1135         else if (type == ISL_TOKEN_NE)
1136                 cond = isl_pw_aff_list_ne_set(left, right);
1137         else
1138                 cond = isl_pw_aff_list_eq_set(left, right);
1139
1140         return isl_set_intersect(set, cond);
1141 }
1142
1143 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1144         struct vars *v, __isl_take isl_map *map, int rational)
1145 {
1146         struct isl_token *tok = NULL;
1147         isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1148         isl_set *set;
1149
1150         set = isl_map_wrap(map);
1151         list1 = accept_affine_list(s, isl_set_get_space(set), v);
1152         if (!list1)
1153                 goto error;
1154         tok = isl_stream_next_token(s);
1155         if (!is_comparator(tok)) {
1156                 isl_stream_error(s, tok, "missing operator");
1157                 if (tok)
1158                         isl_stream_push_token(s, tok);
1159                 tok = NULL;
1160                 goto error;
1161         }
1162         for (;;) {
1163                 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1164                 if (!list2)
1165                         goto error;
1166
1167                 set = construct_constraints(set, tok->type, list1, list2,
1168                                                 rational);
1169                 isl_token_free(tok);
1170                 isl_pw_aff_list_free(list1);
1171                 list1 = list2;
1172
1173                 tok = isl_stream_next_token(s);
1174                 if (!is_comparator(tok)) {
1175                         if (tok)
1176                                 isl_stream_push_token(s, tok);
1177                         break;
1178                 }
1179         }
1180         isl_pw_aff_list_free(list1);
1181
1182         return isl_set_unwrap(set);
1183 error:
1184         if (tok)
1185                 isl_token_free(tok);
1186         isl_pw_aff_list_free(list1);
1187         isl_pw_aff_list_free(list2);
1188         isl_set_free(set);
1189         return NULL;
1190 }
1191
1192 static __isl_give isl_map *read_exists(struct isl_stream *s,
1193         struct vars *v, __isl_take isl_map *map, int rational)
1194 {
1195         int n = v->n;
1196         int seen_paren = isl_stream_eat_if_available(s, '(');
1197
1198         map = isl_map_from_domain(isl_map_wrap(map));
1199         map = read_defined_var_list(s, v, map, rational);
1200
1201         if (isl_stream_eat(s, ':'))
1202                 goto error;
1203
1204         map = read_disjuncts(s, v, map, rational);
1205         map = isl_set_unwrap(isl_map_domain(map));
1206
1207         vars_drop(v, v->n - n);
1208         if (seen_paren && isl_stream_eat(s, ')'))
1209                 goto error;
1210
1211         return map;
1212 error:
1213         isl_map_free(map);
1214         return NULL;
1215 }
1216
1217 /* Parse an expression between parentheses and push the result
1218  * back on the stream.
1219  *
1220  * The parsed expression may be either an affine expression
1221  * or a condition.  The first type is pushed onto the stream
1222  * as an isl_pw_aff, while the second is pushed as an isl_map.
1223  *
1224  * If the initial token indicates the start of a condition,
1225  * we parse it as such.
1226  * Otherwise, we first parse an affine expression and push
1227  * that onto the stream.  If the affine expression covers the
1228  * entire expression between parentheses, we return.
1229  * Otherwise, we assume that the affine expression is the
1230  * start of a condition and continue parsing.
1231  */
1232 static int resolve_paren_expr(struct isl_stream *s,
1233         struct vars *v, __isl_take isl_map *map, int rational)
1234 {
1235         struct isl_token *tok, *tok2;
1236         int line, col;
1237         isl_pw_aff *pwaff;
1238
1239         tok = isl_stream_next_token(s);
1240         if (!tok || tok->type != '(')
1241                 goto error;
1242
1243         if (isl_stream_next_token_is(s, '('))
1244                 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1245                         goto error;
1246
1247         if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1248             isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1249             isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1250             isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1251             isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1252                 map = read_disjuncts(s, v, map, rational);
1253                 if (isl_stream_eat(s, ')'))
1254                         goto error;
1255                 tok->type = ISL_TOKEN_MAP;
1256                 tok->u.map = map;
1257                 isl_stream_push_token(s, tok);
1258                 return 0;
1259         }
1260
1261         tok2 = isl_stream_next_token(s);
1262         if (!tok2)
1263                 goto error;
1264         line = tok2->line;
1265         col = tok2->col;
1266         isl_stream_push_token(s, tok2);
1267
1268         pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1269         if (!pwaff)
1270                 goto error;
1271
1272         tok2 = isl_token_new(s->ctx, line, col, 0);
1273         if (!tok2)
1274                 goto error2;
1275         tok2->type = ISL_TOKEN_AFF;
1276         tok2->u.pwaff = pwaff;
1277
1278         if (isl_stream_eat_if_available(s, ')')) {
1279                 isl_stream_push_token(s, tok2);
1280                 isl_token_free(tok);
1281                 isl_map_free(map);
1282                 return 0;
1283         }
1284
1285         isl_stream_push_token(s, tok2);
1286
1287         map = read_disjuncts(s, v, map, rational);
1288         if (isl_stream_eat(s, ')'))
1289                 goto error;
1290
1291         tok->type = ISL_TOKEN_MAP;
1292         tok->u.map = map;
1293         isl_stream_push_token(s, tok);
1294
1295         return 0;
1296 error2:
1297         isl_pw_aff_free(pwaff);
1298 error:
1299         isl_token_free(tok);
1300         isl_map_free(map);
1301         return -1;
1302 }
1303
1304 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1305         struct vars *v, __isl_take isl_map *map, int rational)
1306 {
1307         if (isl_stream_next_token_is(s, '('))
1308                 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1309                         goto error;
1310
1311         if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1312                 struct isl_token *tok;
1313                 tok = isl_stream_next_token(s);
1314                 if (!tok)
1315                         goto error;
1316                 isl_map_free(map);
1317                 map = isl_map_copy(tok->u.map);
1318                 isl_token_free(tok);
1319                 return map;
1320         }
1321
1322         if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1323                 return read_exists(s, v, map, rational);
1324
1325         if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1326                 return map;
1327
1328         if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1329                 isl_space *dim = isl_map_get_space(map);
1330                 isl_map_free(map);
1331                 return isl_map_empty(dim);
1332         }
1333                 
1334         return add_constraint(s, v, map, rational);
1335 error:
1336         isl_map_free(map);
1337         return NULL;
1338 }
1339
1340 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1341         struct vars *v, __isl_take isl_map *map, int rational)
1342 {
1343         isl_map *res;
1344         int negate;
1345
1346         negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1347         res = read_conjunct(s, v, isl_map_copy(map), rational);
1348         if (negate)
1349                 res = isl_map_subtract(isl_map_copy(map), res);
1350
1351         while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1352                 isl_map *res_i;
1353
1354                 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1355                 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1356                 if (negate)
1357                         res = isl_map_subtract(res, res_i);
1358                 else
1359                         res = isl_map_intersect(res, res_i);
1360         }
1361
1362         isl_map_free(map);
1363         return res;
1364 }
1365
1366 static struct isl_map *read_disjuncts(struct isl_stream *s,
1367         struct vars *v, __isl_take isl_map *map, int rational)
1368 {
1369         isl_map *res;
1370
1371         if (isl_stream_next_token_is(s, '}')) {
1372                 isl_space *dim = isl_map_get_space(map);
1373                 isl_map_free(map);
1374                 return isl_map_universe(dim);
1375         }
1376
1377         res = read_conjuncts(s, v, isl_map_copy(map), rational);
1378         while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1379                 isl_map *res_i;
1380
1381                 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1382                 res = isl_map_union(res, res_i);
1383         }
1384
1385         isl_map_free(map);
1386         return res;
1387 }
1388
1389 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1390 {
1391         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1392                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1393                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1394         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1395
1396         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1397                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1398         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1399
1400         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1401                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1402                            isl_basic_map_dim(bmap, isl_dim_in) +
1403                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1404         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1405
1406         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1407                 return 1 + pos;
1408
1409         return 0;
1410 }
1411
1412 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1413         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1414 {
1415         int j;
1416         struct isl_token *tok;
1417         int type;
1418         int k;
1419         isl_int *c;
1420         unsigned nparam;
1421         unsigned dim;
1422
1423         if (!bmap)
1424                 return NULL;
1425
1426         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1427         dim = isl_basic_map_dim(bmap, isl_dim_out);
1428
1429         tok = isl_stream_next_token(s);
1430         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1431                 isl_stream_error(s, tok, "expecting coefficient");
1432                 if (tok)
1433                         isl_stream_push_token(s, tok);
1434                 goto error;
1435         }
1436         if (!tok->on_new_line) {
1437                 isl_stream_error(s, tok, "coefficient should appear on new line");
1438                 isl_stream_push_token(s, tok);
1439                 goto error;
1440         }
1441
1442         type = isl_int_get_si(tok->u.v);
1443         isl_token_free(tok);
1444
1445         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1446         if (type == 0) {
1447                 k = isl_basic_map_alloc_equality(bmap);
1448                 c = bmap->eq[k];
1449         } else {
1450                 k = isl_basic_map_alloc_inequality(bmap);
1451                 c = bmap->ineq[k];
1452         }
1453         if (k < 0)
1454                 goto error;
1455
1456         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1457                 int pos;
1458                 tok = isl_stream_next_token(s);
1459                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1460                         isl_stream_error(s, tok, "expecting coefficient");
1461                         if (tok)
1462                                 isl_stream_push_token(s, tok);
1463                         goto error;
1464                 }
1465                 if (tok->on_new_line) {
1466                         isl_stream_error(s, tok,
1467                                 "coefficient should not appear on new line");
1468                         isl_stream_push_token(s, tok);
1469                         goto error;
1470                 }
1471                 pos = polylib_pos_to_isl_pos(bmap, j);
1472                 isl_int_set(c[pos], tok->u.v);
1473                 isl_token_free(tok);
1474         }
1475
1476         return bmap;
1477 error:
1478         isl_basic_map_free(bmap);
1479         return NULL;
1480 }
1481
1482 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1483 {
1484         int i;
1485         struct isl_token *tok;
1486         struct isl_token *tok2;
1487         int n_row, n_col;
1488         int on_new_line;
1489         unsigned in = 0, out, local = 0;
1490         struct isl_basic_map *bmap = NULL;
1491         int nparam = 0;
1492
1493         tok = isl_stream_next_token(s);
1494         if (!tok) {
1495                 isl_stream_error(s, NULL, "unexpected EOF");
1496                 return NULL;
1497         }
1498         tok2 = isl_stream_next_token(s);
1499         if (!tok2) {
1500                 isl_token_free(tok);
1501                 isl_stream_error(s, NULL, "unexpected EOF");
1502                 return NULL;
1503         }
1504         if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1505                 isl_stream_push_token(s, tok2);
1506                 isl_stream_push_token(s, tok);
1507                 isl_stream_error(s, NULL,
1508                                  "expecting constraint matrix dimensions");
1509                 return NULL;
1510         }
1511         n_row = isl_int_get_si(tok->u.v);
1512         n_col = isl_int_get_si(tok2->u.v);
1513         on_new_line = tok2->on_new_line;
1514         isl_token_free(tok2);
1515         isl_token_free(tok);
1516         isl_assert(s->ctx, !on_new_line, return NULL);
1517         isl_assert(s->ctx, n_row >= 0, return NULL);
1518         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1519         tok = isl_stream_next_token_on_same_line(s);
1520         if (tok) {
1521                 if (tok->type != ISL_TOKEN_VALUE) {
1522                         isl_stream_error(s, tok,
1523                                     "expecting number of output dimensions");
1524                         isl_stream_push_token(s, tok);
1525                         goto error;
1526                 }
1527                 out = isl_int_get_si(tok->u.v);
1528                 isl_token_free(tok);
1529
1530                 tok = isl_stream_next_token_on_same_line(s);
1531                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1532                         isl_stream_error(s, tok,
1533                                     "expecting number of input dimensions");
1534                         if (tok)
1535                                 isl_stream_push_token(s, tok);
1536                         goto error;
1537                 }
1538                 in = isl_int_get_si(tok->u.v);
1539                 isl_token_free(tok);
1540
1541                 tok = isl_stream_next_token_on_same_line(s);
1542                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1543                         isl_stream_error(s, tok,
1544                                     "expecting number of existentials");
1545                         if (tok)
1546                                 isl_stream_push_token(s, tok);
1547                         goto error;
1548                 }
1549                 local = isl_int_get_si(tok->u.v);
1550                 isl_token_free(tok);
1551
1552                 tok = isl_stream_next_token_on_same_line(s);
1553                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1554                         isl_stream_error(s, tok,
1555                                     "expecting number of parameters");
1556                         if (tok)
1557                                 isl_stream_push_token(s, tok);
1558                         goto error;
1559                 }
1560                 nparam = isl_int_get_si(tok->u.v);
1561                 isl_token_free(tok);
1562                 if (n_col != 1 + out + in + local + nparam + 1) {
1563                         isl_stream_error(s, NULL,
1564                                     "dimensions don't match");
1565                         goto error;
1566                 }
1567         } else
1568                 out = n_col - 2 - nparam;
1569         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1570         if (!bmap)
1571                 return NULL;
1572
1573         for (i = 0; i < local; ++i) {
1574                 int k = isl_basic_map_alloc_div(bmap);
1575                 if (k < 0)
1576                         goto error;
1577                 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1578         }
1579
1580         for (i = 0; i < n_row; ++i)
1581                 bmap = basic_map_read_polylib_constraint(s, bmap);
1582
1583         tok = isl_stream_next_token_on_same_line(s);
1584         if (tok) {
1585                 isl_stream_error(s, tok, "unexpected extra token on line");
1586                 isl_stream_push_token(s, tok);
1587                 goto error;
1588         }
1589
1590         bmap = isl_basic_map_simplify(bmap);
1591         bmap = isl_basic_map_finalize(bmap);
1592         return bmap;
1593 error:
1594         isl_basic_map_free(bmap);
1595         return NULL;
1596 }
1597
1598 static struct isl_map *map_read_polylib(struct isl_stream *s)
1599 {
1600         struct isl_token *tok;
1601         struct isl_token *tok2;
1602         int i, n;
1603         struct isl_map *map;
1604
1605         tok = isl_stream_next_token(s);
1606         if (!tok) {
1607                 isl_stream_error(s, NULL, "unexpected EOF");
1608                 return NULL;
1609         }
1610         tok2 = isl_stream_next_token_on_same_line(s);
1611         if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1612                 isl_stream_push_token(s, tok2);
1613                 isl_stream_push_token(s, tok);
1614                 return isl_map_from_basic_map(basic_map_read_polylib(s));
1615         }
1616         if (tok2) {
1617                 isl_stream_error(s, tok2, "unexpected token");
1618                 isl_stream_push_token(s, tok2);
1619                 isl_stream_push_token(s, tok);
1620                 return NULL;
1621         }
1622         n = isl_int_get_si(tok->u.v);
1623         isl_token_free(tok);
1624
1625         isl_assert(s->ctx, n >= 1, return NULL);
1626
1627         map = isl_map_from_basic_map(basic_map_read_polylib(s));
1628
1629         for (i = 1; map && i < n; ++i)
1630                 map = isl_map_union(map,
1631                         isl_map_from_basic_map(basic_map_read_polylib(s)));
1632
1633         return map;
1634 }
1635
1636 static int optional_power(struct isl_stream *s)
1637 {
1638         int pow;
1639         struct isl_token *tok;
1640
1641         tok = isl_stream_next_token(s);
1642         if (!tok)
1643                 return 1;
1644         if (tok->type != '^') {
1645                 isl_stream_push_token(s, tok);
1646                 return 1;
1647         }
1648         isl_token_free(tok);
1649         tok = isl_stream_next_token(s);
1650         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1651                 isl_stream_error(s, tok, "expecting exponent");
1652                 if (tok)
1653                         isl_stream_push_token(s, tok);
1654                 return 1;
1655         }
1656         pow = isl_int_get_si(tok->u.v);
1657         isl_token_free(tok);
1658         return pow;
1659 }
1660
1661 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1662         __isl_keep isl_map *map, struct vars *v);
1663
1664 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1665         __isl_keep isl_map *map, struct vars *v)
1666 {
1667         isl_pw_qpolynomial *pwqp;
1668         struct isl_token *tok;
1669
1670         tok = next_token(s);
1671         if (!tok) {
1672                 isl_stream_error(s, NULL, "unexpected EOF");
1673                 return NULL;
1674         }
1675         if (tok->type == '(') {
1676                 int pow;
1677
1678                 isl_token_free(tok);
1679                 pwqp = read_term(s, map, v);
1680                 if (!pwqp)
1681                         return NULL;
1682                 if (isl_stream_eat(s, ')'))
1683                         goto error;
1684                 pow = optional_power(s);
1685                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1686         } else if (tok->type == ISL_TOKEN_VALUE) {
1687                 struct isl_token *tok2;
1688                 tok2 = isl_stream_next_token(s);
1689                 isl_qpolynomial *qp;
1690                 if (tok2 && tok2->type == '/') {
1691                         isl_token_free(tok2);
1692                         tok2 = next_token(s);
1693                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1694                                 isl_stream_error(s, tok2, "expected denominator");
1695                                 isl_token_free(tok);
1696                                 isl_token_free(tok2);
1697                                 return NULL;
1698                         }
1699                         qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1700                                                     tok->u.v, tok2->u.v);
1701                         isl_token_free(tok2);
1702                 } else {
1703                         isl_stream_push_token(s, tok2);
1704                         qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1705                                                 tok->u.v);
1706                 }
1707                 isl_token_free(tok);
1708                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1709         } else if (tok->type == ISL_TOKEN_INFTY) {
1710                 isl_qpolynomial *qp;
1711                 isl_token_free(tok);
1712                 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1713                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1714         } else if (tok->type == ISL_TOKEN_NAN) {
1715                 isl_qpolynomial *qp;
1716                 isl_token_free(tok);
1717                 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1718                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1719         } else if (tok->type == ISL_TOKEN_IDENT) {
1720                 int n = v->n;
1721                 int pos = vars_pos(v, tok->u.s, -1);
1722                 int pow;
1723                 isl_qpolynomial *qp;
1724                 if (pos < 0) {
1725                         isl_token_free(tok);
1726                         return NULL;
1727                 }
1728                 if (pos >= n) {
1729                         vars_drop(v, v->n - n);
1730                         isl_stream_error(s, tok, "unknown identifier");
1731                         isl_token_free(tok);
1732                         return NULL;
1733                 }
1734                 isl_token_free(tok);
1735                 pow = optional_power(s);
1736                 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1737                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1738         } else if (tok->type == '[') {
1739                 isl_pw_aff *pwaff;
1740                 int pow;
1741
1742                 isl_stream_push_token(s, tok);
1743                 pwaff = accept_div(s, isl_map_get_space(map), v);
1744                 pow = optional_power(s);
1745                 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1746                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1747         } else if (tok->type == '-') {
1748                 isl_token_free(tok);
1749                 pwqp = read_factor(s, map, v);
1750                 pwqp = isl_pw_qpolynomial_neg(pwqp);
1751         } else {
1752                 isl_stream_error(s, tok, "unexpected isl_token");
1753                 isl_stream_push_token(s, tok);
1754                 return NULL;
1755         }
1756
1757         if (isl_stream_eat_if_available(s, '*') ||
1758             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1759                 isl_pw_qpolynomial *pwqp2;
1760
1761                 pwqp2 = read_factor(s, map, v);
1762                 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1763         }
1764
1765         return pwqp;
1766 error:
1767         isl_pw_qpolynomial_free(pwqp);
1768         return NULL;
1769 }
1770
1771 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1772         __isl_keep isl_map *map, struct vars *v)
1773 {
1774         struct isl_token *tok;
1775         isl_pw_qpolynomial *pwqp;
1776
1777         pwqp = read_factor(s, map, v);
1778
1779         for (;;) {
1780                 tok = next_token(s);
1781                 if (!tok)
1782                         return pwqp;
1783
1784                 if (tok->type == '+') {
1785                         isl_pw_qpolynomial *pwqp2;
1786
1787                         isl_token_free(tok);
1788                         pwqp2 = read_factor(s, map, v);
1789                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1790                 } else if (tok->type == '-') {
1791                         isl_pw_qpolynomial *pwqp2;
1792
1793                         isl_token_free(tok);
1794                         pwqp2 = read_factor(s, map, v);
1795                         pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1796                 } else if (tok->type == ISL_TOKEN_VALUE &&
1797                             isl_int_is_neg(tok->u.v)) {
1798                         isl_pw_qpolynomial *pwqp2;
1799
1800                         isl_stream_push_token(s, tok);
1801                         pwqp2 = read_factor(s, map, v);
1802                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1803                 } else {
1804                         isl_stream_push_token(s, tok);
1805                         break;
1806                 }
1807         }
1808
1809         return pwqp;
1810 }
1811
1812 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1813         __isl_take isl_map *map, struct vars *v, int rational)
1814 {
1815         struct isl_token *tok;
1816
1817         tok = isl_stream_next_token(s);
1818         if (!tok) {
1819                 isl_stream_error(s, NULL, "unexpected EOF");
1820                 goto error;
1821         }
1822         if (tok->type == ':' ||
1823             (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1824                 isl_token_free(tok);
1825                 map = read_disjuncts(s, v, map, rational);
1826         } else
1827                 isl_stream_push_token(s, tok);
1828
1829         return map;
1830 error:
1831         isl_map_free(map);
1832         return NULL;
1833 }
1834
1835 static struct isl_obj obj_read_poly(struct isl_stream *s,
1836         __isl_take isl_map *map, struct vars *v, int n)
1837 {
1838         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1839         isl_pw_qpolynomial *pwqp;
1840         struct isl_set *set;
1841
1842         pwqp = read_term(s, map, v);
1843         map = read_optional_disjuncts(s, map, v, 0);
1844         set = isl_map_range(map);
1845
1846         pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1847
1848         vars_drop(v, v->n - n);
1849
1850         obj.v = pwqp;
1851         return obj;
1852 }
1853
1854 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1855         __isl_take isl_set *set, struct vars *v, int n)
1856 {
1857         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1858         isl_pw_qpolynomial *pwqp;
1859         isl_pw_qpolynomial_fold *pwf = NULL;
1860
1861         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1862                 return obj_read_poly(s, set, v, n);
1863
1864         if (isl_stream_eat(s, '('))
1865                 goto error;
1866
1867         pwqp = read_term(s, set, v);
1868         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1869
1870         while (isl_stream_eat_if_available(s, ',')) {
1871                 isl_pw_qpolynomial_fold *pwf_i;
1872                 pwqp = read_term(s, set, v);
1873                 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1874                                                                         pwqp);
1875                 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1876         }
1877
1878         if (isl_stream_eat(s, ')'))
1879                 goto error;
1880
1881         set = read_optional_disjuncts(s, set, v, 0);
1882         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1883
1884         vars_drop(v, v->n - n);
1885
1886         obj.v = pwf;
1887         return obj;
1888 error:
1889         isl_set_free(set);
1890         isl_pw_qpolynomial_fold_free(pwf);
1891         obj.type = isl_obj_none;
1892         return obj;
1893 }
1894
1895 static int is_rational(struct isl_stream *s)
1896 {
1897         struct isl_token *tok;
1898
1899         tok = isl_stream_next_token(s);
1900         if (!tok)
1901                 return 0;
1902         if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1903                 isl_token_free(tok);
1904                 isl_stream_eat(s, ':');
1905                 return 1;
1906         }
1907
1908         isl_stream_push_token(s, tok);
1909
1910         return 0;
1911 }
1912
1913 static struct isl_obj obj_read_body(struct isl_stream *s,
1914         __isl_take isl_map *map, struct vars *v)
1915 {
1916         struct isl_token *tok;
1917         struct isl_obj obj = { isl_obj_set, NULL };
1918         int n = v->n;
1919         int rational;
1920
1921         rational = is_rational(s);
1922         if (rational)
1923                 map = isl_map_set_rational(map);
1924
1925         if (isl_stream_next_token_is(s, ':')) {
1926                 obj.type = isl_obj_set;
1927                 obj.v = read_optional_disjuncts(s, map, v, rational);
1928                 return obj;
1929         }
1930
1931         if (!next_is_tuple(s))
1932                 return obj_read_poly_or_fold(s, map, v, n);
1933
1934         map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
1935         if (!map)
1936                 goto error;
1937         tok = isl_stream_next_token(s);
1938         if (!tok)
1939                 goto error;
1940         if (tok->type == ISL_TOKEN_TO) {
1941                 obj.type = isl_obj_map;
1942                 isl_token_free(tok);
1943                 if (!next_is_tuple(s)) {
1944                         isl_set *set = isl_map_domain(map);
1945                         return obj_read_poly_or_fold(s, set, v, n);
1946                 }
1947                 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
1948                 if (!map)
1949                         goto error;
1950         } else {
1951                 map = isl_map_domain(map);
1952                 isl_stream_push_token(s, tok);
1953         }
1954
1955         map = read_optional_disjuncts(s, map, v, rational);
1956
1957         vars_drop(v, v->n - n);
1958
1959         obj.v = map;
1960         return obj;
1961 error:
1962         isl_map_free(map);
1963         obj.type = isl_obj_none;
1964         return obj;
1965 }
1966
1967 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1968 {
1969         if (obj.type == isl_obj_map) {
1970                 obj.v = isl_union_map_from_map(obj.v);
1971                 obj.type = isl_obj_union_map;
1972         } else if (obj.type == isl_obj_set) {
1973                 obj.v = isl_union_set_from_set(obj.v);
1974                 obj.type = isl_obj_union_set;
1975         } else if (obj.type == isl_obj_pw_qpolynomial) {
1976                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1977                 obj.type = isl_obj_union_pw_qpolynomial;
1978         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1979                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1980                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1981         } else
1982                 isl_assert(ctx, 0, goto error);
1983         return obj;
1984 error:
1985         obj.type->free(obj.v);
1986         obj.type = isl_obj_none;
1987         return obj;
1988 }
1989
1990 static struct isl_obj obj_add(struct isl_ctx *ctx,
1991         struct isl_obj obj1, struct isl_obj obj2)
1992 {
1993         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1994                 obj1 = to_union(ctx, obj1);
1995         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1996                 obj2 = to_union(ctx, obj2);
1997         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1998                 obj1 = to_union(ctx, obj1);
1999         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2000                 obj2 = to_union(ctx, obj2);
2001         if (obj1.type == isl_obj_pw_qpolynomial &&
2002             obj2.type == isl_obj_union_pw_qpolynomial)
2003                 obj1 = to_union(ctx, obj1);
2004         if (obj1.type == isl_obj_union_pw_qpolynomial &&
2005             obj2.type == isl_obj_pw_qpolynomial)
2006                 obj2 = to_union(ctx, obj2);
2007         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2008             obj2.type == isl_obj_union_pw_qpolynomial_fold)
2009                 obj1 = to_union(ctx, obj1);
2010         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2011             obj2.type == isl_obj_pw_qpolynomial_fold)
2012                 obj2 = to_union(ctx, obj2);
2013         isl_assert(ctx, obj1.type == obj2.type, goto error);
2014         if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2015                 obj1 = to_union(ctx, obj1);
2016                 obj2 = to_union(ctx, obj2);
2017         }
2018         if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2019                 obj1 = to_union(ctx, obj1);
2020                 obj2 = to_union(ctx, obj2);
2021         }
2022         if (obj1.type == isl_obj_pw_qpolynomial &&
2023             !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2024                 obj1 = to_union(ctx, obj1);
2025                 obj2 = to_union(ctx, obj2);
2026         }
2027         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2028             !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2029                 obj1 = to_union(ctx, obj1);
2030                 obj2 = to_union(ctx, obj2);
2031         }
2032         obj1.v = obj1.type->add(obj1.v, obj2.v);
2033         return obj1;
2034 error:
2035         obj1.type->free(obj1.v);
2036         obj2.type->free(obj2.v);
2037         obj1.type = isl_obj_none;
2038         obj1.v = NULL;
2039         return obj1;
2040 }
2041
2042 static struct isl_obj obj_read(struct isl_stream *s)
2043 {
2044         isl_map *map = NULL;
2045         struct isl_token *tok;
2046         struct vars *v = NULL;
2047         struct isl_obj obj = { isl_obj_set, NULL };
2048
2049         tok = next_token(s);
2050         if (!tok) {
2051                 isl_stream_error(s, NULL, "unexpected EOF");
2052                 goto error;
2053         }
2054         if (tok->type == ISL_TOKEN_VALUE) {
2055                 struct isl_token *tok2;
2056                 struct isl_map *map;
2057
2058                 tok2 = isl_stream_next_token(s);
2059                 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2060                     isl_int_is_neg(tok2->u.v)) {
2061                         if (tok2)
2062                                 isl_stream_push_token(s, tok2);
2063                         obj.type = isl_obj_int;
2064                         obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2065                         isl_token_free(tok);
2066                         return obj;
2067                 }
2068                 isl_stream_push_token(s, tok2);
2069                 isl_stream_push_token(s, tok);
2070                 map = map_read_polylib(s);
2071                 if (!map)
2072                         goto error;
2073                 if (isl_map_may_be_set(map))
2074                         obj.v = isl_map_range(map);
2075                 else {
2076                         obj.type = isl_obj_map;
2077                         obj.v = map;
2078                 }
2079                 return obj;
2080         }
2081         v = vars_new(s->ctx);
2082         if (!v) {
2083                 isl_stream_push_token(s, tok);
2084                 goto error;
2085         }
2086         map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2087         if (tok->type == '[') {
2088                 isl_stream_push_token(s, tok);
2089                 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2090                 if (!map)
2091                         goto error;
2092                 tok = isl_stream_next_token(s);
2093                 if (!tok || tok->type != ISL_TOKEN_TO) {
2094                         isl_stream_error(s, tok, "expecting '->'");
2095                         if (tok)
2096                                 isl_stream_push_token(s, tok);
2097                         goto error;
2098                 }
2099                 isl_token_free(tok);
2100                 tok = isl_stream_next_token(s);
2101         }
2102         if (!tok || tok->type != '{') {
2103                 isl_stream_error(s, tok, "expecting '{'");
2104                 if (tok)
2105                         isl_stream_push_token(s, tok);
2106                 goto error;
2107         }
2108         isl_token_free(tok);
2109
2110         tok = isl_stream_next_token(s);
2111         if (!tok)
2112                 ;
2113         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2114                 isl_token_free(tok);
2115                 if (isl_stream_eat(s, '='))
2116                         goto error;
2117                 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2118                 if (!map)
2119                         goto error;
2120         } else if (tok->type == '}') {
2121                 obj.type = isl_obj_union_set;
2122                 obj.v = isl_union_set_empty(isl_map_get_space(map));
2123                 isl_token_free(tok);
2124                 goto done;
2125         } else
2126                 isl_stream_push_token(s, tok);
2127
2128         for (;;) {
2129                 struct isl_obj o;
2130                 tok = NULL;
2131                 o = obj_read_body(s, isl_map_copy(map), v);
2132                 if (o.type == isl_obj_none || !o.v)
2133                         goto error;
2134                 if (!obj.v)
2135                         obj = o;
2136                 else {
2137                         obj = obj_add(s->ctx, obj, o);
2138                         if (obj.type == isl_obj_none || !obj.v)
2139                                 goto error;
2140                 }
2141                 tok = isl_stream_next_token(s);
2142                 if (!tok || tok->type != ';')
2143                         break;
2144                 isl_token_free(tok);
2145                 if (isl_stream_next_token_is(s, '}')) {
2146                         tok = isl_stream_next_token(s);
2147                         break;
2148                 }
2149         }
2150
2151         if (tok && tok->type == '}') {
2152                 isl_token_free(tok);
2153         } else {
2154                 isl_stream_error(s, tok, "unexpected isl_token");
2155                 if (tok)
2156                         isl_token_free(tok);
2157                 goto error;
2158         }
2159 done:
2160         vars_free(v);
2161         isl_map_free(map);
2162
2163         return obj;
2164 error:
2165         isl_map_free(map);
2166         obj.type->free(obj.v);
2167         if (v)
2168                 vars_free(v);
2169         obj.v = NULL;
2170         return obj;
2171 }
2172
2173 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2174 {
2175         return obj_read(s);
2176 }
2177
2178 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2179 {
2180         struct isl_obj obj;
2181
2182         obj = obj_read(s);
2183         if (obj.v)
2184                 isl_assert(s->ctx, obj.type == isl_obj_map ||
2185                                    obj.type == isl_obj_set, goto error);
2186         
2187         if (obj.type == isl_obj_set)
2188                 obj.v = isl_map_from_range(obj.v);
2189
2190         return obj.v;
2191 error:
2192         obj.type->free(obj.v);
2193         return NULL;
2194 }
2195
2196 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2197 {
2198         struct isl_obj obj;
2199
2200         obj = obj_read(s);
2201         if (obj.v) {
2202                 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2203                         obj.v = isl_map_range(obj.v);
2204                         obj.type = isl_obj_set;
2205                 }
2206                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2207         }
2208
2209         return obj.v;
2210 error:
2211         obj.type->free(obj.v);
2212         return NULL;
2213 }
2214
2215 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2216 {
2217         struct isl_obj obj;
2218
2219         obj = obj_read(s);
2220         if (obj.type == isl_obj_map) {
2221                 obj.type = isl_obj_union_map;
2222                 obj.v = isl_union_map_from_map(obj.v);
2223         }
2224         if (obj.type == isl_obj_set) {
2225                 obj.type = isl_obj_union_set;
2226                 obj.v = isl_union_set_from_set(obj.v);
2227         }
2228         if (obj.v && obj.type == isl_obj_union_set &&
2229             isl_union_set_is_empty(obj.v))
2230                 obj.type = isl_obj_union_map;
2231         if (obj.v && obj.type != isl_obj_union_map)
2232                 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2233
2234         return obj.v;
2235 error:
2236         obj.type->free(obj.v);
2237         return NULL;
2238 }
2239
2240 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2241 {
2242         struct isl_obj obj;
2243
2244         obj = obj_read(s);
2245         if (obj.type == isl_obj_set) {
2246                 obj.type = isl_obj_union_set;
2247                 obj.v = isl_union_set_from_set(obj.v);
2248         }
2249         if (obj.v)
2250                 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2251
2252         return obj.v;
2253 error:
2254         obj.type->free(obj.v);
2255         return NULL;
2256 }
2257
2258 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2259 {
2260         struct isl_obj obj;
2261         struct isl_map *map;
2262         struct isl_basic_map *bmap;
2263
2264         obj = obj_read(s);
2265         map = obj.v;
2266         if (!map)
2267                 return NULL;
2268
2269         isl_assert(map->ctx, map->n <= 1, goto error);
2270
2271         if (map->n == 0)
2272                 bmap = isl_basic_map_empty_like_map(map);
2273         else
2274                 bmap = isl_basic_map_copy(map->p[0]);
2275
2276         isl_map_free(map);
2277
2278         return bmap;
2279 error:
2280         isl_map_free(map);
2281         return NULL;
2282 }
2283
2284 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2285 {
2286         isl_basic_map *bmap;
2287         bmap = basic_map_read(s);
2288         if (!bmap)
2289                 return NULL;
2290         if (!isl_basic_map_may_be_set(bmap))
2291                 isl_die(s->ctx, isl_error_invalid,
2292                         "input is not a set", goto error);
2293         return isl_basic_map_range(bmap);
2294 error:
2295         isl_basic_map_free(bmap);
2296         return NULL;
2297 }
2298
2299 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2300         FILE *input)
2301 {
2302         struct isl_basic_map *bmap;
2303         struct isl_stream *s = isl_stream_new_file(ctx, input);
2304         if (!s)
2305                 return NULL;
2306         bmap = basic_map_read(s);
2307         isl_stream_free(s);
2308         return bmap;
2309 }
2310
2311 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2312         FILE *input)
2313 {
2314         isl_basic_set *bset;
2315         struct isl_stream *s = isl_stream_new_file(ctx, input);
2316         if (!s)
2317                 return NULL;
2318         bset = basic_set_read(s);
2319         isl_stream_free(s);
2320         return bset;
2321 }
2322
2323 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2324         const char *str)
2325 {
2326         struct isl_basic_map *bmap;
2327         struct isl_stream *s = isl_stream_new_str(ctx, str);
2328         if (!s)
2329                 return NULL;
2330         bmap = basic_map_read(s);
2331         isl_stream_free(s);
2332         return bmap;
2333 }
2334
2335 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2336         const char *str)
2337 {
2338         isl_basic_set *bset;
2339         struct isl_stream *s = isl_stream_new_str(ctx, str);
2340         if (!s)
2341                 return NULL;
2342         bset = basic_set_read(s);
2343         isl_stream_free(s);
2344         return bset;
2345 }
2346
2347 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2348         FILE *input)
2349 {
2350         struct isl_map *map;
2351         struct isl_stream *s = isl_stream_new_file(ctx, input);
2352         if (!s)
2353                 return NULL;
2354         map = isl_stream_read_map(s);
2355         isl_stream_free(s);
2356         return map;
2357 }
2358
2359 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2360         const char *str)
2361 {
2362         struct isl_map *map;
2363         struct isl_stream *s = isl_stream_new_str(ctx, str);
2364         if (!s)
2365                 return NULL;
2366         map = isl_stream_read_map(s);
2367         isl_stream_free(s);
2368         return map;
2369 }
2370
2371 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2372         FILE *input)
2373 {
2374         isl_set *set;
2375         struct isl_stream *s = isl_stream_new_file(ctx, input);
2376         if (!s)
2377                 return NULL;
2378         set = isl_stream_read_set(s);
2379         isl_stream_free(s);
2380         return set;
2381 }
2382
2383 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2384         const char *str)
2385 {
2386         isl_set *set;
2387         struct isl_stream *s = isl_stream_new_str(ctx, str);
2388         if (!s)
2389                 return NULL;
2390         set = isl_stream_read_set(s);
2391         isl_stream_free(s);
2392         return set;
2393 }
2394
2395 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2396         FILE *input)
2397 {
2398         isl_union_map *umap;
2399         struct isl_stream *s = isl_stream_new_file(ctx, input);
2400         if (!s)
2401                 return NULL;
2402         umap = isl_stream_read_union_map(s);
2403         isl_stream_free(s);
2404         return umap;
2405 }
2406
2407 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2408                 const char *str)
2409 {
2410         isl_union_map *umap;
2411         struct isl_stream *s = isl_stream_new_str(ctx, str);
2412         if (!s)
2413                 return NULL;
2414         umap = isl_stream_read_union_map(s);
2415         isl_stream_free(s);
2416         return umap;
2417 }
2418
2419 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2420         FILE *input)
2421 {
2422         isl_union_set *uset;
2423         struct isl_stream *s = isl_stream_new_file(ctx, input);
2424         if (!s)
2425                 return NULL;
2426         uset = isl_stream_read_union_set(s);
2427         isl_stream_free(s);
2428         return uset;
2429 }
2430
2431 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2432                 const char *str)
2433 {
2434         isl_union_set *uset;
2435         struct isl_stream *s = isl_stream_new_str(ctx, str);
2436         if (!s)
2437                 return NULL;
2438         uset = isl_stream_read_union_set(s);
2439         isl_stream_free(s);
2440         return uset;
2441 }
2442
2443 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2444 {
2445         struct isl_vec *vec = NULL;
2446         struct isl_token *tok;
2447         unsigned size;
2448         int j;
2449
2450         tok = isl_stream_next_token(s);
2451         if (!tok || tok->type != ISL_TOKEN_VALUE) {
2452                 isl_stream_error(s, tok, "expecting vector length");
2453                 goto error;
2454         }
2455
2456         size = isl_int_get_si(tok->u.v);
2457         isl_token_free(tok);
2458
2459         vec = isl_vec_alloc(s->ctx, size);
2460
2461         for (j = 0; j < size; ++j) {
2462                 tok = isl_stream_next_token(s);
2463                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2464                         isl_stream_error(s, tok, "expecting constant value");
2465                         goto error;
2466                 }
2467                 isl_int_set(vec->el[j], tok->u.v);
2468                 isl_token_free(tok);
2469         }
2470
2471         return vec;
2472 error:
2473         isl_token_free(tok);
2474         isl_vec_free(vec);
2475         return NULL;
2476 }
2477
2478 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2479 {
2480         return isl_vec_read_polylib(s);
2481 }
2482
2483 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2484 {
2485         isl_vec *v;
2486         struct isl_stream *s = isl_stream_new_file(ctx, input);
2487         if (!s)
2488                 return NULL;
2489         v = vec_read(s);
2490         isl_stream_free(s);
2491         return v;
2492 }
2493
2494 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2495         struct isl_stream *s)
2496 {
2497         struct isl_obj obj;
2498
2499         obj = obj_read(s);
2500         if (obj.v)
2501                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2502                            goto error);
2503
2504         return obj.v;
2505 error:
2506         obj.type->free(obj.v);
2507         return NULL;
2508 }
2509
2510 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2511                 const char *str)
2512 {
2513         isl_pw_qpolynomial *pwqp;
2514         struct isl_stream *s = isl_stream_new_str(ctx, str);
2515         if (!s)
2516                 return NULL;
2517         pwqp = isl_stream_read_pw_qpolynomial(s);
2518         isl_stream_free(s);
2519         return pwqp;
2520 }
2521
2522 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2523                 FILE *input)
2524 {
2525         isl_pw_qpolynomial *pwqp;
2526         struct isl_stream *s = isl_stream_new_file(ctx, input);
2527         if (!s)
2528                 return NULL;
2529         pwqp = isl_stream_read_pw_qpolynomial(s);
2530         isl_stream_free(s);
2531         return pwqp;
2532 }
2533
2534 /* Is the next token an identifer not in "v"?
2535  */
2536 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2537 {
2538         int n = v->n;
2539         int fresh;
2540         struct isl_token *tok;
2541
2542         tok = isl_stream_next_token(s);
2543         if (!tok)
2544                 return 0;
2545         fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2546         isl_stream_push_token(s, tok);
2547
2548         vars_drop(v, v->n - n);
2549
2550         return fresh;
2551 }
2552
2553 /* First read the domain of the affine expression, which may be
2554  * a parameter space or a set.
2555  * The tricky part is that we don't know if the domain is a set or not,
2556  * so when we are trying to read the domain, we may actually be reading
2557  * the affine expression itself (defined on a parameter domains)
2558  * If the tuple we are reading is named, we assume it's the domain.
2559  * Also, if inside the tuple, the first thing we find is a nested tuple
2560  * or a new identifier, we again assume it's the domain.
2561  * Otherwise, we assume we are reading an affine expression.
2562  */
2563 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2564         __isl_take isl_set *dom, struct vars *v)
2565 {
2566         struct isl_token *tok;
2567
2568         tok = isl_stream_next_token(s);
2569         if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2570                 isl_stream_push_token(s, tok);
2571                 return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2572         }
2573         if (!tok || tok->type != '[') {
2574                 isl_stream_error(s, tok, "expecting '['");
2575                 goto error;
2576         }
2577         if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2578                 isl_stream_push_token(s, tok);
2579                 dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2580         } else
2581                 isl_stream_push_token(s, tok);
2582
2583         return dom;
2584 error:
2585         if (tok)
2586                 isl_stream_push_token(s, tok);
2587         isl_set_free(dom);
2588         return NULL;
2589 }
2590
2591 /* Read an affine expression from "s".
2592  */
2593 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2594 {
2595         isl_aff *aff;
2596         isl_multi_aff *ma;
2597
2598         ma = isl_stream_read_multi_aff(s);
2599         if (!ma)
2600                 return NULL;
2601         if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2602                 isl_die(s->ctx, isl_error_invalid,
2603                         "expecting single affine expression",
2604                         goto error);
2605
2606         aff = isl_multi_aff_get_aff(ma, 0);
2607         isl_multi_aff_free(ma);
2608         return aff;
2609 error:
2610         isl_multi_aff_free(ma);
2611         return NULL;
2612 }
2613
2614 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2615  */
2616 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2617         __isl_take isl_set *dom, struct vars *v)
2618 {
2619         isl_pw_aff *pwaff = NULL;
2620
2621         if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2622                 goto error;
2623
2624         if (isl_stream_eat(s, '['))
2625                 goto error;
2626
2627         pwaff = accept_affine(s, isl_set_get_space(dom), v);
2628
2629         if (isl_stream_eat(s, ']'))
2630                 goto error;
2631
2632         dom = read_optional_disjuncts(s, dom, v, 0);
2633         pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2634
2635         return pwaff;
2636 error:
2637         isl_set_free(dom);
2638         isl_pw_aff_free(pwaff);
2639         return NULL;
2640 }
2641
2642 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2643 {
2644         struct vars *v;
2645         isl_set *dom = NULL;
2646         isl_set *aff_dom;
2647         isl_pw_aff *pa = NULL;
2648         int n;
2649
2650         v = vars_new(s->ctx);
2651         if (!v)
2652                 return NULL;
2653
2654         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2655         if (next_is_tuple(s)) {
2656                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2657                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2658                         goto error;
2659         }
2660         if (isl_stream_eat(s, '{'))
2661                 goto error;
2662
2663         n = v->n;
2664         aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2665         pa = read_pw_aff_with_dom(s, aff_dom, v);
2666         vars_drop(v, v->n - n);
2667
2668         while (isl_stream_eat_if_available(s, ';')) {
2669                 isl_pw_aff *pa_i;
2670
2671                 n = v->n;
2672                 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2673                 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2674                 vars_drop(v, v->n - n);
2675
2676                 pa = isl_pw_aff_union_add(pa, pa_i);
2677         }
2678
2679         if (isl_stream_eat(s, '}'))
2680                 goto error;
2681
2682         vars_free(v);
2683         isl_set_free(dom);
2684         return pa;
2685 error:
2686         vars_free(v);
2687         isl_set_free(dom);
2688         isl_pw_aff_free(pa);
2689         return NULL;
2690 }
2691
2692 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2693 {
2694         isl_aff *aff;
2695         struct isl_stream *s = isl_stream_new_str(ctx, str);
2696         if (!s)
2697                 return NULL;
2698         aff = isl_stream_read_aff(s);
2699         isl_stream_free(s);
2700         return aff;
2701 }
2702
2703 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2704 {
2705         isl_pw_aff *pa;
2706         struct isl_stream *s = isl_stream_new_str(ctx, str);
2707         if (!s)
2708                 return NULL;
2709         pa = isl_stream_read_pw_aff(s);
2710         isl_stream_free(s);
2711         return pa;
2712 }
2713
2714 /* Read an isl_pw_multi_aff from "s".
2715  * We currently read a generic object and if it turns out to be a set or
2716  * a map, we convert that to an isl_pw_multi_aff.
2717  * It would be more efficient if we were to construct the isl_pw_multi_aff
2718  * directly.
2719  */
2720 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2721 {
2722         struct isl_obj obj;
2723
2724         obj = obj_read(s);
2725         if (!obj.v)
2726                 return NULL;
2727
2728         if (obj.type == isl_obj_map)
2729                 return isl_pw_multi_aff_from_map(obj.v);
2730         if (obj.type == isl_obj_set)
2731                 return isl_pw_multi_aff_from_set(obj.v);
2732
2733         obj.type->free(obj.v);
2734         isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2735                 return NULL);
2736 }
2737
2738 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2739         const char *str)
2740 {
2741         isl_pw_multi_aff *pma;
2742         struct isl_stream *s = isl_stream_new_str(ctx, str);
2743         if (!s)
2744                 return NULL;
2745         pma = isl_stream_read_pw_multi_aff(s);
2746         isl_stream_free(s);
2747         return pma;
2748 }
2749
2750 /* Assuming "pa" represents a single affine expression defined on a universe
2751  * domain, extract this affine expression.
2752  */
2753 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2754 {
2755         isl_aff *aff;
2756
2757         if (!pa)
2758                 return NULL;
2759         if (pa->n != 1)
2760                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2761                         "expecting single affine expression",
2762                         goto error);
2763         if (!isl_set_plain_is_universe(pa->p[0].set))
2764                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2765                         "expecting universe domain",
2766                         goto error);
2767
2768         aff = isl_aff_copy(pa->p[0].aff);
2769         isl_pw_aff_free(pa);
2770         return aff;
2771 error:
2772         isl_pw_aff_free(pa);
2773         return NULL;
2774 }
2775
2776 /* Read a multi-affine expression from "s".
2777  * If the multi-affine expression has a domain, then then tuple
2778  * representing this domain cannot involve any affine expressions.
2779  * The tuple representing the actual expressions needs to consist
2780  * of only affine expressions.  Moreover, these expressions can
2781  * only depend on parameters and input dimensions and not on other
2782  * output dimensions.
2783  */
2784 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2785 {
2786         struct vars *v;
2787         isl_set *dom = NULL;
2788         isl_multi_pw_aff *tuple = NULL;
2789         int dim, i, n;
2790         isl_space *space, *dom_space;
2791         isl_multi_aff *ma = NULL;
2792
2793         v = vars_new(s->ctx);
2794         if (!v)
2795                 return NULL;
2796
2797         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2798         if (next_is_tuple(s)) {
2799                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2800                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2801                         goto error;
2802         }
2803         if (!isl_set_plain_is_universe(dom))
2804                 isl_die(s->ctx, isl_error_invalid,
2805                         "expecting universe parameter domain", goto error);
2806         if (isl_stream_eat(s, '{'))
2807                 goto error;
2808
2809         tuple = read_tuple(s, v, 0, 0);
2810         if (!tuple)
2811                 goto error;
2812         if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2813                 isl_set *set;
2814                 isl_space *space;
2815                 int has_expr;
2816
2817                 has_expr = tuple_has_expr(tuple);
2818                 if (has_expr < 0)
2819                         goto error;
2820                 if (has_expr)
2821                         isl_die(s->ctx, isl_error_invalid,
2822                                 "expecting universe domain", goto error);
2823                 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2824                 set = isl_set_universe(space);
2825                 dom = isl_set_intersect_params(set, dom);
2826                 isl_multi_pw_aff_free(tuple);
2827                 tuple = read_tuple(s, v, 0, 0);
2828                 if (!tuple)
2829                         goto error;
2830         }
2831
2832         if (isl_stream_eat(s, '}'))
2833                 goto error;
2834
2835         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2836         dim = isl_set_dim(dom, isl_dim_all);
2837         dom_space = isl_set_get_space(dom);
2838         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2839         space = isl_space_align_params(space, isl_space_copy(dom_space));
2840         if (!isl_space_is_params(dom_space))
2841                 space = isl_space_map_from_domain_and_range(
2842                                 isl_space_copy(dom_space), space);
2843         isl_space_free(dom_space);
2844         ma = isl_multi_aff_alloc(space);
2845
2846         for (i = 0; i < n; ++i) {
2847                 isl_pw_aff *pa;
2848                 isl_aff *aff;
2849                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2850                 aff = aff_from_pw_aff(pa);
2851                 if (!aff)
2852                         goto error;
2853                 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2854                         isl_aff_free(aff);
2855                         isl_die(s->ctx, isl_error_invalid,
2856                                 "not an affine expression", goto error);
2857                 }
2858                 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2859                 space = isl_multi_aff_get_domain_space(ma);
2860                 aff = isl_aff_reset_domain_space(aff, space);
2861                 ma = isl_multi_aff_set_aff(ma, i, aff);
2862         }
2863
2864         isl_multi_pw_aff_free(tuple);
2865         vars_free(v);
2866         isl_set_free(dom);
2867         return ma;
2868 error:
2869         isl_multi_pw_aff_free(tuple);
2870         vars_free(v);
2871         isl_set_free(dom);
2872         isl_multi_aff_free(ma);
2873         return NULL;
2874 }
2875
2876 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2877         const char *str)
2878 {
2879         isl_multi_aff *maff;
2880         struct isl_stream *s = isl_stream_new_str(ctx, str);
2881         if (!s)
2882                 return NULL;
2883         maff = isl_stream_read_multi_aff(s);
2884         isl_stream_free(s);
2885         return maff;
2886 }
2887
2888 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2889         struct isl_stream *s)
2890 {
2891         struct isl_obj obj;
2892
2893         obj = obj_read(s);
2894         if (obj.type == isl_obj_pw_qpolynomial) {
2895                 obj.type = isl_obj_union_pw_qpolynomial;
2896                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2897         }
2898         if (obj.v)
2899                 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2900                            goto error);
2901
2902         return obj.v;
2903 error:
2904         obj.type->free(obj.v);
2905         return NULL;
2906 }
2907
2908 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2909         isl_ctx *ctx, const char *str)
2910 {
2911         isl_union_pw_qpolynomial *upwqp;
2912         struct isl_stream *s = isl_stream_new_str(ctx, str);
2913         if (!s)
2914                 return NULL;
2915         upwqp = isl_stream_read_union_pw_qpolynomial(s);
2916         isl_stream_free(s);
2917         return upwqp;
2918 }