isl_basic_set_opt: avoid invalid access on error path
[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                         vars_drop(v, v->n - n);
347                         isl_stream_error(s, tok, "unknown identifier");
348                         goto error;
349                 }
350
351                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
352                 if (!aff)
353                         goto error;
354                 isl_int_set_si(aff->v->el[2 + pos], 1);
355                 res = isl_pw_aff_from_aff(aff);
356                 isl_token_free(tok);
357         } else if (tok->type == ISL_TOKEN_VALUE) {
358                 if (isl_stream_eat_if_available(s, '*')) {
359                         res = accept_affine_factor(s, isl_space_copy(dim), v);
360                         res = isl_pw_aff_scale(res, tok->u.v);
361                 } else {
362                         isl_local_space *ls;
363                         isl_aff *aff;
364                         ls = isl_local_space_from_space(isl_space_copy(dim));
365                         aff = isl_aff_zero_on_domain(ls);
366                         aff = isl_aff_add_constant(aff, tok->u.v);
367                         res = isl_pw_aff_from_aff(aff);
368                 }
369                 isl_token_free(tok);
370         } else if (tok->type == '(') {
371                 isl_token_free(tok);
372                 tok = NULL;
373                 res = accept_affine(s, isl_space_copy(dim), v);
374                 if (!res)
375                         goto error;
376                 if (isl_stream_eat(s, ')'))
377                         goto error;
378         } else if (tok->type == '[' ||
379                     tok->type == ISL_TOKEN_FLOORD ||
380                     tok->type == ISL_TOKEN_CEILD) {
381                 isl_stream_push_token(s, tok);
382                 tok = NULL;
383                 res = accept_div(s, isl_space_copy(dim), v);
384         } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
385                 isl_stream_push_token(s, tok);
386                 tok = NULL;
387                 res = accept_minmax(s, isl_space_copy(dim), v);
388         } else {
389                 isl_stream_error(s, tok, "expecting factor");
390                 goto error;
391         }
392         if (isl_stream_eat_if_available(s, '%') ||
393             isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
394                 isl_space_free(dim);
395                 return affine_mod(s, v, res);
396         }
397         if (isl_stream_eat_if_available(s, '*')) {
398                 isl_int f;
399                 isl_int_init(f);
400                 isl_int_set_si(f, 1);
401                 if (accept_cst_factor(s, &f) < 0) {
402                         isl_int_clear(f);
403                         goto error2;
404                 }
405                 res = isl_pw_aff_scale(res, f);
406                 isl_int_clear(f);
407         }
408         if (isl_stream_eat_if_available(s, '/')) {
409                 isl_int f;
410                 isl_int_init(f);
411                 isl_int_set_si(f, 1);
412                 if (accept_cst_factor(s, &f) < 0) {
413                         isl_int_clear(f);
414                         goto error2;
415                 }
416                 res = isl_pw_aff_scale_down(res, f);
417                 isl_int_clear(f);
418         }
419
420         isl_space_free(dim);
421         return res;
422 error:
423         isl_token_free(tok);
424 error2:
425         isl_pw_aff_free(res);
426         isl_space_free(dim);
427         return NULL;
428 }
429
430 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
431 {
432         isl_aff *aff;
433         isl_space *space;
434
435         space = isl_pw_aff_get_domain_space(pwaff);
436         aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
437         aff = isl_aff_add_constant(aff, v);
438
439         return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
440 }
441
442 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
443         __isl_take isl_space *dim, struct vars *v)
444 {
445         struct isl_token *tok = NULL;
446         isl_local_space *ls;
447         isl_pw_aff *res;
448         int sign = 1;
449
450         ls = isl_local_space_from_space(isl_space_copy(dim));
451         res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
452         if (!res)
453                 goto error;
454
455         for (;;) {
456                 tok = next_token(s);
457                 if (!tok) {
458                         isl_stream_error(s, NULL, "unexpected EOF");
459                         goto error;
460                 }
461                 if (tok->type == '-') {
462                         sign = -sign;
463                         isl_token_free(tok);
464                         continue;
465                 }
466                 if (tok->type == '(' || tok->type == '[' ||
467                     tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
468                     tok->type == ISL_TOKEN_FLOORD ||
469                     tok->type == ISL_TOKEN_CEILD ||
470                     tok->type == ISL_TOKEN_IDENT ||
471                     tok->type == ISL_TOKEN_AFF) {
472                         isl_pw_aff *term;
473                         isl_stream_push_token(s, tok);
474                         tok = NULL;
475                         term = accept_affine_factor(s, isl_space_copy(dim), v);
476                         if (sign < 0)
477                                 res = isl_pw_aff_sub(res, term);
478                         else
479                                 res = isl_pw_aff_add(res, term);
480                         if (!res)
481                                 goto error;
482                         sign = 1;
483                 } else if (tok->type == ISL_TOKEN_VALUE) {
484                         if (sign < 0)
485                                 isl_int_neg(tok->u.v, tok->u.v);
486                         if (isl_stream_eat_if_available(s, '*') ||
487                             isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
488                                 isl_pw_aff *term;
489                                 term = accept_affine_factor(s,
490                                                         isl_space_copy(dim), v);
491                                 term = isl_pw_aff_scale(term, tok->u.v);
492                                 res = isl_pw_aff_add(res, term);
493                                 if (!res)
494                                         goto error;
495                         } else {
496                                 res = add_cst(res, tok->u.v);
497                         }
498                         sign = 1;
499                 } else {
500                         isl_stream_error(s, tok, "unexpected isl_token");
501                         isl_stream_push_token(s, tok);
502                         isl_pw_aff_free(res);
503                         isl_space_free(dim);
504                         return NULL;
505                 }
506                 isl_token_free(tok);
507
508                 tok = next_token(s);
509                 if (tok && tok->type == '-') {
510                         sign = -sign;
511                         isl_token_free(tok);
512                 } else if (tok && tok->type == '+') {
513                         /* nothing */
514                         isl_token_free(tok);
515                 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
516                            isl_int_is_neg(tok->u.v)) {
517                         isl_stream_push_token(s, tok);
518                 } else {
519                         if (tok)
520                                 isl_stream_push_token(s, tok);
521                         break;
522                 }
523         }
524
525         isl_space_free(dim);
526         return res;
527 error:
528         isl_space_free(dim);
529         isl_token_free(tok);
530         isl_pw_aff_free(res);
531         return NULL;
532 }
533
534 static int is_comparator(struct isl_token *tok)
535 {
536         if (!tok)
537                 return 0;
538
539         switch (tok->type) {
540         case ISL_TOKEN_LT:
541         case ISL_TOKEN_GT:
542         case ISL_TOKEN_LE:
543         case ISL_TOKEN_GE:
544         case ISL_TOKEN_NE:
545         case '=':
546                 return 1;
547         default:
548                 return 0;
549         }
550 }
551
552 static struct isl_map *read_disjuncts(struct isl_stream *s,
553         struct vars *v, __isl_take isl_map *map, int rational);
554 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
555         __isl_take isl_space *dim, struct vars *v, int rational);
556
557 /* Accept a ternary operator, given the first argument.
558  */
559 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
560         __isl_take isl_map *cond, struct vars *v, int rational)
561 {
562         isl_space *dim;
563         isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
564
565         if (!cond)
566                 return NULL;
567
568         if (isl_stream_eat(s, '?'))
569                 goto error;
570
571         dim = isl_space_wrap(isl_map_get_space(cond));
572         pwaff1 = accept_extended_affine(s, dim, v, rational);
573         if (!pwaff1)
574                 goto error;
575
576         if (isl_stream_eat(s, ':'))
577                 goto error;
578
579         dim = isl_pw_aff_get_domain_space(pwaff1);
580         pwaff2 = accept_extended_affine(s, dim, v, rational);
581         if (!pwaff1)
582                 goto error;
583
584         pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
585         return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
586 error:
587         isl_map_free(cond);
588         isl_pw_aff_free(pwaff1);
589         isl_pw_aff_free(pwaff2);
590         return NULL;
591 }
592
593 /* Accept an affine expression that may involve ternary operators.
594  * We first read an affine expression.
595  * If it is not followed by a comparison operator, we simply return it.
596  * Otherwise, we assume the affine epxression is part of the first
597  * argument of a ternary operator and try to parse that.
598  */
599 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
600         __isl_take isl_space *dim, struct vars *v, int rational)
601 {
602         isl_space *space;
603         isl_map *cond;
604         isl_pw_aff *pwaff;
605         struct isl_token *tok;
606         int line = -1, col = -1;
607         int is_comp;
608
609         tok = isl_stream_next_token(s);
610         if (tok) {
611                 line = tok->line;
612                 col = tok->col;
613                 isl_stream_push_token(s, tok);
614         }
615
616         pwaff = accept_affine(s, dim, v);
617         if (rational)
618                 pwaff = isl_pw_aff_set_rational(pwaff);
619         if (!pwaff)
620                 return NULL;
621
622         tok = isl_stream_next_token(s);
623         if (!tok)
624                 return isl_pw_aff_free(pwaff);
625
626         is_comp = is_comparator(tok);
627         isl_stream_push_token(s, tok);
628         if (!is_comp)
629                 return pwaff;
630
631         tok = isl_token_new(s->ctx, line, col, 0);
632         if (!tok)
633                 return isl_pw_aff_free(pwaff);
634         tok->type = ISL_TOKEN_AFF;
635         tok->u.pwaff = pwaff;
636
637         space = isl_pw_aff_get_domain_space(pwaff);
638         cond = isl_map_universe(isl_space_unwrap(space));
639
640         isl_stream_push_token(s, tok);
641
642         cond = read_disjuncts(s, v, cond, rational);
643
644         return accept_ternary(s, cond, v, rational);
645 }
646
647 static __isl_give isl_map *read_var_def(struct isl_stream *s,
648         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
649         int rational)
650 {
651         isl_pw_aff *def;
652         int pos;
653         isl_map *def_map;
654
655         if (type == isl_dim_param)
656                 pos = isl_map_dim(map, isl_dim_param);
657         else {
658                 pos = isl_map_dim(map, isl_dim_in);
659                 if (type == isl_dim_out)
660                         pos += isl_map_dim(map, isl_dim_out);
661                 type = isl_dim_in;
662         }
663         --pos;
664
665         def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
666                                         v, rational);
667         def_map = isl_map_from_pw_aff(def);
668         def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
669         def_map = isl_set_unwrap(isl_map_domain(def_map));
670
671         map = isl_map_intersect(map, def_map);
672
673         return map;
674 }
675
676 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
677         __isl_take isl_space *dim, struct vars *v)
678 {
679         isl_pw_aff *pwaff;
680         isl_pw_aff_list *list;
681         struct isl_token *tok = NULL;
682
683         pwaff = accept_affine(s, isl_space_copy(dim), v);
684         list = isl_pw_aff_list_from_pw_aff(pwaff);
685         if (!list)
686                 goto error;
687
688         for (;;) {
689                 tok = isl_stream_next_token(s);
690                 if (!tok) {
691                         isl_stream_error(s, NULL, "unexpected EOF");
692                         goto error;
693                 }
694                 if (tok->type != ',') {
695                         isl_stream_push_token(s, tok);
696                         break;
697                 }
698                 isl_token_free(tok);
699
700                 pwaff = accept_affine(s, isl_space_copy(dim), v);
701                 list = isl_pw_aff_list_concat(list,
702                                 isl_pw_aff_list_from_pw_aff(pwaff));
703                 if (!list)
704                         goto error;
705         }
706
707         isl_space_free(dim);
708         return list;
709 error:
710         isl_space_free(dim);
711         isl_pw_aff_list_free(list);
712         return NULL;
713 }
714
715 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
716         struct vars *v, __isl_take isl_map *map, int rational)
717 {
718         struct isl_token *tok;
719
720         while ((tok = isl_stream_next_token(s)) != NULL) {
721                 int p;
722                 int n = v->n;
723
724                 if (tok->type != ISL_TOKEN_IDENT)
725                         break;
726
727                 p = vars_pos(v, tok->u.s, -1);
728                 if (p < 0)
729                         goto error;
730                 if (p < n) {
731                         isl_stream_error(s, tok, "expecting unique identifier");
732                         goto error;
733                 }
734
735                 map = isl_map_add_dims(map, isl_dim_out, 1);
736
737                 isl_token_free(tok);
738                 tok = isl_stream_next_token(s);
739                 if (tok && tok->type == '=') {
740                         isl_token_free(tok);
741                         map = read_var_def(s, map, isl_dim_out, v, rational);
742                         tok = isl_stream_next_token(s);
743                 }
744
745                 if (!tok || tok->type != ',')
746                         break;
747
748                 isl_token_free(tok);
749         }
750         if (tok)
751                 isl_stream_push_token(s, tok);
752
753         return map;
754 error:
755         isl_token_free(tok);
756         isl_map_free(map);
757         return NULL;
758 }
759
760 static int next_is_tuple(struct isl_stream *s)
761 {
762         struct isl_token *tok;
763         int is_tuple;
764
765         tok = isl_stream_next_token(s);
766         if (!tok)
767                 return 0;
768         if (tok->type == '[') {
769                 isl_stream_push_token(s, tok);
770                 return 1;
771         }
772         if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
773                 isl_stream_push_token(s, tok);
774                 return 0;
775         }
776
777         is_tuple = isl_stream_next_token_is(s, '[');
778
779         isl_stream_push_token(s, tok);
780
781         return is_tuple;
782 }
783
784 /* Allocate an initial tuple with zero dimensions and an anonymous,
785  * unstructured space.
786  * A tuple is represented as an isl_multi_pw_aff.
787  * The range space is the space of the tuple.
788  * The domain space is an anonymous space
789  * with a dimension for each variable in the set of variables in "v".
790  * If a given dimension is not defined in terms of earlier dimensions in
791  * the input, then the corresponding isl_pw_aff is set equal to one time
792  * the variable corresponding to the dimension being defined.
793  */
794 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
795 {
796         return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
797 }
798
799 /* Is "pa" an expression in term of earlier dimensions?
800  * The alternative is that the dimension is defined to be equal to itself,
801  * meaning that it has a universe domain and an expression that depends
802  * on itself.  "i" is the position of the expression in a sequence
803  * of "n" expressions.  The final dimensions of "pa" correspond to
804  * these "n" expressions.
805  */
806 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
807 {
808         isl_aff *aff;
809
810         if (!pa)
811                 return -1;
812         if (pa->n != 1)
813                 return 1;
814         if (!isl_set_plain_is_universe(pa->p[0].set))
815                 return 1;
816
817         aff = pa->p[0].aff;
818         if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
819                 return 1;
820         return 0;
821 }
822
823 /* Does the tuple contain any dimensions that are defined
824  * in terms of earlier dimensions?
825  */
826 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
827 {
828         int i, n;
829         int has_expr = 0;
830         isl_pw_aff *pa;
831
832         if (!tuple)
833                 return -1;
834         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
835         for (i = 0; i < n; ++i) {
836                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
837                 has_expr = pw_aff_is_expr(pa, i, n);
838                 isl_pw_aff_free(pa);
839                 if (has_expr < 0 || has_expr)
840                         break;
841         }
842
843         return has_expr;
844 }
845
846 /* Add a dimension to the given tuple.
847  * The dimension is initially undefined, so it is encoded
848  * as one times itself.
849  */
850 static __isl_give isl_multi_pw_aff *tuple_add_dim(
851         __isl_take isl_multi_pw_aff *tuple, struct vars *v)
852 {
853         isl_space *space;
854         isl_aff *aff;
855         isl_pw_aff *pa;
856
857         tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
858         space = isl_multi_pw_aff_get_domain_space(tuple);
859         aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
860         aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
861         pa = isl_pw_aff_from_aff(aff);
862         tuple = isl_multi_pw_aff_flat_range_product(tuple,
863                                             isl_multi_pw_aff_from_pw_aff(pa));
864
865         return tuple;
866 }
867
868 /* Set the name of dimension "pos" in "tuple" to "name".
869  * During printing, we add primes if the same name appears more than once
870  * to distinguish the occurrences.  Here, we remove those primes from "name"
871  * before setting the name of the dimension.
872  */
873 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
874         __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
875 {
876         char *prime;
877
878         if (!name)
879                 return tuple;
880
881         prime = strchr(name, '\'');
882         if (prime)
883                 *prime = '\0';
884         tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
885         if (prime)
886                 *prime = '\'';
887
888         return tuple;
889 }
890
891 /* Read an affine expression from "s" and replace the definition
892  * of dimension "pos" in "tuple" by this expression.
893  *
894  * accept_extended_affine requires a wrapped space as input.
895  * The domain space of "tuple", on the other hand is an anonymous space,
896  * so we have to adjust the space of the isl_pw_aff before adding it
897  * to "tuple".
898  */
899 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
900         __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
901         int rational)
902 {
903         isl_space *space;
904         isl_pw_aff *def;
905
906         space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
907         def = accept_extended_affine(s, space, v, rational);
908         space = isl_space_set_alloc(s->ctx, 0, v->n);
909         def = isl_pw_aff_reset_domain_space(def, space);
910         tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
911
912         return tuple;
913 }
914
915 /* Read a list of variables and/or affine expressions and return the list
916  * as an isl_multi_pw_aff.
917  * The elements in the list are separated by either "," or "][".
918  * If "comma" is set then only "," is allowed.
919  */
920 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
921         struct vars *v, int rational, int comma)
922 {
923         int i = 0;
924         struct isl_token *tok;
925         isl_multi_pw_aff *res;
926
927         res = tuple_alloc(v);
928
929         if (isl_stream_next_token_is(s, ']'))
930                 return res;
931
932         while ((tok = next_token(s)) != NULL) {
933                 int new_name = 0;
934
935                 res = tuple_add_dim(res, v);
936
937                 if (tok->type == ISL_TOKEN_IDENT) {
938                         int n = v->n;
939                         int p = vars_pos(v, tok->u.s, -1);
940                         if (p < 0)
941                                 goto error;
942                         new_name = p >= n;
943                 }
944
945                 if (new_name) {
946                         res = tuple_set_dim_name(res, i, v->v->name);
947                         isl_token_free(tok);
948                         if (isl_stream_eat_if_available(s, '='))
949                                 res = read_tuple_var_def(s, res, i, v,
950                                                         rational);
951                 } else {
952                         isl_stream_push_token(s, tok);
953                         tok = NULL;
954                         if (vars_add_anon(v) < 0)
955                                 goto error;
956                         res = read_tuple_var_def(s, res, i, v, rational);
957                 }
958
959                 tok = isl_stream_next_token(s);
960                 if (!comma && tok && tok->type == ']' &&
961                     isl_stream_next_token_is(s, '[')) {
962                         isl_token_free(tok);
963                         tok = isl_stream_next_token(s);
964                 } else if (!tok || tok->type != ',')
965                         break;
966
967                 isl_token_free(tok);
968                 i++;
969         }
970         if (tok)
971                 isl_stream_push_token(s, tok);
972
973         return res;
974 error:
975         isl_token_free(tok);
976         return isl_multi_pw_aff_free(res);
977 }
978
979 /* Read a tuple and represent it as an isl_multi_pw_aff.  See tuple_alloc.
980  */
981 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
982         struct vars *v, int rational, int comma)
983 {
984         struct isl_token *tok;
985         char *name = NULL;
986         isl_multi_pw_aff *res = NULL;
987
988         tok = isl_stream_next_token(s);
989         if (!tok)
990                 goto error;
991         if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
992                 name = strdup(tok->u.s);
993                 isl_token_free(tok);
994                 if (!name)
995                         goto error;
996         } else
997                 isl_stream_push_token(s, tok);
998         if (isl_stream_eat(s, '['))
999                 goto error;
1000         if (next_is_tuple(s)) {
1001                 isl_multi_pw_aff *out;
1002                 int n;
1003                 res = read_tuple(s, v, rational, comma);
1004                 if (isl_stream_eat(s, ISL_TOKEN_TO))
1005                         goto error;
1006                 out = read_tuple(s, v, rational, comma);
1007                 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1008                 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1009                 res = isl_multi_pw_aff_range_product(res, out);
1010         } else
1011                 res = read_tuple_var_list(s, v, rational, comma);
1012         if (isl_stream_eat(s, ']'))
1013                 goto error;
1014
1015         if (name) {
1016                 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1017                 free(name);
1018         }
1019
1020         return res;
1021 error:
1022         free(name);
1023         return isl_multi_pw_aff_free(res);
1024 }
1025
1026 /* Read a tuple from "s" and add it to "map".
1027  * The tuple is initially represented as an isl_multi_pw_aff.
1028  * We first create the appropriate space in "map" based on the range
1029  * space of this isl_multi_pw_aff.  Then, we add equalities based
1030  * on the affine expressions.  These live in an anonymous space,
1031  * however, so we first need to reset the space to that of "map".
1032  */
1033 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1034         __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1035         int rational, int comma)
1036 {
1037         int i, n;
1038         isl_multi_pw_aff *tuple;
1039         isl_space *space = NULL;
1040
1041         tuple = read_tuple(s, v, rational, comma);
1042         if (!tuple)
1043                 goto error;
1044
1045         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1046         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1047         if (!space)
1048                 goto error;
1049
1050         if (type == isl_dim_param) {
1051                 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1052                     isl_space_is_wrapping(space)) {
1053                         isl_die(s->ctx, isl_error_invalid,
1054                                 "parameter tuples cannot be named or nested",
1055                                 goto error);
1056                 }
1057                 map = isl_map_add_dims(map, type, n);
1058                 for (i = 0; i < n; ++i) {
1059                         isl_id *id;
1060                         if (!isl_space_has_dim_name(space, isl_dim_set, i))
1061                                 isl_die(s->ctx, isl_error_invalid,
1062                                         "parameters must be named",
1063                                         goto error);
1064                         id = isl_space_get_dim_id(space, isl_dim_set, i);
1065                         map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1066                 }
1067         } else if (type == isl_dim_in) {
1068                 isl_set *set;
1069
1070                 set = isl_set_universe(isl_space_copy(space));
1071                 if (rational)
1072                         set = isl_set_set_rational(set);
1073                 set = isl_set_intersect_params(set, isl_map_params(map));
1074                 map = isl_map_from_domain(set);
1075         } else {
1076                 isl_set *set;
1077
1078                 set = isl_set_universe(isl_space_copy(space));
1079                 if (rational)
1080                         set = isl_set_set_rational(set);
1081                 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1082         }
1083
1084         for (i = 0; i < n; ++i) {
1085                 isl_pw_aff *pa;
1086                 isl_space *space;
1087                 isl_aff *aff;
1088                 isl_set *set;
1089                 isl_map *map_i;
1090
1091                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1092                 space = isl_pw_aff_get_domain_space(pa);
1093                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1094                 aff = isl_aff_add_coefficient_si(aff,
1095                                                 isl_dim_in, v->n - n + i, -1);
1096                 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1097                 if (rational)
1098                         pa = isl_pw_aff_set_rational(pa);
1099                 set = isl_pw_aff_zero_set(pa);
1100                 map_i = isl_map_from_range(set);
1101                 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1102                 map = isl_map_intersect(map, map_i);
1103         }
1104
1105         isl_space_free(space);
1106         isl_multi_pw_aff_free(tuple);
1107         return map;
1108 error:
1109         isl_space_free(space);
1110         isl_multi_pw_aff_free(tuple);
1111         isl_map_free(map);
1112         return NULL;
1113 }
1114
1115 static __isl_give isl_set *construct_constraints(
1116         __isl_take isl_set *set, int type,
1117         __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1118         int rational)
1119 {
1120         isl_set *cond;
1121
1122         left = isl_pw_aff_list_copy(left);
1123         right = isl_pw_aff_list_copy(right);
1124         if (rational) {
1125                 left = isl_pw_aff_list_set_rational(left);
1126                 right = isl_pw_aff_list_set_rational(right);
1127         }
1128         if (type == ISL_TOKEN_LE)
1129                 cond = isl_pw_aff_list_le_set(left, right);
1130         else if (type == ISL_TOKEN_GE)
1131                 cond = isl_pw_aff_list_ge_set(left, right);
1132         else if (type == ISL_TOKEN_LT)
1133                 cond = isl_pw_aff_list_lt_set(left, right);
1134         else if (type == ISL_TOKEN_GT)
1135                 cond = isl_pw_aff_list_gt_set(left, right);
1136         else if (type == ISL_TOKEN_NE)
1137                 cond = isl_pw_aff_list_ne_set(left, right);
1138         else
1139                 cond = isl_pw_aff_list_eq_set(left, 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, 0);
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, 0);
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, 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, 1);
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, 0);
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, 0);
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         isl_set_free(dom);
2589         return NULL;
2590 }
2591
2592 /* Read an affine expression from "s".
2593  */
2594 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2595 {
2596         isl_aff *aff;
2597         isl_multi_aff *ma;
2598
2599         ma = isl_stream_read_multi_aff(s);
2600         if (!ma)
2601                 return NULL;
2602         if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2603                 isl_die(s->ctx, isl_error_invalid,
2604                         "expecting single affine expression",
2605                         goto error);
2606
2607         aff = isl_multi_aff_get_aff(ma, 0);
2608         isl_multi_aff_free(ma);
2609         return aff;
2610 error:
2611         isl_multi_aff_free(ma);
2612         return NULL;
2613 }
2614
2615 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2616  */
2617 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2618         __isl_take isl_set *dom, struct vars *v)
2619 {
2620         isl_pw_aff *pwaff = NULL;
2621
2622         if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2623                 goto error;
2624
2625         if (isl_stream_eat(s, '['))
2626                 goto error;
2627
2628         pwaff = accept_affine(s, isl_set_get_space(dom), v);
2629
2630         if (isl_stream_eat(s, ']'))
2631                 goto error;
2632
2633         dom = read_optional_disjuncts(s, dom, v, 0);
2634         pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2635
2636         return pwaff;
2637 error:
2638         isl_set_free(dom);
2639         isl_pw_aff_free(pwaff);
2640         return NULL;
2641 }
2642
2643 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2644 {
2645         struct vars *v;
2646         isl_set *dom = NULL;
2647         isl_set *aff_dom;
2648         isl_pw_aff *pa = NULL;
2649         int n;
2650
2651         v = vars_new(s->ctx);
2652         if (!v)
2653                 return NULL;
2654
2655         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2656         if (next_is_tuple(s)) {
2657                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2658                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2659                         goto error;
2660         }
2661         if (isl_stream_eat(s, '{'))
2662                 goto error;
2663
2664         n = v->n;
2665         aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2666         pa = read_pw_aff_with_dom(s, aff_dom, v);
2667         vars_drop(v, v->n - n);
2668
2669         while (isl_stream_eat_if_available(s, ';')) {
2670                 isl_pw_aff *pa_i;
2671
2672                 n = v->n;
2673                 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2674                 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2675                 vars_drop(v, v->n - n);
2676
2677                 pa = isl_pw_aff_union_add(pa, pa_i);
2678         }
2679
2680         if (isl_stream_eat(s, '}'))
2681                 goto error;
2682
2683         vars_free(v);
2684         isl_set_free(dom);
2685         return pa;
2686 error:
2687         vars_free(v);
2688         isl_set_free(dom);
2689         isl_pw_aff_free(pa);
2690         return NULL;
2691 }
2692
2693 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2694 {
2695         isl_aff *aff;
2696         struct isl_stream *s = isl_stream_new_str(ctx, str);
2697         if (!s)
2698                 return NULL;
2699         aff = isl_stream_read_aff(s);
2700         isl_stream_free(s);
2701         return aff;
2702 }
2703
2704 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2705 {
2706         isl_pw_aff *pa;
2707         struct isl_stream *s = isl_stream_new_str(ctx, str);
2708         if (!s)
2709                 return NULL;
2710         pa = isl_stream_read_pw_aff(s);
2711         isl_stream_free(s);
2712         return pa;
2713 }
2714
2715 /* Read an isl_pw_multi_aff from "s".
2716  * We currently read a generic object and if it turns out to be a set or
2717  * a map, we convert that to an isl_pw_multi_aff.
2718  * It would be more efficient if we were to construct the isl_pw_multi_aff
2719  * directly.
2720  */
2721 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2722 {
2723         struct isl_obj obj;
2724
2725         obj = obj_read(s);
2726         if (!obj.v)
2727                 return NULL;
2728
2729         if (obj.type == isl_obj_map)
2730                 return isl_pw_multi_aff_from_map(obj.v);
2731         if (obj.type == isl_obj_set)
2732                 return isl_pw_multi_aff_from_set(obj.v);
2733
2734         obj.type->free(obj.v);
2735         isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2736                 return NULL);
2737 }
2738
2739 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2740         const char *str)
2741 {
2742         isl_pw_multi_aff *pma;
2743         struct isl_stream *s = isl_stream_new_str(ctx, str);
2744         if (!s)
2745                 return NULL;
2746         pma = isl_stream_read_pw_multi_aff(s);
2747         isl_stream_free(s);
2748         return pma;
2749 }
2750
2751 /* Assuming "pa" represents a single affine expression defined on a universe
2752  * domain, extract this affine expression.
2753  */
2754 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2755 {
2756         isl_aff *aff;
2757
2758         if (!pa)
2759                 return NULL;
2760         if (pa->n != 1)
2761                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2762                         "expecting single affine expression",
2763                         goto error);
2764         if (!isl_set_plain_is_universe(pa->p[0].set))
2765                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2766                         "expecting universe domain",
2767                         goto error);
2768
2769         aff = isl_aff_copy(pa->p[0].aff);
2770         isl_pw_aff_free(pa);
2771         return aff;
2772 error:
2773         isl_pw_aff_free(pa);
2774         return NULL;
2775 }
2776
2777 /* Read a multi-affine expression from "s".
2778  * If the multi-affine expression has a domain, then then tuple
2779  * representing this domain cannot involve any affine expressions.
2780  * The tuple representing the actual expressions needs to consist
2781  * of only affine expressions.  Moreover, these expressions can
2782  * only depend on parameters and input dimensions and not on other
2783  * output dimensions.
2784  */
2785 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2786 {
2787         struct vars *v;
2788         isl_set *dom = NULL;
2789         isl_multi_pw_aff *tuple = NULL;
2790         int dim, i, n;
2791         isl_space *space, *dom_space;
2792         isl_multi_aff *ma = NULL;
2793
2794         v = vars_new(s->ctx);
2795         if (!v)
2796                 return NULL;
2797
2798         dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2799         if (next_is_tuple(s)) {
2800                 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2801                 if (isl_stream_eat(s, ISL_TOKEN_TO))
2802                         goto error;
2803         }
2804         if (!isl_set_plain_is_universe(dom))
2805                 isl_die(s->ctx, isl_error_invalid,
2806                         "expecting universe parameter domain", goto error);
2807         if (isl_stream_eat(s, '{'))
2808                 goto error;
2809
2810         tuple = read_tuple(s, v, 0, 0);
2811         if (!tuple)
2812                 goto error;
2813         if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2814                 isl_set *set;
2815                 isl_space *space;
2816                 int has_expr;
2817
2818                 has_expr = tuple_has_expr(tuple);
2819                 if (has_expr < 0)
2820                         goto error;
2821                 if (has_expr)
2822                         isl_die(s->ctx, isl_error_invalid,
2823                                 "expecting universe domain", goto error);
2824                 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2825                 set = isl_set_universe(space);
2826                 dom = isl_set_intersect_params(set, dom);
2827                 isl_multi_pw_aff_free(tuple);
2828                 tuple = read_tuple(s, v, 0, 0);
2829                 if (!tuple)
2830                         goto error;
2831         }
2832
2833         if (isl_stream_eat(s, '}'))
2834                 goto error;
2835
2836         n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2837         dim = isl_set_dim(dom, isl_dim_all);
2838         dom_space = isl_set_get_space(dom);
2839         space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2840         space = isl_space_align_params(space, isl_space_copy(dom_space));
2841         if (!isl_space_is_params(dom_space))
2842                 space = isl_space_map_from_domain_and_range(
2843                                 isl_space_copy(dom_space), space);
2844         isl_space_free(dom_space);
2845         ma = isl_multi_aff_alloc(space);
2846
2847         for (i = 0; i < n; ++i) {
2848                 isl_pw_aff *pa;
2849                 isl_aff *aff;
2850                 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2851                 aff = aff_from_pw_aff(pa);
2852                 if (!aff)
2853                         goto error;
2854                 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2855                         isl_aff_free(aff);
2856                         isl_die(s->ctx, isl_error_invalid,
2857                                 "not an affine expression", goto error);
2858                 }
2859                 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2860                 space = isl_multi_aff_get_domain_space(ma);
2861                 aff = isl_aff_reset_domain_space(aff, space);
2862                 ma = isl_multi_aff_set_aff(ma, i, aff);
2863         }
2864
2865         isl_multi_pw_aff_free(tuple);
2866         vars_free(v);
2867         isl_set_free(dom);
2868         return ma;
2869 error:
2870         isl_multi_pw_aff_free(tuple);
2871         vars_free(v);
2872         isl_set_free(dom);
2873         isl_multi_aff_free(ma);
2874         return NULL;
2875 }
2876
2877 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
2878         const char *str)
2879 {
2880         isl_multi_aff *maff;
2881         struct isl_stream *s = isl_stream_new_str(ctx, str);
2882         if (!s)
2883                 return NULL;
2884         maff = isl_stream_read_multi_aff(s);
2885         isl_stream_free(s);
2886         return maff;
2887 }
2888
2889 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
2890         struct isl_stream *s)
2891 {
2892         struct isl_obj obj;
2893
2894         obj = obj_read(s);
2895         if (obj.type == isl_obj_pw_qpolynomial) {
2896                 obj.type = isl_obj_union_pw_qpolynomial;
2897                 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2898         }
2899         if (obj.v)
2900                 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
2901                            goto error);
2902
2903         return obj.v;
2904 error:
2905         obj.type->free(obj.v);
2906         return NULL;
2907 }
2908
2909 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
2910         isl_ctx *ctx, const char *str)
2911 {
2912         isl_union_pw_qpolynomial *upwqp;
2913         struct isl_stream *s = isl_stream_new_str(ctx, str);
2914         if (!s)
2915                 return NULL;
2916         upwqp = isl_stream_read_union_pw_qpolynomial(s);
2917         isl_stream_free(s);
2918         return upwqp;
2919 }