6bf6929b737b5e0648f3633466ca6eb95a07f9e9
[platform/upstream/isl.git] / 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                         return NULL;
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  */
918 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
919         struct vars *v, int rational)
920 {
921         int i = 0;
922         struct isl_token *tok;
923         isl_multi_pw_aff *res;
924
925         res = tuple_alloc(v);
926
927         if (isl_stream_next_token_is(s, ']'))
928                 return res;
929
930         while ((tok = next_token(s)) != NULL) {
931                 int new_name = 0;
932
933                 res = tuple_add_dim(res, v);
934
935                 if (tok->type == ISL_TOKEN_IDENT) {
936                         int n = v->n;
937                         int p = vars_pos(v, tok->u.s, -1);
938                         if (p < 0)
939                                 goto error;
940                         new_name = p >= n;
941                 }
942
943                 if (new_name) {
944                         res = tuple_set_dim_name(res, i, v->v->name);
945                         isl_token_free(tok);
946                         if (isl_stream_eat_if_available(s, '='))
947                                 res = read_tuple_var_def(s, res, i, v,
948                                                         rational);
949                 } else {
950                         isl_stream_push_token(s, tok);
951                         tok = NULL;
952                         if (vars_add_anon(v) < 0)
953                                 goto error;
954                         res = read_tuple_var_def(s, res, i, v, rational);
955                 }
956
957                 tok = isl_stream_next_token(s);
958                 if (tok && tok->type == ']' &&
959                     isl_stream_next_token_is(s, '[')) {
960                         isl_token_free(tok);
961                         tok = isl_stream_next_token(s);
962                 } else if (!tok || tok->type != ',')
963                         break;
964
965                 isl_token_free(tok);
966                 i++;
967         }
968         if (tok)
969                 isl_stream_push_token(s, tok);
970
971         return res;
972 error:
973         isl_token_free(tok);
974         return isl_multi_pw_aff_free(res);
975 }
976
977 /* Read a tuple and represent it as an isl_multi_pw_aff.  See tuple_alloc.
978  */
979 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
980         struct vars *v, int rational)
981 {
982         struct isl_token *tok;
983         char *name = NULL;
984         isl_multi_pw_aff *res = NULL;
985
986         tok = isl_stream_next_token(s);
987         if (!tok)
988                 goto error;
989         if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
990                 name = strdup(tok->u.s);
991                 isl_token_free(tok);
992                 if (!name)
993                         goto error;
994         } else
995                 isl_stream_push_token(s, tok);
996         if (isl_stream_eat(s, '['))
997                 goto error;
998         if (next_is_tuple(s)) {
999                 isl_multi_pw_aff *out;
1000                 int n;
1001                 res = read_tuple(s, v, rational);
1002                 if (isl_stream_eat(s, ISL_TOKEN_TO))
1003                         goto error;
1004                 out = read_tuple(s, v, rational);
1005                 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1006                 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1007                 res = isl_multi_pw_aff_range_product(res, out);
1008         } else
1009                 res = read_tuple_var_list(s, v, rational);
1010         if (isl_stream_eat(s, ']'))
1011                 goto error;
1012
1013         if (name) {
1014                 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1015                 free(name);
1016         }
1017
1018         return res;
1019 error:
1020         free(name);
1021         return isl_multi_pw_aff_free(res);
1022 }
1023
1024 /* Read a tuple from "s" and add it to "map".
1025  * The tuple is initially represented as an isl_multi_pw_aff.
1026  * We first create the appropriate space in "map" based on the range
1027  * space of this isl_multi_pw_aff.  Then, we add equalities based
1028  * on the affine expressions.  These live in an anonymous space,
1029  * however, so we first need to reset the space to that of "map".
1030  */
1031 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1032         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1033         int rational)
1034 {
1035         int i, n;
1036         isl_multi_pw_aff *tuple;
1037         isl_space *space = NULL;
1038
1039         tuple = read_tuple(s, v, rational);
1040         if (!tuple)
1041                 goto error;
1042
1043         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1044         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1045
1046         if (type == isl_dim_param) {
1047                 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1048                     isl_space_is_wrapping(space)) {
1049                         isl_die(s->ctx, isl_error_invalid,
1050                                 "parameter tuples cannot be named or nested",
1051                                 goto error);
1052                 }
1053                 map = isl_map_add_dims(map, type, n);
1054                 for (i = 0; i < n; ++i) {
1055                         isl_id *id;
1056                         if (!isl_space_has_dim_name(space, isl_dim_set, i))
1057                                 isl_die(s->ctx, isl_error_invalid,
1058                                         "parameters must be named",
1059                                         goto error);
1060                         id = isl_space_get_dim_id(space, isl_dim_set, i);
1061                         map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1062                 }
1063         } else if (type == isl_dim_in) {
1064                 isl_set *set;
1065
1066                 set = isl_set_universe(isl_space_copy(space));
1067                 if (rational)
1068                         set = isl_set_set_rational(set);
1069                 set = isl_set_intersect_params(set, isl_map_params(map));
1070                 map = isl_map_from_domain(set);
1071         } else {
1072                 isl_set *set;
1073
1074                 set = isl_set_universe(isl_space_copy(space));
1075                 if (rational)
1076                         set = isl_set_set_rational(set);
1077                 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1078         }
1079
1080         for (i = 0; i < n; ++i) {
1081                 isl_pw_aff *pa;
1082                 isl_space *space;
1083                 isl_aff *aff;
1084                 isl_set *set;
1085                 isl_map *map_i;
1086
1087                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1088                 space = isl_pw_aff_get_domain_space(pa);
1089                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1090                 aff = isl_aff_add_coefficient_si(aff,
1091                                                 isl_dim_in, v->n - n + i, -1);
1092                 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1093                 if (rational)
1094                         pa = isl_pw_aff_set_rational(pa);
1095                 set = isl_pw_aff_zero_set(pa);
1096                 map_i = isl_map_from_range(set);
1097                 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1098                 map = isl_map_intersect(map, map_i);
1099         }
1100
1101         isl_space_free(space);
1102         isl_multi_pw_aff_free(tuple);
1103         return map;
1104 error:
1105         isl_space_free(space);
1106         isl_multi_pw_aff_free(tuple);
1107         isl_map_free(map);
1108         return NULL;
1109 }
1110
1111 static __isl_give isl_set *construct_constraints(
1112         __isl_take isl_set *set, int type,
1113         __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1114         int rational)
1115 {
1116         isl_set *cond;
1117
1118         if (rational) {
1119                 left = isl_pw_aff_list_set_rational(left);
1120                 right = isl_pw_aff_list_set_rational(right);
1121         }
1122         if (type == ISL_TOKEN_LE)
1123                 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
1124                                               isl_pw_aff_list_copy(right));
1125         else if (type == ISL_TOKEN_GE)
1126                 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
1127                                               isl_pw_aff_list_copy(right));
1128         else if (type == ISL_TOKEN_LT)
1129                 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
1130                                               isl_pw_aff_list_copy(right));
1131         else if (type == ISL_TOKEN_GT)
1132                 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
1133                                               isl_pw_aff_list_copy(right));
1134         else if (type == ISL_TOKEN_NE)
1135                 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
1136                                               isl_pw_aff_list_copy(right));
1137         else
1138                 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
1139                                               isl_pw_aff_list_copy(right));
1140
1141         return isl_set_intersect(set, cond);
1142 }
1143
1144 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1145         struct vars *v, __isl_take isl_map *map, int rational)
1146 {
1147         struct isl_token *tok = NULL;
1148         isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1149         isl_set *set;
1150
1151         set = isl_map_wrap(map);
1152         list1 = accept_affine_list(s, isl_set_get_space(set), v);
1153         if (!list1)
1154                 goto error;
1155         tok = isl_stream_next_token(s);
1156         if (!is_comparator(tok)) {
1157                 isl_stream_error(s, tok, "missing operator");
1158                 if (tok)
1159                         isl_stream_push_token(s, tok);
1160                 tok = NULL;
1161                 goto error;
1162         }
1163         for (;;) {
1164                 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1165                 if (!list2)
1166                         goto error;
1167
1168                 set = construct_constraints(set, tok->type, list1, list2,
1169                                                 rational);
1170                 isl_token_free(tok);
1171                 isl_pw_aff_list_free(list1);
1172                 list1 = list2;
1173
1174                 tok = isl_stream_next_token(s);
1175                 if (!is_comparator(tok)) {
1176                         if (tok)
1177                                 isl_stream_push_token(s, tok);
1178                         break;
1179                 }
1180         }
1181         isl_pw_aff_list_free(list1);
1182
1183         return isl_set_unwrap(set);
1184 error:
1185         if (tok)
1186                 isl_token_free(tok);
1187         isl_pw_aff_list_free(list1);
1188         isl_pw_aff_list_free(list2);
1189         isl_set_free(set);
1190         return NULL;
1191 }
1192
1193 static __isl_give isl_map *read_exists(struct isl_stream *s,
1194         struct vars *v, __isl_take isl_map *map, int rational)
1195 {
1196         int n = v->n;
1197         int seen_paren = isl_stream_eat_if_available(s, '(');
1198
1199         map = isl_map_from_domain(isl_map_wrap(map));
1200         map = read_defined_var_list(s, v, map, rational);
1201
1202         if (isl_stream_eat(s, ':'))
1203                 goto error;
1204
1205         map = read_disjuncts(s, v, map, rational);
1206         map = isl_set_unwrap(isl_map_domain(map));
1207
1208         vars_drop(v, v->n - n);
1209         if (seen_paren && isl_stream_eat(s, ')'))
1210                 goto error;
1211
1212         return map;
1213 error:
1214         isl_map_free(map);
1215         return NULL;
1216 }
1217
1218 /* Parse an expression between parentheses and push the result
1219  * back on the stream.
1220  *
1221  * The parsed expression may be either an affine expression
1222  * or a condition.  The first type is pushed onto the stream
1223  * as an isl_pw_aff, while the second is pushed as an isl_map.
1224  *
1225  * If the initial token indicates the start of a condition,
1226  * we parse it as such.
1227  * Otherwise, we first parse an affine expression and push
1228  * that onto the stream.  If the affine expression covers the
1229  * entire expression between parentheses, we return.
1230  * Otherwise, we assume that the affine expression is the
1231  * start of a condition and continue parsing.
1232  */
1233 static int resolve_paren_expr(struct isl_stream *s,
1234         struct vars *v, __isl_take isl_map *map, int rational)
1235 {
1236         struct isl_token *tok, *tok2;
1237         int line, col;
1238         isl_pw_aff *pwaff;
1239
1240         tok = isl_stream_next_token(s);
1241         if (!tok || tok->type != '(')
1242                 goto error;
1243
1244         if (isl_stream_next_token_is(s, '('))
1245                 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1246                         goto error;
1247
1248         if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1249             isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1250             isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1251             isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1252             isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1253                 map = read_disjuncts(s, v, map, rational);
1254                 if (isl_stream_eat(s, ')'))
1255                         goto error;
1256                 tok->type = ISL_TOKEN_MAP;
1257                 tok->u.map = map;
1258                 isl_stream_push_token(s, tok);
1259                 return 0;
1260         }
1261
1262         tok2 = isl_stream_next_token(s);
1263         if (!tok2)
1264                 goto error;
1265         line = tok2->line;
1266         col = tok2->col;
1267         isl_stream_push_token(s, tok2);
1268
1269         pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1270         if (!pwaff)
1271                 goto error;
1272
1273         tok2 = isl_token_new(s->ctx, line, col, 0);
1274         if (!tok2)
1275                 goto error2;
1276         tok2->type = ISL_TOKEN_AFF;
1277         tok2->u.pwaff = pwaff;
1278
1279         if (isl_stream_eat_if_available(s, ')')) {
1280                 isl_stream_push_token(s, tok2);
1281                 isl_token_free(tok);
1282                 isl_map_free(map);
1283                 return 0;
1284         }
1285
1286         isl_stream_push_token(s, tok2);
1287
1288         map = read_disjuncts(s, v, map, rational);
1289         if (isl_stream_eat(s, ')'))
1290                 goto error;
1291
1292         tok->type = ISL_TOKEN_MAP;
1293         tok->u.map = map;
1294         isl_stream_push_token(s, tok);
1295
1296         return 0;
1297 error2:
1298         isl_pw_aff_free(pwaff);
1299 error:
1300         isl_token_free(tok);
1301         isl_map_free(map);
1302         return -1;
1303 }
1304
1305 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1306         struct vars *v, __isl_take isl_map *map, int rational)
1307 {
1308         if (isl_stream_next_token_is(s, '('))
1309                 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1310                         goto error;
1311
1312         if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1313                 struct isl_token *tok;
1314                 tok = isl_stream_next_token(s);
1315                 if (!tok)
1316                         goto error;
1317                 isl_map_free(map);
1318                 map = isl_map_copy(tok->u.map);
1319                 isl_token_free(tok);
1320                 return map;
1321         }
1322
1323         if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1324                 return read_exists(s, v, map, rational);
1325
1326         if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1327                 return map;
1328
1329         if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1330                 isl_space *dim = isl_map_get_space(map);
1331                 isl_map_free(map);
1332                 return isl_map_empty(dim);
1333         }
1334                 
1335         return add_constraint(s, v, map, rational);
1336 error:
1337         isl_map_free(map);
1338         return NULL;
1339 }
1340
1341 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1342         struct vars *v, __isl_take isl_map *map, int rational)
1343 {
1344         isl_map *res;
1345         int negate;
1346
1347         negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1348         res = read_conjunct(s, v, isl_map_copy(map), rational);
1349         if (negate)
1350                 res = isl_map_subtract(isl_map_copy(map), res);
1351
1352         while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1353                 isl_map *res_i;
1354
1355                 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1356                 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1357                 if (negate)
1358                         res = isl_map_subtract(res, res_i);
1359                 else
1360                         res = isl_map_intersect(res, res_i);
1361         }
1362
1363         isl_map_free(map);
1364         return res;
1365 }
1366
1367 static struct isl_map *read_disjuncts(struct isl_stream *s,
1368         struct vars *v, __isl_take isl_map *map, int rational)
1369 {
1370         isl_map *res;
1371
1372         if (isl_stream_next_token_is(s, '}')) {
1373                 isl_space *dim = isl_map_get_space(map);
1374                 isl_map_free(map);
1375                 return isl_map_universe(dim);
1376         }
1377
1378         res = read_conjuncts(s, v, isl_map_copy(map), rational);
1379         while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1380                 isl_map *res_i;
1381
1382                 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1383                 res = isl_map_union(res, res_i);
1384         }
1385
1386         isl_map_free(map);
1387         return res;
1388 }
1389
1390 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1391 {
1392         if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1393                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1394                            isl_basic_map_dim(bmap, isl_dim_in) + pos;
1395         pos -= isl_basic_map_dim(bmap, isl_dim_out);
1396
1397         if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1398                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1399         pos -= isl_basic_map_dim(bmap, isl_dim_in);
1400
1401         if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1402                 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1403                            isl_basic_map_dim(bmap, isl_dim_in) +
1404                            isl_basic_map_dim(bmap, isl_dim_out) + pos;
1405         pos -= isl_basic_map_dim(bmap, isl_dim_div);
1406
1407         if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1408                 return 1 + pos;
1409
1410         return 0;
1411 }
1412
1413 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1414         struct isl_stream *s, __isl_take isl_basic_map *bmap)
1415 {
1416         int j;
1417         struct isl_token *tok;
1418         int type;
1419         int k;
1420         isl_int *c;
1421         unsigned nparam;
1422         unsigned dim;
1423
1424         if (!bmap)
1425                 return NULL;
1426
1427         nparam = isl_basic_map_dim(bmap, isl_dim_param);
1428         dim = isl_basic_map_dim(bmap, isl_dim_out);
1429
1430         tok = isl_stream_next_token(s);
1431         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1432                 isl_stream_error(s, tok, "expecting coefficient");
1433                 if (tok)
1434                         isl_stream_push_token(s, tok);
1435                 goto error;
1436         }
1437         if (!tok->on_new_line) {
1438                 isl_stream_error(s, tok, "coefficient should appear on new line");
1439                 isl_stream_push_token(s, tok);
1440                 goto error;
1441         }
1442
1443         type = isl_int_get_si(tok->u.v);
1444         isl_token_free(tok);
1445
1446         isl_assert(s->ctx, type == 0 || type == 1, goto error);
1447         if (type == 0) {
1448                 k = isl_basic_map_alloc_equality(bmap);
1449                 c = bmap->eq[k];
1450         } else {
1451                 k = isl_basic_map_alloc_inequality(bmap);
1452                 c = bmap->ineq[k];
1453         }
1454         if (k < 0)
1455                 goto error;
1456
1457         for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1458                 int pos;
1459                 tok = isl_stream_next_token(s);
1460                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1461                         isl_stream_error(s, tok, "expecting coefficient");
1462                         if (tok)
1463                                 isl_stream_push_token(s, tok);
1464                         goto error;
1465                 }
1466                 if (tok->on_new_line) {
1467                         isl_stream_error(s, tok,
1468                                 "coefficient should not appear on new line");
1469                         isl_stream_push_token(s, tok);
1470                         goto error;
1471                 }
1472                 pos = polylib_pos_to_isl_pos(bmap, j);
1473                 isl_int_set(c[pos], tok->u.v);
1474                 isl_token_free(tok);
1475         }
1476
1477         return bmap;
1478 error:
1479         isl_basic_map_free(bmap);
1480         return NULL;
1481 }
1482
1483 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1484 {
1485         int i;
1486         struct isl_token *tok;
1487         struct isl_token *tok2;
1488         int n_row, n_col;
1489         int on_new_line;
1490         unsigned in = 0, out, local = 0;
1491         struct isl_basic_map *bmap = NULL;
1492         int nparam = 0;
1493
1494         tok = isl_stream_next_token(s);
1495         if (!tok) {
1496                 isl_stream_error(s, NULL, "unexpected EOF");
1497                 return NULL;
1498         }
1499         tok2 = isl_stream_next_token(s);
1500         if (!tok2) {
1501                 isl_token_free(tok);
1502                 isl_stream_error(s, NULL, "unexpected EOF");
1503                 return NULL;
1504         }
1505         if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1506                 isl_stream_push_token(s, tok2);
1507                 isl_stream_push_token(s, tok);
1508                 isl_stream_error(s, NULL,
1509                                  "expecting constraint matrix dimensions");
1510                 return NULL;
1511         }
1512         n_row = isl_int_get_si(tok->u.v);
1513         n_col = isl_int_get_si(tok2->u.v);
1514         on_new_line = tok2->on_new_line;
1515         isl_token_free(tok2);
1516         isl_token_free(tok);
1517         isl_assert(s->ctx, !on_new_line, return NULL);
1518         isl_assert(s->ctx, n_row >= 0, return NULL);
1519         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1520         tok = isl_stream_next_token_on_same_line(s);
1521         if (tok) {
1522                 if (tok->type != ISL_TOKEN_VALUE) {
1523                         isl_stream_error(s, tok,
1524                                     "expecting number of output dimensions");
1525                         isl_stream_push_token(s, tok);
1526                         goto error;
1527                 }
1528                 out = isl_int_get_si(tok->u.v);
1529                 isl_token_free(tok);
1530
1531                 tok = isl_stream_next_token_on_same_line(s);
1532                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1533                         isl_stream_error(s, tok,
1534                                     "expecting number of input dimensions");
1535                         if (tok)
1536                                 isl_stream_push_token(s, tok);
1537                         goto error;
1538                 }
1539                 in = isl_int_get_si(tok->u.v);
1540                 isl_token_free(tok);
1541
1542                 tok = isl_stream_next_token_on_same_line(s);
1543                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1544                         isl_stream_error(s, tok,
1545                                     "expecting number of existentials");
1546                         if (tok)
1547                                 isl_stream_push_token(s, tok);
1548                         goto error;
1549                 }
1550                 local = isl_int_get_si(tok->u.v);
1551                 isl_token_free(tok);
1552
1553                 tok = isl_stream_next_token_on_same_line(s);
1554                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1555                         isl_stream_error(s, tok,
1556                                     "expecting number of parameters");
1557                         if (tok)
1558                                 isl_stream_push_token(s, tok);
1559                         goto error;
1560                 }
1561                 nparam = isl_int_get_si(tok->u.v);
1562                 isl_token_free(tok);
1563                 if (n_col != 1 + out + in + local + nparam + 1) {
1564                         isl_stream_error(s, NULL,
1565                                     "dimensions don't match");
1566                         goto error;
1567                 }
1568         } else
1569                 out = n_col - 2 - nparam;
1570         bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1571         if (!bmap)
1572                 return NULL;
1573
1574         for (i = 0; i < local; ++i) {
1575                 int k = isl_basic_map_alloc_div(bmap);
1576                 if (k < 0)
1577                         goto error;
1578                 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1579         }
1580
1581         for (i = 0; i < n_row; ++i)
1582                 bmap = basic_map_read_polylib_constraint(s, bmap);
1583
1584         tok = isl_stream_next_token_on_same_line(s);
1585         if (tok) {
1586                 isl_stream_error(s, tok, "unexpected extra token on line");
1587                 isl_stream_push_token(s, tok);
1588                 goto error;
1589         }
1590
1591         bmap = isl_basic_map_simplify(bmap);
1592         bmap = isl_basic_map_finalize(bmap);
1593         return bmap;
1594 error:
1595         isl_basic_map_free(bmap);
1596         return NULL;
1597 }
1598
1599 static struct isl_map *map_read_polylib(struct isl_stream *s)
1600 {
1601         struct isl_token *tok;
1602         struct isl_token *tok2;
1603         int i, n;
1604         struct isl_map *map;
1605
1606         tok = isl_stream_next_token(s);
1607         if (!tok) {
1608                 isl_stream_error(s, NULL, "unexpected EOF");
1609                 return NULL;
1610         }
1611         tok2 = isl_stream_next_token_on_same_line(s);
1612         if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1613                 isl_stream_push_token(s, tok2);
1614                 isl_stream_push_token(s, tok);
1615                 return isl_map_from_basic_map(basic_map_read_polylib(s));
1616         }
1617         if (tok2) {
1618                 isl_stream_error(s, tok2, "unexpected token");
1619                 isl_stream_push_token(s, tok2);
1620                 isl_stream_push_token(s, tok);
1621                 return NULL;
1622         }
1623         n = isl_int_get_si(tok->u.v);
1624         isl_token_free(tok);
1625
1626         isl_assert(s->ctx, n >= 1, return NULL);
1627
1628         map = isl_map_from_basic_map(basic_map_read_polylib(s));
1629
1630         for (i = 1; map && i < n; ++i)
1631                 map = isl_map_union(map,
1632                         isl_map_from_basic_map(basic_map_read_polylib(s)));
1633
1634         return map;
1635 }
1636
1637 static int optional_power(struct isl_stream *s)
1638 {
1639         int pow;
1640         struct isl_token *tok;
1641
1642         tok = isl_stream_next_token(s);
1643         if (!tok)
1644                 return 1;
1645         if (tok->type != '^') {
1646                 isl_stream_push_token(s, tok);
1647                 return 1;
1648         }
1649         isl_token_free(tok);
1650         tok = isl_stream_next_token(s);
1651         if (!tok || tok->type != ISL_TOKEN_VALUE) {
1652                 isl_stream_error(s, tok, "expecting exponent");
1653                 if (tok)
1654                         isl_stream_push_token(s, tok);
1655                 return 1;
1656         }
1657         pow = isl_int_get_si(tok->u.v);
1658         isl_token_free(tok);
1659         return pow;
1660 }
1661
1662 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1663         __isl_keep isl_map *map, struct vars *v);
1664
1665 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1666         __isl_keep isl_map *map, struct vars *v)
1667 {
1668         isl_pw_qpolynomial *pwqp;
1669         struct isl_token *tok;
1670
1671         tok = next_token(s);
1672         if (!tok) {
1673                 isl_stream_error(s, NULL, "unexpected EOF");
1674                 return NULL;
1675         }
1676         if (tok->type == '(') {
1677                 int pow;
1678
1679                 isl_token_free(tok);
1680                 pwqp = read_term(s, map, v);
1681                 if (!pwqp)
1682                         return NULL;
1683                 if (isl_stream_eat(s, ')'))
1684                         goto error;
1685                 pow = optional_power(s);
1686                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1687         } else if (tok->type == ISL_TOKEN_VALUE) {
1688                 struct isl_token *tok2;
1689                 tok2 = isl_stream_next_token(s);
1690                 isl_qpolynomial *qp;
1691                 if (tok2 && tok2->type == '/') {
1692                         isl_token_free(tok2);
1693                         tok2 = next_token(s);
1694                         if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1695                                 isl_stream_error(s, tok2, "expected denominator");
1696                                 isl_token_free(tok);
1697                                 isl_token_free(tok2);
1698                                 return NULL;
1699                         }
1700                         qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1701                                                     tok->u.v, tok2->u.v);
1702                         isl_token_free(tok2);
1703                 } else {
1704                         isl_stream_push_token(s, tok2);
1705                         qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1706                                                 tok->u.v);
1707                 }
1708                 isl_token_free(tok);
1709                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1710         } else if (tok->type == ISL_TOKEN_INFTY) {
1711                 isl_qpolynomial *qp;
1712                 isl_token_free(tok);
1713                 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1714                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1715         } else if (tok->type == ISL_TOKEN_NAN) {
1716                 isl_qpolynomial *qp;
1717                 isl_token_free(tok);
1718                 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1719                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1720         } else if (tok->type == ISL_TOKEN_IDENT) {
1721                 int n = v->n;
1722                 int pos = vars_pos(v, tok->u.s, -1);
1723                 int pow;
1724                 isl_qpolynomial *qp;
1725                 if (pos < 0) {
1726                         isl_token_free(tok);
1727                         return NULL;
1728                 }
1729                 if (pos >= n) {
1730                         vars_drop(v, v->n - n);
1731                         isl_stream_error(s, tok, "unknown identifier");
1732                         isl_token_free(tok);
1733                         return NULL;
1734                 }
1735                 isl_token_free(tok);
1736                 pow = optional_power(s);
1737                 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1738                 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1739         } else if (tok->type == '[') {
1740                 isl_pw_aff *pwaff;
1741                 int pow;
1742
1743                 isl_stream_push_token(s, tok);
1744                 pwaff = accept_div(s, isl_map_get_space(map), v);
1745                 pow = optional_power(s);
1746                 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1747                 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1748         } else if (tok->type == '-') {
1749                 isl_token_free(tok);
1750                 pwqp = read_factor(s, map, v);
1751                 pwqp = isl_pw_qpolynomial_neg(pwqp);
1752         } else {
1753                 isl_stream_error(s, tok, "unexpected isl_token");
1754                 isl_stream_push_token(s, tok);
1755                 return NULL;
1756         }
1757
1758         if (isl_stream_eat_if_available(s, '*') ||
1759             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1760                 isl_pw_qpolynomial *pwqp2;
1761
1762                 pwqp2 = read_factor(s, map, v);
1763                 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1764         }
1765
1766         return pwqp;
1767 error:
1768         isl_pw_qpolynomial_free(pwqp);
1769         return NULL;
1770 }
1771
1772 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1773         __isl_keep isl_map *map, struct vars *v)
1774 {
1775         struct isl_token *tok;
1776         isl_pw_qpolynomial *pwqp;
1777
1778         pwqp = read_factor(s, map, v);
1779
1780         for (;;) {
1781                 tok = next_token(s);
1782                 if (!tok)
1783                         return pwqp;
1784
1785                 if (tok->type == '+') {
1786                         isl_pw_qpolynomial *pwqp2;
1787
1788                         isl_token_free(tok);
1789                         pwqp2 = read_factor(s, map, v);
1790                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1791                 } else if (tok->type == '-') {
1792                         isl_pw_qpolynomial *pwqp2;
1793
1794                         isl_token_free(tok);
1795                         pwqp2 = read_factor(s, map, v);
1796                         pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1797                 } else if (tok->type == ISL_TOKEN_VALUE &&
1798                             isl_int_is_neg(tok->u.v)) {
1799                         isl_pw_qpolynomial *pwqp2;
1800
1801                         isl_stream_push_token(s, tok);
1802                         pwqp2 = read_factor(s, map, v);
1803                         pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1804                 } else {
1805                         isl_stream_push_token(s, tok);
1806                         break;
1807                 }
1808         }
1809
1810         return pwqp;
1811 }
1812
1813 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1814         __isl_take isl_map *map, struct vars *v, int rational)
1815 {
1816         struct isl_token *tok;
1817
1818         tok = isl_stream_next_token(s);
1819         if (!tok) {
1820                 isl_stream_error(s, NULL, "unexpected EOF");
1821                 goto error;
1822         }
1823         if (tok->type == ':' ||
1824             (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1825                 isl_token_free(tok);
1826                 map = read_disjuncts(s, v, map, rational);
1827         } else
1828                 isl_stream_push_token(s, tok);
1829
1830         return map;
1831 error:
1832         isl_map_free(map);
1833         return NULL;
1834 }
1835
1836 static struct isl_obj obj_read_poly(struct isl_stream *s,
1837         __isl_take isl_map *map, struct vars *v, int n)
1838 {
1839         struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1840         isl_pw_qpolynomial *pwqp;
1841         struct isl_set *set;
1842
1843         pwqp = read_term(s, map, v);
1844         map = read_optional_disjuncts(s, map, v, 0);
1845         set = isl_map_range(map);
1846
1847         pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1848
1849         vars_drop(v, v->n - n);
1850
1851         obj.v = pwqp;
1852         return obj;
1853 }
1854
1855 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1856         __isl_take isl_set *set, struct vars *v, int n)
1857 {
1858         struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1859         isl_pw_qpolynomial *pwqp;
1860         isl_pw_qpolynomial_fold *pwf = NULL;
1861
1862         if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1863                 return obj_read_poly(s, set, v, n);
1864
1865         if (isl_stream_eat(s, '('))
1866                 goto error;
1867
1868         pwqp = read_term(s, set, v);
1869         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1870
1871         while (isl_stream_eat_if_available(s, ',')) {
1872                 isl_pw_qpolynomial_fold *pwf_i;
1873                 pwqp = read_term(s, set, v);
1874                 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1875                                                                         pwqp);
1876                 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1877         }
1878
1879         if (isl_stream_eat(s, ')'))
1880                 goto error;
1881
1882         set = read_optional_disjuncts(s, set, v, 0);
1883         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1884
1885         vars_drop(v, v->n - n);
1886
1887         obj.v = pwf;
1888         return obj;
1889 error:
1890         isl_set_free(set);
1891         isl_pw_qpolynomial_fold_free(pwf);
1892         obj.type = isl_obj_none;
1893         return obj;
1894 }
1895
1896 static int is_rational(struct isl_stream *s)
1897 {
1898         struct isl_token *tok;
1899
1900         tok = isl_stream_next_token(s);
1901         if (!tok)
1902                 return 0;
1903         if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1904                 isl_token_free(tok);
1905                 isl_stream_eat(s, ':');
1906                 return 1;
1907         }
1908
1909         isl_stream_push_token(s, tok);
1910
1911         return 0;
1912 }
1913
1914 static struct isl_obj obj_read_body(struct isl_stream *s,
1915         __isl_take isl_map *map, struct vars *v)
1916 {
1917         struct isl_token *tok;
1918         struct isl_obj obj = { isl_obj_set, NULL };
1919         int n = v->n;
1920         int rational;
1921
1922         rational = is_rational(s);
1923         if (rational)
1924                 map = isl_map_set_rational(map);
1925
1926         if (isl_stream_next_token_is(s, ':')) {
1927                 obj.type = isl_obj_set;
1928                 obj.v = read_optional_disjuncts(s, map, v, rational);
1929                 return obj;
1930         }
1931
1932         if (!next_is_tuple(s))
1933                 return obj_read_poly_or_fold(s, map, v, n);
1934
1935         map = read_map_tuple(s, map, isl_dim_in, v, rational);
1936         if (!map)
1937                 goto error;
1938         tok = isl_stream_next_token(s);
1939         if (!tok)
1940                 goto error;
1941         if (tok->type == ISL_TOKEN_TO) {
1942                 obj.type = isl_obj_map;
1943                 isl_token_free(tok);
1944                 if (!next_is_tuple(s)) {
1945                         isl_set *set = isl_map_domain(map);
1946                         return obj_read_poly_or_fold(s, set, v, n);
1947                 }
1948                 map = read_map_tuple(s, map, isl_dim_out, v, rational);
1949                 if (!map)
1950                         goto error;
1951         } else {
1952                 map = isl_map_domain(map);
1953                 isl_stream_push_token(s, tok);
1954         }
1955
1956         map = read_optional_disjuncts(s, map, v, rational);
1957
1958         vars_drop(v, v->n - n);
1959
1960         obj.v = map;
1961         return obj;
1962 error:
1963         isl_map_free(map);
1964         obj.type = isl_obj_none;
1965         return obj;
1966 }
1967
1968 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1969 {
1970         if (obj.type == isl_obj_map) {
1971                 obj.v = isl_union_map_from_map(obj.v);
1972                 obj.type = isl_obj_union_map;
1973         } else if (obj.type == isl_obj_set) {
1974                 obj.v = isl_union_set_from_set(obj.v);
1975                 obj.type = isl_obj_union_set;
1976         } else if (obj.type == isl_obj_pw_qpolynomial) {
1977                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1978                 obj.type = isl_obj_union_pw_qpolynomial;
1979         } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1980                 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1981                 obj.type = isl_obj_union_pw_qpolynomial_fold;
1982         } else
1983                 isl_assert(ctx, 0, goto error);
1984         return obj;
1985 error:
1986         obj.type->free(obj.v);
1987         obj.type = isl_obj_none;
1988         return obj;
1989 }
1990
1991 static struct isl_obj obj_add(struct isl_ctx *ctx,
1992         struct isl_obj obj1, struct isl_obj obj2)
1993 {
1994         if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1995                 obj1 = to_union(ctx, obj1);
1996         if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1997                 obj2 = to_union(ctx, obj2);
1998         if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1999                 obj1 = to_union(ctx, obj1);
2000         if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2001                 obj2 = to_union(ctx, obj2);
2002         if (obj1.type == isl_obj_pw_qpolynomial &&
2003             obj2.type == isl_obj_union_pw_qpolynomial)
2004                 obj1 = to_union(ctx, obj1);
2005         if (obj1.type == isl_obj_union_pw_qpolynomial &&
2006             obj2.type == isl_obj_pw_qpolynomial)
2007                 obj2 = to_union(ctx, obj2);
2008         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2009             obj2.type == isl_obj_union_pw_qpolynomial_fold)
2010                 obj1 = to_union(ctx, obj1);
2011         if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2012             obj2.type == isl_obj_pw_qpolynomial_fold)
2013                 obj2 = to_union(ctx, obj2);
2014         isl_assert(ctx, obj1.type == obj2.type, goto error);
2015         if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2016                 obj1 = to_union(ctx, obj1);
2017                 obj2 = to_union(ctx, obj2);
2018         }
2019         if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2020                 obj1 = to_union(ctx, obj1);
2021                 obj2 = to_union(ctx, obj2);
2022         }
2023         if (obj1.type == isl_obj_pw_qpolynomial &&
2024             !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2025                 obj1 = to_union(ctx, obj1);
2026                 obj2 = to_union(ctx, obj2);
2027         }
2028         if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2029             !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2030                 obj1 = to_union(ctx, obj1);
2031                 obj2 = to_union(ctx, obj2);
2032         }
2033         obj1.v = obj1.type->add(obj1.v, obj2.v);
2034         return obj1;
2035 error:
2036         obj1.type->free(obj1.v);
2037         obj2.type->free(obj2.v);
2038         obj1.type = isl_obj_none;
2039         obj1.v = NULL;
2040         return obj1;
2041 }
2042
2043 static struct isl_obj obj_read(struct isl_stream *s)
2044 {
2045         isl_map *map = NULL;
2046         struct isl_token *tok;
2047         struct vars *v = NULL;
2048         struct isl_obj obj = { isl_obj_set, NULL };
2049
2050         tok = next_token(s);
2051         if (!tok) {
2052                 isl_stream_error(s, NULL, "unexpected EOF");
2053                 goto error;
2054         }
2055         if (tok->type == ISL_TOKEN_VALUE) {
2056                 struct isl_token *tok2;
2057                 struct isl_map *map;
2058
2059                 tok2 = isl_stream_next_token(s);
2060                 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2061                     isl_int_is_neg(tok2->u.v)) {
2062                         if (tok2)
2063                                 isl_stream_push_token(s, tok2);
2064                         obj.type = isl_obj_int;
2065                         obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2066                         isl_token_free(tok);
2067                         return obj;
2068                 }
2069                 isl_stream_push_token(s, tok2);
2070                 isl_stream_push_token(s, tok);
2071                 map = map_read_polylib(s);
2072                 if (!map)
2073                         goto error;
2074                 if (isl_map_may_be_set(map))
2075                         obj.v = isl_map_range(map);
2076                 else {
2077                         obj.type = isl_obj_map;
2078                         obj.v = map;
2079                 }
2080                 return obj;
2081         }
2082         v = vars_new(s->ctx);
2083         if (!v) {
2084                 isl_stream_push_token(s, tok);
2085                 goto error;
2086         }
2087         map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2088         if (tok->type == '[') {
2089                 isl_stream_push_token(s, tok);
2090                 map = read_map_tuple(s, map, isl_dim_param, v, 0);
2091                 if (!map)
2092                         goto error;
2093                 tok = isl_stream_next_token(s);
2094                 if (!tok || tok->type != ISL_TOKEN_TO) {
2095                         isl_stream_error(s, tok, "expecting '->'");
2096                         if (tok)
2097                                 isl_stream_push_token(s, tok);
2098                         goto error;
2099                 }
2100                 isl_token_free(tok);
2101                 tok = isl_stream_next_token(s);
2102         }
2103         if (!tok || tok->type != '{') {
2104                 isl_stream_error(s, tok, "expecting '{'");
2105                 if (tok)
2106                         isl_stream_push_token(s, tok);
2107                 goto error;
2108         }
2109         isl_token_free(tok);
2110
2111         tok = isl_stream_next_token(s);
2112         if (!tok)
2113                 ;
2114         else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2115                 isl_token_free(tok);
2116                 if (isl_stream_eat(s, '='))
2117                         goto error;
2118                 map = read_map_tuple(s, map, isl_dim_param, v, 0);
2119                 if (!map)
2120                         goto error;
2121         } else if (tok->type == '}') {
2122                 obj.type = isl_obj_union_set;
2123                 obj.v = isl_union_set_empty(isl_map_get_space(map));
2124                 isl_token_free(tok);
2125                 goto done;
2126         } else
2127                 isl_stream_push_token(s, tok);
2128
2129         for (;;) {
2130                 struct isl_obj o;
2131                 tok = NULL;
2132                 o = obj_read_body(s, isl_map_copy(map), v);
2133                 if (o.type == isl_obj_none || !o.v)
2134                         goto error;
2135                 if (!obj.v)
2136                         obj = o;
2137                 else {
2138                         obj = obj_add(s->ctx, obj, o);
2139                         if (obj.type == isl_obj_none || !obj.v)
2140                                 goto error;
2141                 }
2142                 tok = isl_stream_next_token(s);
2143                 if (!tok || tok->type != ';')
2144                         break;
2145                 isl_token_free(tok);
2146                 if (isl_stream_next_token_is(s, '}')) {
2147                         tok = isl_stream_next_token(s);
2148                         break;
2149                 }
2150         }
2151
2152         if (tok && tok->type == '}') {
2153                 isl_token_free(tok);
2154         } else {
2155                 isl_stream_error(s, tok, "unexpected isl_token");
2156                 if (tok)
2157                         isl_token_free(tok);
2158                 goto error;
2159         }
2160 done:
2161         vars_free(v);
2162         isl_map_free(map);
2163
2164         return obj;
2165 error:
2166         isl_map_free(map);
2167         obj.type->free(obj.v);
2168         if (v)
2169                 vars_free(v);
2170         obj.v = NULL;
2171         return obj;
2172 }
2173
2174 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2175 {
2176         return obj_read(s);
2177 }
2178
2179 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2180 {
2181         struct isl_obj obj;
2182
2183         obj = obj_read(s);
2184         if (obj.v)
2185                 isl_assert(s->ctx, obj.type == isl_obj_map ||
2186                                    obj.type == isl_obj_set, goto error);
2187         
2188         if (obj.type == isl_obj_set)
2189                 obj.v = isl_map_from_range(obj.v);
2190
2191         return obj.v;
2192 error:
2193         obj.type->free(obj.v);
2194         return NULL;
2195 }
2196
2197 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2198 {
2199         struct isl_obj obj;
2200
2201         obj = obj_read(s);
2202         if (obj.v) {
2203                 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2204                         obj.v = isl_map_range(obj.v);
2205                         obj.type = isl_obj_set;
2206                 }
2207                 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2208         }
2209
2210         return obj.v;
2211 error:
2212         obj.type->free(obj.v);
2213         return NULL;
2214 }
2215
2216 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2217 {
2218         struct isl_obj obj;
2219
2220         obj = obj_read(s);
2221         if (obj.type == isl_obj_map) {
2222                 obj.type = isl_obj_union_map;
2223                 obj.v = isl_union_map_from_map(obj.v);
2224         }
2225         if (obj.type == isl_obj_set) {
2226                 obj.type = isl_obj_union_set;
2227                 obj.v = isl_union_set_from_set(obj.v);
2228         }
2229         if (obj.v && obj.type == isl_obj_union_set &&
2230             isl_union_set_is_empty(obj.v))
2231                 obj.type = isl_obj_union_map;
2232         if (obj.v && obj.type != isl_obj_union_map)
2233                 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2234
2235         return obj.v;
2236 error:
2237         obj.type->free(obj.v);
2238         return NULL;
2239 }
2240
2241 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2242 {
2243         struct isl_obj obj;
2244
2245         obj = obj_read(s);
2246         if (obj.type == isl_obj_set) {
2247                 obj.type = isl_obj_union_set;
2248                 obj.v = isl_union_set_from_set(obj.v);
2249         }
2250         if (obj.v)
2251                 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2252
2253         return obj.v;
2254 error:
2255         obj.type->free(obj.v);
2256         return NULL;
2257 }
2258
2259 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2260 {
2261         struct isl_obj obj;
2262         struct isl_map *map;
2263         struct isl_basic_map *bmap;
2264
2265         obj = obj_read(s);
2266         map = obj.v;
2267         if (!map)
2268                 return NULL;
2269
2270         isl_assert(map->ctx, map->n <= 1, goto error);
2271
2272         if (map->n == 0)
2273                 bmap = isl_basic_map_empty_like_map(map);
2274         else
2275                 bmap = isl_basic_map_copy(map->p[0]);
2276
2277         isl_map_free(map);
2278
2279         return bmap;
2280 error:
2281         isl_map_free(map);
2282         return NULL;
2283 }
2284
2285 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2286 {
2287         isl_basic_map *bmap;
2288         bmap = basic_map_read(s);
2289         if (!bmap)
2290                 return NULL;
2291         if (!isl_basic_map_may_be_set(bmap))
2292                 isl_die(s->ctx, isl_error_invalid,
2293                         "input is not a set", goto error);
2294         return isl_basic_map_range(bmap);
2295 error:
2296         isl_basic_map_free(bmap);
2297         return NULL;
2298 }
2299
2300 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2301         FILE *input)
2302 {
2303         struct isl_basic_map *bmap;
2304         struct isl_stream *s = isl_stream_new_file(ctx, input);
2305         if (!s)
2306                 return NULL;
2307         bmap = basic_map_read(s);
2308         isl_stream_free(s);
2309         return bmap;
2310 }
2311
2312 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2313         FILE *input)
2314 {
2315         isl_basic_set *bset;
2316         struct isl_stream *s = isl_stream_new_file(ctx, input);
2317         if (!s)
2318                 return NULL;
2319         bset = basic_set_read(s);
2320         isl_stream_free(s);
2321         return bset;
2322 }
2323
2324 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2325         const char *str)
2326 {
2327         struct isl_basic_map *bmap;
2328         struct isl_stream *s = isl_stream_new_str(ctx, str);
2329         if (!s)
2330                 return NULL;
2331         bmap = basic_map_read(s);
2332         isl_stream_free(s);
2333         return bmap;
2334 }
2335
2336 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2337         const char *str)
2338 {
2339         isl_basic_set *bset;
2340         struct isl_stream *s = isl_stream_new_str(ctx, str);
2341         if (!s)
2342                 return NULL;
2343         bset = basic_set_read(s);
2344         isl_stream_free(s);
2345         return bset;
2346 }
2347
2348 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2349         FILE *input)
2350 {
2351         struct isl_map *map;
2352         struct isl_stream *s = isl_stream_new_file(ctx, input);
2353         if (!s)
2354                 return NULL;
2355         map = isl_stream_read_map(s);
2356         isl_stream_free(s);
2357         return map;
2358 }
2359
2360 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2361         const char *str)
2362 {
2363         struct isl_map *map;
2364         struct isl_stream *s = isl_stream_new_str(ctx, str);
2365         if (!s)
2366                 return NULL;
2367         map = isl_stream_read_map(s);
2368         isl_stream_free(s);
2369         return map;
2370 }
2371
2372 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2373         FILE *input)
2374 {
2375         isl_set *set;
2376         struct isl_stream *s = isl_stream_new_file(ctx, input);
2377         if (!s)
2378                 return NULL;
2379         set = isl_stream_read_set(s);
2380         isl_stream_free(s);
2381         return set;
2382 }
2383
2384 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2385         const char *str)
2386 {
2387         isl_set *set;
2388         struct isl_stream *s = isl_stream_new_str(ctx, str);
2389         if (!s)
2390                 return NULL;
2391         set = isl_stream_read_set(s);
2392         isl_stream_free(s);
2393         return set;
2394 }
2395
2396 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2397         FILE *input)
2398 {
2399         isl_union_map *umap;
2400         struct isl_stream *s = isl_stream_new_file(ctx, input);
2401         if (!s)
2402                 return NULL;
2403         umap = isl_stream_read_union_map(s);
2404         isl_stream_free(s);
2405         return umap;
2406 }
2407
2408 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2409                 const char *str)
2410 {
2411         isl_union_map *umap;
2412         struct isl_stream *s = isl_stream_new_str(ctx, str);
2413         if (!s)
2414                 return NULL;
2415         umap = isl_stream_read_union_map(s);
2416         isl_stream_free(s);
2417         return umap;
2418 }
2419
2420 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2421         FILE *input)
2422 {
2423         isl_union_set *uset;
2424         struct isl_stream *s = isl_stream_new_file(ctx, input);
2425         if (!s)
2426                 return NULL;
2427         uset = isl_stream_read_union_set(s);
2428         isl_stream_free(s);
2429         return uset;
2430 }
2431
2432 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2433                 const char *str)
2434 {
2435         isl_union_set *uset;
2436         struct isl_stream *s = isl_stream_new_str(ctx, str);
2437         if (!s)
2438                 return NULL;
2439         uset = isl_stream_read_union_set(s);
2440         isl_stream_free(s);
2441         return uset;
2442 }
2443
2444 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2445 {
2446         struct isl_vec *vec = NULL;
2447         struct isl_token *tok;
2448         unsigned size;
2449         int j;
2450
2451         tok = isl_stream_next_token(s);
2452         if (!tok || tok->type != ISL_TOKEN_VALUE) {
2453                 isl_stream_error(s, tok, "expecting vector length");
2454                 goto error;
2455         }
2456
2457         size = isl_int_get_si(tok->u.v);
2458         isl_token_free(tok);
2459
2460         vec = isl_vec_alloc(s->ctx, size);
2461
2462         for (j = 0; j < size; ++j) {
2463                 tok = isl_stream_next_token(s);
2464                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2465                         isl_stream_error(s, tok, "expecting constant value");
2466                         goto error;
2467                 }
2468                 isl_int_set(vec->el[j], tok->u.v);
2469                 isl_token_free(tok);
2470         }
2471
2472         return vec;
2473 error:
2474         isl_token_free(tok);
2475         isl_vec_free(vec);
2476         return NULL;
2477 }
2478
2479 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2480 {
2481         return isl_vec_read_polylib(s);
2482 }
2483
2484 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2485 {
2486         isl_vec *v;
2487         struct isl_stream *s = isl_stream_new_file(ctx, input);
2488         if (!s)
2489                 return NULL;
2490         v = vec_read(s);
2491         isl_stream_free(s);
2492         return v;
2493 }
2494
2495 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2496         struct isl_stream *s)
2497 {
2498         struct isl_obj obj;
2499
2500         obj = obj_read(s);
2501         if (obj.v)
2502                 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2503                            goto error);
2504
2505         return obj.v;
2506 error:
2507         obj.type->free(obj.v);
2508         return NULL;
2509 }
2510
2511 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2512                 const char *str)
2513 {
2514         isl_pw_qpolynomial *pwqp;
2515         struct isl_stream *s = isl_stream_new_str(ctx, str);
2516         if (!s)
2517                 return NULL;
2518         pwqp = isl_stream_read_pw_qpolynomial(s);
2519         isl_stream_free(s);
2520         return pwqp;
2521 }
2522
2523 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2524                 FILE *input)
2525 {
2526         isl_pw_qpolynomial *pwqp;
2527         struct isl_stream *s = isl_stream_new_file(ctx, input);
2528         if (!s)
2529                 return NULL;
2530         pwqp = isl_stream_read_pw_qpolynomial(s);
2531         isl_stream_free(s);
2532         return pwqp;
2533 }
2534
2535 /* Is the next token an identifer not in "v"?
2536  */
2537 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2538 {
2539         int n = v->n;
2540         int fresh;
2541         struct isl_token *tok;
2542
2543         tok = isl_stream_next_token(s);
2544         if (!tok)
2545                 return 0;
2546         fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2547         isl_stream_push_token(s, tok);
2548
2549         vars_drop(v, v->n - n);
2550
2551         return fresh;
2552 }
2553
2554 /* First read the domain of the affine expression, which may be
2555  * a parameter space or a set.
2556  * The tricky part is that we don't know if the domain is a set or not,
2557  * so when we are trying to read the domain, we may actually be reading
2558  * the affine expression itself (defined on a parameter domains)
2559  * If the tuple we are reading is named, we assume it's the domain.
2560  * Also, if inside the tuple, the first thing we find is a nested tuple
2561  * or a new identifier, we again assume it's the domain.
2562  * Otherwise, we assume we are reading an affine expression.
2563  */
2564 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2565         __isl_take isl_set *dom, struct vars *v)
2566 {
2567         struct isl_token *tok;
2568
2569         tok = isl_stream_next_token(s);
2570         if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2571                 isl_stream_push_token(s, tok);
2572                 return read_map_tuple(s, dom, isl_dim_set, v, 1);
2573         }
2574         if (!tok || tok->type != '[') {
2575                 isl_stream_error(s, tok, "expecting '['");
2576                 goto error;
2577         }
2578         if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2579                 isl_stream_push_token(s, tok);
2580                 dom = read_map_tuple(s, dom, isl_dim_set, v, 1);
2581         } else
2582                 isl_stream_push_token(s, tok);
2583
2584         return dom;
2585 error:
2586         if (tok)
2587                 isl_stream_push_token(s, tok);
2588         vars_free(v);
2589         isl_set_free(dom);
2590         return NULL;
2591 }
2592
2593 /* Read an affine expression from "s".
2594  */
2595 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2596 {
2597         isl_aff *aff;
2598         isl_multi_aff *ma;
2599
2600         ma = isl_stream_read_multi_aff(s);
2601         if (!ma)
2602                 return NULL;
2603         if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2604                 isl_die(s->ctx, isl_error_invalid,
2605                         "expecting single affine expression",
2606                         goto error);
2607
2608         aff = isl_multi_aff_get_aff(ma, 0);
2609         isl_multi_aff_free(ma);
2610         return aff;
2611 error:
2612         isl_multi_aff_free(ma);
2613         return NULL;
2614 }
2615
2616 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2617  */
2618 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2619         __isl_take isl_set *dom, struct vars *v)
2620 {
2621         isl_pw_aff *pwaff = NULL;
2622
2623         if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2624                 goto error;
2625
2626         if (isl_stream_eat(s, '['))
2627                 goto error;
2628
2629         pwaff = accept_affine(s, isl_set_get_space(dom), v);
2630
2631         if (isl_stream_eat(s, ']'))
2632                 goto error;
2633
2634         dom = read_optional_disjuncts(s, dom, v, 0);
2635         pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2636
2637         return pwaff;
2638 error:
2639         isl_set_free(dom);
2640         isl_pw_aff_free(pwaff);
2641         return NULL;
2642 }
2643
2644 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2645 {
2646         struct vars *v;
2647         isl_set *dom = NULL;
2648         isl_set *aff_dom;
2649         isl_pw_aff *pa = NULL;
2650         int n;
2651
2652         v = vars_new(s->ctx);
2653         if (!v)
2654                 return NULL;
2655
2656         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2657         if (next_is_tuple(s)) {
2658                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1);
2659                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2660                         goto error;
2661         }
2662         if (isl_stream_eat(s, '{'))
2663                 goto error;
2664
2665         n = v->n;
2666         aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2667         pa = read_pw_aff_with_dom(s, aff_dom, v);
2668         vars_drop(v, v->n - n);
2669
2670         while (isl_stream_eat_if_available(s, ';')) {
2671                 isl_pw_aff *pa_i;
2672
2673                 n = v->n;
2674                 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2675                 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2676                 vars_drop(v, v->n - n);
2677
2678                 pa = isl_pw_aff_union_add(pa, pa_i);
2679         }
2680
2681         if (isl_stream_eat(s, '}'))
2682                 goto error;
2683
2684         vars_free(v);
2685         isl_set_free(dom);
2686         return pa;
2687 error:
2688         vars_free(v);
2689         isl_set_free(dom);
2690         isl_pw_aff_free(pa);
2691         return NULL;
2692 }
2693
2694 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2695 {
2696         isl_aff *aff;
2697         struct isl_stream *s = isl_stream_new_str(ctx, str);
2698         if (!s)
2699                 return NULL;
2700         aff = isl_stream_read_aff(s);
2701         isl_stream_free(s);
2702         return aff;
2703 }
2704
2705 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2706 {
2707         isl_pw_aff *pa;
2708         struct isl_stream *s = isl_stream_new_str(ctx, str);
2709         if (!s)
2710                 return NULL;
2711         pa = isl_stream_read_pw_aff(s);
2712         isl_stream_free(s);
2713         return pa;
2714 }
2715
2716 /* Read an isl_pw_multi_aff from "s".
2717  * We currently read a generic object and if it turns out to be a set or
2718  * a map, we convert that to an isl_pw_multi_aff.
2719  * It would be more efficient if we were to construct the isl_pw_multi_aff
2720  * directly.
2721  */
2722 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2723 {
2724         struct isl_obj obj;
2725
2726         obj = obj_read(s);
2727         if (!obj.v)
2728                 return NULL;
2729
2730         if (obj.type == isl_obj_map)
2731                 return isl_pw_multi_aff_from_map(obj.v);
2732         if (obj.type == isl_obj_set)
2733                 return isl_pw_multi_aff_from_set(obj.v);
2734
2735         obj.type->free(obj.v);
2736         isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2737                 return NULL);
2738 }
2739
2740 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2741         const char *str)
2742 {
2743         isl_pw_multi_aff *pma;
2744         struct isl_stream *s = isl_stream_new_str(ctx, str);
2745         if (!s)
2746                 return NULL;
2747         pma = isl_stream_read_pw_multi_aff(s);
2748         isl_stream_free(s);
2749         return pma;
2750 }
2751
2752 /* Assuming "pa" represents a single affine expression defined on a universe
2753  * domain, extract this affine expression.
2754  */
2755 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2756 {
2757         isl_aff *aff;
2758
2759         if (!pa)
2760                 return NULL;
2761         if (pa->n != 1)
2762                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2763                         "expecting single affine expression",
2764                         goto error);
2765         if (!isl_set_plain_is_universe(pa->p[0].set))
2766                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2767                         "expecting universe domain",
2768                         goto error);
2769
2770         aff = isl_aff_copy(pa->p[0].aff);
2771         isl_pw_aff_free(pa);
2772         return aff;
2773 error:
2774         isl_pw_aff_free(pa);
2775         return NULL;
2776 }
2777
2778 /* Read a multi-affine expression from "s".
2779  * If the multi-affine expression has a domain, then then tuple
2780  * representing this domain cannot involve any affine expressions.
2781  * The tuple representing the actual expressions needs to consist
2782  * of only affine expressions.  Moreover, these expressions can
2783  * only depend on parameters and input dimensions and not on other
2784  * output dimensions.
2785  */
2786 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2787 {
2788         struct vars *v;
2789         isl_set *dom = NULL;
2790         isl_multi_pw_aff *tuple = NULL;
2791         int dim, i, n;
2792         isl_space *space, *dom_space;
2793         isl_multi_aff *ma = NULL;
2794
2795         v = vars_new(s->ctx);
2796         if (!v)
2797                 return NULL;
2798
2799         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2800         if (next_is_tuple(s)) {
2801                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1);
2802                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2803                         goto error;
2804         }
2805         if (!isl_set_plain_is_universe(dom))
2806                 isl_die(s->ctx, isl_error_invalid,
2807                         "expecting universe parameter domain", goto error);
2808         if (isl_stream_eat(s, '{'))
2809                 goto error;
2810
2811         tuple = read_tuple(s, v, 0);
2812         if (!tuple)
2813                 goto error;
2814         if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2815                 isl_set *set;
2816                 isl_space *space;
2817                 int has_expr;
2818
2819                 has_expr = tuple_has_expr(tuple);
2820                 if (has_expr < 0)
2821                         goto error;
2822                 if (has_expr)
2823                         isl_die(s->ctx, isl_error_invalid,
2824                                 "expecting universe domain", goto error);
2825                 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2826                 set = isl_set_universe(space);
2827                 dom = isl_set_intersect_params(set, dom);
2828                 isl_multi_pw_aff_free(tuple);
2829                 tuple = read_tuple(s, v, 0);
2830                 if (!tuple)
2831                         goto error;
2832         }
2833
2834         if (isl_stream_eat(s, '}'))
2835                 goto error;
2836
2837         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2838         dim = isl_set_dim(dom, isl_dim_all);
2839         dom_space = isl_set_get_space(dom);
2840         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2841         space = isl_space_align_params(space, isl_space_copy(dom_space));
2842         if (!isl_space_is_params(dom_space))
2843                 space = isl_space_map_from_domain_and_range(
2844                                 isl_space_copy(dom_space), space);
2845         isl_space_free(dom_space);
2846         ma = isl_multi_aff_alloc(space);
2847
2848         for (i = 0; i < n; ++i) {
2849                 isl_pw_aff *pa;
2850                 isl_aff *aff;
2851                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2852                 aff = aff_from_pw_aff(pa);
2853                 if (!aff)
2854                         goto error;
2855                 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2856                         isl_aff_free(aff);
2857                         isl_die(s->ctx, isl_error_invalid,
2858                                 "not an affine expression", goto error);
2859                 }
2860                 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2861                 space = isl_multi_aff_get_domain_space(ma);
2862                 aff = isl_aff_reset_domain_space(aff, space);
2863                 ma = isl_multi_aff_set_aff(ma, i, aff);
2864         }
2865
2866         isl_multi_pw_aff_free(tuple);
2867         vars_free(v);
2868         isl_set_free(dom);
2869         return ma;
2870 error:
2871         isl_multi_pw_aff_free(tuple);
2872         vars_free(v);
2873         isl_set_free(dom);
2874         isl_multi_aff_free(ma);
2875         return NULL;
2876 }
2877
2878 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2879         const char *str)
2880 {
2881         isl_multi_aff *maff;
2882         struct isl_stream *s = isl_stream_new_str(ctx, str);
2883         if (!s)
2884                 return NULL;
2885         maff = isl_stream_read_multi_aff(s);
2886         isl_stream_free(s);
2887         return maff;
2888 }
2889
2890 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2891         struct isl_stream *s)
2892 {
2893         struct isl_obj obj;
2894
2895         obj = obj_read(s);
2896         if (obj.type == isl_obj_pw_qpolynomial) {
2897                 obj.type = isl_obj_union_pw_qpolynomial;
2898                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2899         }
2900         if (obj.v)
2901                 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2902                            goto error);
2903
2904         return obj.v;
2905 error:
2906         obj.type->free(obj.v);
2907         return NULL;
2908 }
2909
2910 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2911         isl_ctx *ctx, const char *str)
2912 {
2913         isl_union_pw_qpolynomial *upwqp;
2914         struct isl_stream *s = isl_stream_new_str(ctx, str);
2915         if (!s)
2916                 return NULL;
2917         upwqp = isl_stream_read_union_pw_qpolynomial(s);
2918         isl_stream_free(s);
2919         return upwqp;
2920 }