isl_input.c: fix memory deallocation problem on missing operator
[platform/upstream/isl.git] / isl_input.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <isl_set.h>
18 #include <isl_seq.h>
19 #include "isl_stream.h"
20 #include "isl_map_private.h"
21
22 struct variable {
23         char                    *name;
24         int                      pos;
25         struct variable         *next;
26 };
27
28 struct vars {
29         struct isl_ctx  *ctx;
30         int              n;
31         struct variable *v;
32 };
33
34 static struct vars *vars_new(struct isl_ctx *ctx)
35 {
36         struct vars *v;
37         v = isl_alloc_type(ctx, struct vars);
38         if (!v)
39                 return NULL;
40         v->ctx = ctx;
41         v->n = 0;
42         v->v = NULL;
43         return v;
44 }
45
46 static void variable_free(struct variable *var)
47 {
48         while (var) {
49                 struct variable *next = var->next;
50                 free(var->name);
51                 free(var);
52                 var = next;
53         }
54 }
55
56 static void vars_free(struct vars *v)
57 {
58         if (!v)
59                 return;
60         variable_free(v->v);
61         free(v);
62 }
63
64 static struct variable *variable_new(struct vars *v, const char *name, int len,
65                                 int pos)
66 {
67         struct variable *var;
68         var = isl_alloc_type(v->ctx, struct variable);
69         if (!var)
70                 goto error;
71         var->name = strdup(name);
72         var->name[len] = '\0';
73         var->pos = pos;
74         var->next = v->v;
75         return var;
76 error:
77         variable_free(v->v);
78         return NULL;
79 }
80
81 static int vars_pos(struct vars *v, const char *s, int len)
82 {
83         int pos;
84         struct variable *q;
85
86         if (len == -1)
87                 len = strlen(s);
88         for (q = v->v; q; q = q->next) {
89                 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
90                         break;
91         }
92         if (q)
93                 pos = q->pos;
94         else {
95                 pos = v->n;
96                 v->v = variable_new(v, s, len, v->n);
97                 if (!v->v)
98                         return -1;
99                 v->n++;
100         }
101         return pos;
102 }
103
104 static struct vars *read_var_list(struct isl_stream *s, struct vars *v)
105 {
106         struct isl_token *tok;
107
108         while ((tok = isl_stream_next_token(s)) != NULL) {
109                 int p;
110                 int n = v->n;
111
112                 if (tok->type != ISL_TOKEN_IDENT)
113                         break;
114
115                 p = vars_pos(v, tok->u.s, -1);
116                 if (p < 0)
117                         goto error;
118                 if (p < n) {
119                         isl_stream_error(s, tok, "expecting unique identifier");
120                         goto error;
121                 }
122                 isl_token_free(tok);
123                 tok = isl_stream_next_token(s);
124                 if (!tok || tok->type != ',')
125                         break;
126
127                 isl_token_free(tok);
128         }
129         if (tok)
130                 isl_stream_push_token(s, tok);
131
132         return v;
133 error:
134         isl_token_free(tok);
135         vars_free(v);
136         return NULL;
137 }
138
139 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
140 {
141         struct isl_token *tok = NULL;
142         struct isl_vec *aff;
143
144         aff = isl_vec_alloc(v->ctx, 1 + v->n);
145         isl_seq_clr(aff->el, aff->size);
146         if (!aff)
147                 return NULL;
148
149         for (;;) {
150                 tok = isl_stream_next_token(s);
151                 if (!tok) {
152                         isl_stream_error(s, NULL, "unexpected EOF");
153                         goto error;
154                 }
155                 if (tok->type == ISL_TOKEN_IDENT) {
156                         int n = v->n;
157                         int pos = vars_pos(v, tok->u.s, -1);
158                         if (pos < 0)
159                                 goto error;
160                         if (pos >= n) {
161                                 isl_stream_error(s, tok, "unknown identifier");
162                                 goto error;
163                         }
164                         isl_int_add_ui(aff->el[1 + pos], aff->el[1 + pos], 1);
165                 } else if (tok->type == ISL_TOKEN_VALUE) {
166                         struct isl_token *tok2;
167                         int n = v->n;
168                         int pos = -1;
169                         tok2 = isl_stream_next_token(s);
170                         if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
171                                 pos = vars_pos(v, tok2->u.s, -1);
172                                 if (pos < 0)
173                                         goto error;
174                                 if (pos >= n) {
175                                         isl_stream_error(s, tok2,
176                                                 "unknown identifier");
177                                         isl_token_free(tok2);
178                                         goto error;
179                                 }
180                                 isl_token_free(tok2);
181                         } else if (tok2)
182                                 isl_stream_push_token(s, tok2);
183                         isl_int_add(aff->el[1 + pos],
184                                         aff->el[1 + pos], tok->u.v);
185                 } else if (tok->type == '+') {
186                         /* nothing */
187                 } else {
188                         isl_stream_push_token(s, tok);
189                         break;
190                 }
191                 isl_token_free(tok);
192         }
193
194         return aff;
195 error:
196         isl_vec_free(aff);
197         return NULL;
198 }
199
200 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
201         struct vars *v, struct isl_basic_map *bmap, int k)
202 {
203         struct isl_token *tok;
204         int seen_paren = 0;
205         struct isl_vec *aff;
206
207         if (isl_stream_eat(s, '['))
208                 goto error;
209
210         tok = isl_stream_next_token(s);
211         if (!tok)
212                 goto error;
213         if (tok->type == '(') {
214                 seen_paren = 1;
215                 isl_token_free(tok);
216         } else
217                 isl_stream_push_token(s, tok);
218
219         aff = accept_affine(s, v);
220         if (!aff)
221                 goto error;
222
223         isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
224
225         isl_vec_free(aff);
226
227         if (seen_paren && isl_stream_eat(s, ')'))
228                 goto error;
229         if (isl_stream_eat(s, '/'))
230                 goto error;
231
232         tok = isl_stream_next_token(s);
233         if (!tok)
234                 goto error;
235         if (tok->type != ISL_TOKEN_VALUE) {
236                 isl_stream_error(s, tok, "expected denominator");
237                 isl_stream_push_token(s, tok);
238                 goto error;
239         }
240         isl_int_set(bmap->div[k][0], tok->u.v);
241         isl_token_free(tok);
242
243         if (isl_stream_eat(s, ']'))
244                 goto error;
245
246         if (isl_basic_map_add_div_constraints(bmap, k) < 0)
247                 goto error;
248
249         return bmap;
250 error:
251         isl_basic_map_free(bmap);
252         return NULL;
253 }
254
255 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
256         struct vars *v, struct isl_basic_map *bmap)
257 {
258         struct isl_token *tok;
259
260         while ((tok = isl_stream_next_token(s)) != NULL) {
261                 int k;
262                 int p;
263                 int n = v->n;
264                 unsigned total = isl_basic_map_total_dim(bmap);
265
266                 if (tok->type != ISL_TOKEN_IDENT)
267                         break;
268
269                 p = vars_pos(v, tok->u.s, -1);
270                 if (p < 0)
271                         goto error;
272                 if (p < n) {
273                         isl_stream_error(s, tok, "expecting unique identifier");
274                         goto error;
275                 }
276                 isl_token_free(tok);
277
278                 bmap = isl_basic_map_cow(bmap);
279                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
280                                                 1, 0, 2);
281
282                 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
283                         goto error;
284                 isl_seq_clr(bmap->div[k], 1 + 1 + total);
285
286                 tok = isl_stream_next_token(s);
287                 if (tok && tok->type == '=') {
288                         isl_token_free(tok);
289                         bmap = add_div_definition(s, v, bmap, k);
290                         tok = isl_stream_next_token(s);
291                 }
292
293                 if (!tok || tok->type != ',')
294                         break;
295
296                 isl_token_free(tok);
297         }
298         if (tok)
299                 isl_stream_push_token(s, tok);
300
301         return bmap;
302 error:
303         isl_token_free(tok);
304         isl_basic_map_free(bmap);
305         return NULL;
306 }
307
308 static struct vars *read_tuple(struct isl_stream *s, struct vars *v)
309 {
310         struct isl_token *tok;
311
312         tok = isl_stream_next_token(s);
313         if (!tok || tok->type != '[') {
314                 isl_stream_error(s, tok, "expecting '['");
315                 goto error;
316         }
317         isl_token_free(tok);
318         v = read_var_list(s, v);
319         tok = isl_stream_next_token(s);
320         if (!tok || tok->type != ']') {
321                 isl_stream_error(s, tok, "expecting ']'");
322                 goto error;
323         }
324         isl_token_free(tok);
325
326         return v;
327 error:
328         if (tok)
329                 isl_token_free(tok);
330         vars_free(v);
331         return NULL;
332 }
333
334 static struct isl_basic_map *add_constraints(struct isl_stream *s,
335         struct vars **v, struct isl_basic_map *bmap);
336
337 static struct isl_basic_map *add_exists(struct isl_stream *s,
338         struct vars **v, struct isl_basic_map *bmap)
339 {
340         struct isl_token *tok;
341         int n = (*v)->n;
342         int extra;
343         int seen_paren = 0;
344         int i;
345         unsigned total;
346
347         tok = isl_stream_next_token(s);
348         if (!tok)
349                 goto error;
350         if (tok->type == '(') {
351                 seen_paren = 1;
352                 isl_token_free(tok);
353         } else
354                 isl_stream_push_token(s, tok);
355
356         bmap = read_defined_var_list(s, *v, bmap);
357         if (!bmap)
358                 goto error;
359
360         if (isl_stream_eat(s, ':'))
361                 goto error;
362         bmap = add_constraints(s, v, bmap);
363         if (seen_paren && isl_stream_eat(s, ')'))
364                 goto error;
365         return bmap;
366 error:
367         isl_basic_map_free(bmap);
368         return NULL;
369 }
370
371 static struct isl_basic_map *add_constraint(struct isl_stream *s,
372         struct vars **v, struct isl_basic_map *bmap)
373 {
374         unsigned total = isl_basic_map_total_dim(bmap);
375         int k;
376         struct isl_token *tok = NULL;
377         struct isl_vec *aff1 = NULL, *aff2 = NULL;
378
379         tok = isl_stream_next_token(s);
380         if (!tok)
381                 goto error;
382         if (tok->type == ISL_TOKEN_EXISTS) {
383                 isl_token_free(tok);
384                 return add_exists(s, v, bmap);
385         }
386         isl_stream_push_token(s, tok);
387         tok = NULL;
388
389         bmap = isl_basic_map_cow(bmap);
390         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
391         k = isl_basic_map_alloc_inequality(bmap);
392         if (k < 0)
393                 goto error;
394
395         aff1 = accept_affine(s, *v);
396         if (!aff1)
397                 goto error;
398         tok = isl_stream_next_token(s);
399         switch (tok->type) {
400         case ISL_TOKEN_LE:
401         case ISL_TOKEN_GE:
402         case '=':
403                 break;
404         default:
405                 isl_stream_error(s, tok, "missing operator");
406                 isl_stream_push_token(s, tok);
407                 tok = NULL;
408                 goto error;
409         }
410         aff2 = accept_affine(s, *v);
411         if (!aff2)
412                 goto error;
413         isl_assert(aff1->ctx, aff1->size == 1 + total, goto error);
414         isl_assert(aff2->ctx, aff2->size == 1 + total, goto error);
415
416         if (tok->type == ISL_TOKEN_LE)
417                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->negone, aff1->el,
418                                                (*v)->ctx->one, aff2->el,
419                                                aff1->size);
420         else if (tok->type == ISL_TOKEN_GE)
421                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
422                                                (*v)->ctx->negone, aff2->el,
423                                                aff1->size);
424         else {
425                 isl_seq_combine(bmap->ineq[k], (*v)->ctx->one, aff1->el,
426                                                (*v)->ctx->negone, aff2->el,
427                                                aff1->size);
428                 isl_basic_map_inequality_to_equality(bmap, k);
429         }
430         isl_token_free(tok);
431         isl_vec_free(aff1);
432         isl_vec_free(aff2);
433
434         return bmap;
435 error:
436         if (tok)
437                 isl_token_free(tok);
438         isl_vec_free(aff1);
439         isl_vec_free(aff2);
440         isl_basic_map_free(bmap);
441         return NULL;
442 }
443
444 static struct isl_basic_map *add_constraints(struct isl_stream *s,
445         struct vars **v, struct isl_basic_map *bmap)
446 {
447         struct isl_token *tok;
448
449         for (;;) {
450                 bmap = add_constraint(s, v, bmap);
451                 if (!bmap)
452                         return NULL;
453                 tok = isl_stream_next_token(s);
454                 if (!tok) {
455                         isl_stream_error(s, NULL, "unexpected EOF");
456                         goto error;
457                 }
458                 if (tok->type != ISL_TOKEN_AND)
459                         break;
460                 isl_token_free(tok);
461         }
462         isl_stream_push_token(s, tok);
463
464         return bmap;
465 error:
466         if (tok)
467                 isl_token_free(tok);
468         isl_basic_map_free(bmap);
469         return NULL;
470 }
471
472 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
473         struct vars **v, __isl_take isl_dim *dim)
474 {
475         struct isl_basic_map *bmap;
476
477         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
478         if (!bmap)
479                 return NULL;
480
481         bmap = add_constraints(s, v, bmap);
482         bmap = isl_basic_map_simplify(bmap);
483         bmap = isl_basic_map_finalize(bmap);
484         return bmap;
485 }
486
487 static struct isl_map *read_disjuncts(struct isl_stream *s,
488         struct vars **v, __isl_take isl_dim *dim)
489 {
490         struct isl_token *tok;
491         struct isl_map *map;
492
493         tok = isl_stream_next_token(s);
494         if (!tok) {
495                 isl_stream_error(s, NULL, "unexpected EOF");
496                 goto error;
497         }
498         if (tok->type == '}') {
499                 isl_stream_push_token(s, tok);
500                 return isl_map_universe(dim);
501         }
502         isl_stream_push_token(s, tok);
503
504         map = isl_map_empty(isl_dim_copy(dim));
505         for (;;) {
506                 struct isl_basic_map *bmap;
507
508                 bmap = read_disjunct(s, v, isl_dim_copy(dim));
509                 map = isl_map_union(map, isl_map_from_basic_map(bmap));
510
511                 tok = isl_stream_next_token(s);
512                 if (!tok || tok->type != ISL_TOKEN_OR)
513                         break;
514                 isl_token_free(tok);
515         }
516         if (tok)
517                 isl_stream_push_token(s, tok);
518
519         isl_dim_free(dim);
520         return map;
521 error:
522         isl_dim_free(dim);
523         return NULL;
524 }
525
526 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
527         struct isl_stream *s, __isl_take isl_basic_map *bmap)
528 {
529         int j;
530         struct isl_token *tok;
531         int type;
532         int k;
533         isl_int *c;
534         unsigned nparam;
535         unsigned dim;
536
537         if (!bmap)
538                 return NULL;
539
540         nparam = isl_basic_map_dim(bmap, isl_dim_param);
541         dim = isl_basic_map_dim(bmap, isl_dim_out);
542
543         tok = isl_stream_next_token(s);
544         if (!tok || tok->type != ISL_TOKEN_VALUE) {
545                 isl_stream_error(s, tok, "expecting coefficient");
546                 if (tok)
547                         isl_stream_push_token(s, tok);
548                 goto error;
549         }
550         if (!tok->on_new_line) {
551                 isl_stream_error(s, tok, "coefficient should appear on new line");
552                 isl_stream_push_token(s, tok);
553                 goto error;
554         }
555
556         type = isl_int_get_si(tok->u.v);
557         isl_token_free(tok);
558
559         isl_assert(s->ctx, type == 0 || type == 1, goto error);
560         if (type == 0) {
561                 k = isl_basic_map_alloc_equality(bmap);
562                 c = bmap->eq[k];
563         } else {
564                 k = isl_basic_map_alloc_inequality(bmap);
565                 c = bmap->ineq[k];
566         }
567         if (k < 0)
568                 goto error;
569
570         for (j = 0; j < dim; ++j) {
571                 tok = isl_stream_next_token(s);
572                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
573                         isl_stream_error(s, tok, "expecting coefficient");
574                         if (tok)
575                                 isl_stream_push_token(s, tok);
576                         goto error;
577                 }
578                 isl_int_set(c[1 + nparam + j], tok->u.v);
579                 isl_token_free(tok);
580         }
581         for (j = 0; j < nparam; ++j) {
582                 tok = isl_stream_next_token(s);
583                 if (!tok || tok->type != ISL_TOKEN_VALUE) {
584                         isl_stream_error(s, tok, "expecting coefficient");
585                         if (tok)
586                                 isl_stream_push_token(s, tok);
587                         goto error;
588                 }
589                 isl_int_set(c[1 + j], tok->u.v);
590                 isl_token_free(tok);
591         }
592         tok = isl_stream_next_token(s);
593         if (!tok || tok->type != ISL_TOKEN_VALUE) {
594                 isl_stream_error(s, tok, "expecting coefficient");
595                 if (tok)
596                         isl_stream_push_token(s, tok);
597                 goto error;
598         }
599         isl_int_set(c[0], tok->u.v);
600         isl_token_free(tok);
601
602         return bmap;
603 error:
604         isl_basic_map_free(bmap);
605         return NULL;
606 }
607
608 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
609         int nparam)
610 {
611         int i;
612         struct isl_token *tok;
613         struct isl_token *tok2;
614         int n_row, n_col;
615         int on_new_line;
616         unsigned dim;
617         struct isl_basic_map *bmap = NULL;
618
619         if (nparam < 0)
620                 nparam = 0;
621
622         tok = isl_stream_next_token(s);
623         if (!tok) {
624                 isl_stream_error(s, NULL, "unexpected EOF");
625                 return NULL;
626         }
627         tok2 = isl_stream_next_token(s);
628         if (!tok2) {
629                 isl_token_free(tok);
630                 isl_stream_error(s, NULL, "unexpected EOF");
631                 return NULL;
632         }
633         n_row = isl_int_get_si(tok->u.v);
634         n_col = isl_int_get_si(tok2->u.v);
635         on_new_line = tok2->on_new_line;
636         isl_token_free(tok2);
637         isl_token_free(tok);
638         isl_assert(s->ctx, !on_new_line, return NULL);
639         isl_assert(s->ctx, n_row >= 0, return NULL);
640         isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
641         dim = n_col - 2 - nparam;
642         bmap = isl_basic_map_alloc(s->ctx, nparam, 0, dim, 0, n_row, n_row);
643         if (!bmap)
644                 return NULL;
645
646         for (i = 0; i < n_row; ++i)
647                 bmap = basic_map_read_polylib_constraint(s, bmap);
648
649         bmap = isl_basic_map_simplify(bmap);
650         bmap = isl_basic_map_finalize(bmap);
651         return bmap;
652 }
653
654 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
655 {
656         struct isl_token *tok;
657         struct isl_token *tok2;
658         int i, n;
659         struct isl_map *map;
660
661         tok = isl_stream_next_token(s);
662         if (!tok) {
663                 isl_stream_error(s, NULL, "unexpected EOF");
664                 return NULL;
665         }
666         tok2 = isl_stream_next_token(s);
667         if (!tok2) {
668                 isl_token_free(tok);
669                 isl_stream_error(s, NULL, "unexpected EOF");
670                 return NULL;
671         }
672         if (!tok2->on_new_line) {
673                 isl_stream_push_token(s, tok2);
674                 isl_stream_push_token(s, tok);
675                 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
676         }
677         isl_stream_push_token(s, tok2);
678         n = isl_int_get_si(tok->u.v);
679         isl_token_free(tok);
680
681         isl_assert(s->ctx, n >= 1, return NULL);
682
683         map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
684
685         for (i = 1; i < n; ++i)
686                 map = isl_map_union(map,
687                         isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
688
689         return map;
690 }
691
692 static struct isl_dim *set_names(struct isl_dim *dim, struct vars *vars,
693         enum isl_dim_type type, int offset, int n)
694 {
695         int i;
696         struct variable *v;
697
698         for (i = 0, v = vars->v; i < offset; ++i, v = v->next)
699                 ;
700         for (i = n - 1; i >= 0; --i, v = v->next)
701                 dim = isl_dim_set_name(dim, type, i, v->name);
702
703         return dim;
704 }
705
706 static struct isl_dim *dim_from_vars(struct vars *vars,
707         int nparam, int n_in, int n_out)
708 {
709         struct isl_dim *dim;
710
711         dim = isl_dim_alloc(vars->ctx, nparam, n_in, n_out);
712         dim = set_names(dim, vars, isl_dim_param, n_out + n_in, nparam);
713         dim = set_names(dim, vars, isl_dim_in, n_out, n_in);
714         dim = set_names(dim, vars, isl_dim_out, 0, n_out);
715
716         return dim;
717 }
718
719 static struct isl_map *map_read(struct isl_stream *s, int nparam)
720 {
721         struct isl_dim *dim = NULL;
722         struct isl_map *map = NULL;
723         struct isl_token *tok;
724         struct vars *v = NULL;
725         int n1;
726         int n2;
727
728         tok = isl_stream_next_token(s);
729         if (!tok) {
730                 isl_stream_error(s, NULL, "unexpected EOF");
731                 goto error;
732         }
733         if (tok->type == ISL_TOKEN_VALUE) {
734                 isl_stream_push_token(s, tok);
735                 return map_read_polylib(s, nparam);
736         }
737         v = vars_new(s->ctx);
738         if (tok->type == '[') {
739                 isl_stream_push_token(s, tok);
740                 v = read_tuple(s, v);
741                 if (!v)
742                         return NULL;
743                 if (nparam >= 0)
744                         isl_assert(s->ctx, nparam == v->n, goto error);
745                 nparam = v->n;
746                 tok = isl_stream_next_token(s);
747                 if (!tok || tok->type != ISL_TOKEN_TO) {
748                         isl_stream_error(s, tok, "expecting '->'");
749                         if (tok)
750                                 isl_stream_push_token(s, tok);
751                         goto error;
752                 }
753                 isl_token_free(tok);
754                 tok = isl_stream_next_token(s);
755         }
756         if (nparam < 0)
757                 nparam = 0;
758         if (!tok || tok->type != '{') {
759                 isl_stream_error(s, tok, "expecting '{'");
760                 if (tok)
761                         isl_stream_push_token(s, tok);
762                 goto error;
763         }
764         isl_token_free(tok);
765         v = read_tuple(s, v);
766         if (!v)
767                 return NULL;
768         n1 = v->n - nparam;
769         tok = isl_stream_next_token(s);
770         if (tok && tok->type == ISL_TOKEN_TO) {
771                 isl_token_free(tok);
772                 v = read_tuple(s, v);
773                 if (!v)
774                         return NULL;
775                 n2 = v->n - n1 - nparam;
776         } else {
777                 if (tok)
778                         isl_stream_push_token(s, tok);
779                 n2 = n1;
780                 n1 = 0;
781         }
782         dim = dim_from_vars(v, nparam, n1, n2);
783         tok = isl_stream_next_token(s);
784         if (!tok) {
785                 isl_stream_error(s, NULL, "unexpected EOF");
786                 goto error;
787         }
788         if (tok->type == ':') {
789                 isl_token_free(tok);
790                 map = read_disjuncts(s, &v, isl_dim_copy(dim));
791                 tok = isl_stream_next_token(s);
792         } else
793                 map = isl_map_universe(isl_dim_copy(dim));
794         if (tok && tok->type == '}') {
795                 isl_token_free(tok);
796         } else {
797                 isl_stream_error(s, tok, "unexpected isl_token");
798                 if (tok)
799                         isl_token_free(tok);
800                 goto error;
801         }
802         vars_free(v);
803         isl_dim_free(dim);
804
805         return map;
806 error:
807         isl_dim_free(dim);
808         isl_map_free(map);
809         if (v)
810                 vars_free(v);
811         return NULL;
812 }
813
814 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
815 {
816         struct isl_map *map;
817         struct isl_basic_map *bmap;
818
819         map = map_read(s, nparam);
820         if (!map)
821                 return NULL;
822
823         isl_assert(map->ctx, map->n <= 1, goto error);
824
825         if (map->n == 0)
826                 bmap = isl_basic_map_empty_like_map(map);
827         else
828                 bmap = isl_basic_map_copy(map->p[0]);
829
830         isl_map_free(map);
831
832         return bmap;
833 error:
834         isl_map_free(map);
835         return NULL;
836 }
837
838 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
839                 FILE *input, int nparam)
840 {
841         struct isl_basic_map *bmap;
842         struct isl_stream *s = isl_stream_new_file(ctx, input);
843         if (!s)
844                 return NULL;
845         bmap = basic_map_read(s, nparam);
846         isl_stream_free(s);
847         return bmap;
848 }
849
850 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
851                 FILE *input, int nparam)
852 {
853         struct isl_basic_map *bmap;
854         bmap = isl_basic_map_read_from_file(ctx, input, nparam);
855         if (!bmap)
856                 return NULL;
857         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
858         return (struct isl_basic_set *)bmap;
859 error:
860         isl_basic_map_free(bmap);
861         return NULL;
862 }
863
864 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
865                 const char *str, int nparam)
866 {
867         struct isl_basic_map *bmap;
868         struct isl_stream *s = isl_stream_new_str(ctx, str);
869         if (!s)
870                 return NULL;
871         bmap = basic_map_read(s, nparam);
872         isl_stream_free(s);
873         return bmap;
874 }
875
876 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
877                 const char *str, int nparam)
878 {
879         struct isl_basic_map *bmap;
880         bmap = isl_basic_map_read_from_str(ctx, str, nparam);
881         if (!bmap)
882                 return NULL;
883         isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
884         return (struct isl_basic_set *)bmap;
885 error:
886         isl_basic_map_free(bmap);
887         return NULL;
888 }
889
890 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
891                 FILE *input, int nparam)
892 {
893         struct isl_map *map;
894         struct isl_stream *s = isl_stream_new_file(ctx, input);
895         if (!s)
896                 return NULL;
897         map = map_read(s, nparam);
898         isl_stream_free(s);
899         return map;
900 }
901
902 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
903                 const char *str, int nparam)
904 {
905         struct isl_map *map;
906         struct isl_stream *s = isl_stream_new_str(ctx, str);
907         if (!s)
908                 return NULL;
909         map = map_read(s, nparam);
910         isl_stream_free(s);
911         return map;
912 }
913
914 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
915                 FILE *input, int nparam)
916 {
917         struct isl_map *map;
918         map = isl_map_read_from_file(ctx, input, nparam);
919         if (!map)
920                 return NULL;
921         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
922         return (struct isl_set *)map;
923 error:
924         isl_map_free(map);
925         return NULL;
926 }
927
928 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
929                 const char *str, int nparam)
930 {
931         struct isl_map *map;
932         map = isl_map_read_from_str(ctx, str, nparam);
933         if (!map)
934                 return NULL;
935         isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
936         return (struct isl_set *)map;
937 error:
938         isl_map_free(map);
939         return NULL;
940 }
941
942 static char *next_line(FILE *input, char *line, unsigned len)
943 {
944         char *p;
945
946         do {
947                 if (!(p = fgets(line, len, input)))
948                         return NULL;
949                 while (isspace(*p) && *p != '\n')
950                         ++p;
951         } while (*p == '#' || *p == '\n');
952
953         return p;
954 }
955
956 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
957                 FILE *input)
958 {
959         struct isl_vec *vec = NULL;
960         char line[1024];
961         char val[1024];
962         char *p;
963         unsigned size;
964         int j;
965         int n;
966         int offset;
967
968         isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
969         isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
970
971         vec = isl_vec_alloc(ctx, size);
972
973         p = next_line(input, line, sizeof(line));
974         isl_assert(ctx, p, goto error);
975
976         for (j = 0; j < size; ++j) {
977                 n = sscanf(p, "%s%n", val, &offset);
978                 isl_assert(ctx, n != 0, goto error);
979                 isl_int_read(vec->el[j], val);
980                 p += offset;
981         }
982
983         return vec;
984 error:
985         isl_vec_free(vec);
986         return NULL;
987 }
988
989 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
990                 FILE *input, unsigned input_format)
991 {
992         if (input_format == ISL_FORMAT_POLYLIB)
993                 return isl_vec_read_from_file_polylib(ctx, input);
994         else
995                 isl_assert(ctx, 0, return NULL);
996 }