isl_{in,}equality_alloc: take isl_local_space intead of isl_space
[isl.git] / isl_input.c
blob031bf1198acdce179b3b6d9bc8402acc5061480c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
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
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/set.h>
19 #include <isl/seq.h>
20 #include <isl/div.h>
21 #include <isl_stream_private.h>
22 #include <isl/obj.h>
23 #include "isl_polynomial_private.h"
24 #include <isl/union_map.h>
25 #include <isl_mat_private.h>
26 #include <isl_aff_private.h>
27 #include <isl/list.h>
29 struct variable {
30 char *name;
31 int pos;
32 struct variable *next;
35 struct vars {
36 struct isl_ctx *ctx;
37 int n;
38 struct variable *v;
41 static struct vars *vars_new(struct isl_ctx *ctx)
43 struct vars *v;
44 v = isl_alloc_type(ctx, struct vars);
45 if (!v)
46 return NULL;
47 v->ctx = ctx;
48 v->n = 0;
49 v->v = NULL;
50 return v;
53 static void variable_free(struct variable *var)
55 while (var) {
56 struct variable *next = var->next;
57 free(var->name);
58 free(var);
59 var = next;
63 static void vars_free(struct vars *v)
65 if (!v)
66 return;
67 variable_free(v->v);
68 free(v);
71 static void vars_drop(struct vars *v, int n)
73 struct variable *var;
75 if (!v || !v->v)
76 return;
78 v->n -= n;
80 var = v->v;
81 while (--n >= 0) {
82 struct variable *next = var->next;
83 free(var->name);
84 free(var);
85 var = next;
87 v->v = var;
90 static struct variable *variable_new(struct vars *v, const char *name, int len,
91 int pos)
93 struct variable *var;
94 var = isl_calloc_type(v->ctx, struct variable);
95 if (!var)
96 goto error;
97 var->name = strdup(name);
98 var->name[len] = '\0';
99 var->pos = pos;
100 var->next = v->v;
101 return var;
102 error:
103 variable_free(v->v);
104 return NULL;
107 static int vars_pos(struct vars *v, const char *s, int len)
109 int pos;
110 struct variable *q;
112 if (len == -1)
113 len = strlen(s);
114 for (q = v->v; q; q = q->next) {
115 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
116 break;
118 if (q)
119 pos = q->pos;
120 else {
121 pos = v->n;
122 v->v = variable_new(v, s, len, v->n);
123 if (!v->v)
124 return -1;
125 v->n++;
127 return pos;
130 static int vars_add_anon(struct vars *v)
132 v->v = variable_new(v, "", 0, v->n);
134 if (!v->v)
135 return -1;
136 v->n++;
138 return 0;
141 static __isl_give isl_map *set_name(__isl_take isl_map *map,
142 enum isl_dim_type type, unsigned pos, char *name)
144 char *prime;
146 if (!map)
147 return NULL;
148 if (!name)
149 return map;
151 prime = strchr(name, '\'');
152 if (prime)
153 *prime = '\0';
154 map = isl_map_set_dim_name(map, type, pos, name);
155 if (prime)
156 *prime = '\'';
158 return map;
161 /* Obtain next token, with some preprocessing.
162 * In particular, evaluate expressions of the form x^y,
163 * with x and y values.
165 static struct isl_token *next_token(struct isl_stream *s)
167 struct isl_token *tok, *tok2;
169 tok = isl_stream_next_token(s);
170 if (!tok || tok->type != ISL_TOKEN_VALUE)
171 return tok;
172 if (!isl_stream_eat_if_available(s, '^'))
173 return tok;
174 tok2 = isl_stream_next_token(s);
175 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
176 isl_stream_error(s, tok2, "expecting constant value");
177 goto error;
180 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
182 isl_token_free(tok2);
183 return tok;
184 error:
185 isl_token_free(tok);
186 isl_token_free(tok2);
187 return NULL;
190 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
192 struct isl_token *tok;
194 tok = next_token(s);
195 if (!tok || tok->type != ISL_TOKEN_VALUE) {
196 isl_stream_error(s, tok, "expecting constant value");
197 goto error;
200 isl_int_mul(*f, *f, tok->u.v);
202 isl_token_free(tok);
204 if (isl_stream_eat_if_available(s, '*'))
205 return accept_cst_factor(s, f);
207 return 0;
208 error:
209 isl_token_free(tok);
210 return -1;
213 /* Given an affine expression aff, return an affine expression
214 * for aff % d, with d the next token on the stream, which is
215 * assumed to be a constant.
217 * We introduce an integer division q = [aff/d] and the result
218 * is set to aff - d q.
220 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
221 struct vars *v, __isl_take isl_pw_aff *aff)
223 struct isl_token *tok;
224 isl_pw_aff *q;
226 tok = next_token(s);
227 if (!tok || tok->type != ISL_TOKEN_VALUE) {
228 isl_stream_error(s, tok, "expecting constant value");
229 goto error;
232 q = isl_pw_aff_copy(aff);
233 q = isl_pw_aff_scale_down(q, tok->u.v);
234 q = isl_pw_aff_floor(q);
235 q = isl_pw_aff_scale(q, tok->u.v);
237 aff = isl_pw_aff_sub(aff, q);
239 isl_token_free(tok);
240 return aff;
241 error:
242 isl_pw_aff_free(aff);
243 isl_token_free(tok);
244 return NULL;
247 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
248 __isl_take isl_space *dim, struct vars *v);
249 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
250 __isl_take isl_space *dim, struct vars *v);
252 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
253 __isl_take isl_space *dim, struct vars *v)
255 struct isl_token *tok;
256 isl_pw_aff_list *list = NULL;
257 int min;
259 tok = isl_stream_next_token(s);
260 if (!tok)
261 goto error;
262 min = tok->type == ISL_TOKEN_MIN;
263 isl_token_free(tok);
265 if (isl_stream_eat(s, '('))
266 goto error;
268 list = accept_affine_list(s, isl_space_copy(dim), v);
269 if (!list)
270 goto error;
272 if (isl_stream_eat(s, ')'))
273 goto error;
275 isl_space_free(dim);
276 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
277 error:
278 isl_space_free(dim);
279 isl_pw_aff_list_free(list);
280 return NULL;
283 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
284 __isl_take isl_space *dim, struct vars *v)
286 struct isl_token *tok;
287 int seen_paren = 0;
288 int f = 0;
289 int c = 0;
290 isl_pw_aff *pwaff = NULL;
292 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
293 f = 1;
294 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
295 c = 1;
296 if (f || c) {
297 if (isl_stream_eat(s, '('))
298 goto error;
299 } else {
300 if (isl_stream_eat(s, '['))
301 goto error;
302 if (isl_stream_eat_if_available(s, '('))
303 seen_paren = 1;
306 pwaff = accept_affine(s, isl_space_copy(dim), v);
308 if (f || c) {
309 if (isl_stream_eat(s, ','))
310 goto error;
311 } else {
312 if (seen_paren && isl_stream_eat(s, ')'))
313 goto error;
314 if (isl_stream_eat(s, '/'))
315 goto error;
318 tok = next_token(s);
319 if (!tok)
320 goto error;
321 if (tok->type != ISL_TOKEN_VALUE) {
322 isl_stream_error(s, tok, "expected denominator");
323 isl_stream_push_token(s, tok);
324 goto error;
326 isl_pw_aff_scale_down(pwaff, tok->u.v);
327 isl_token_free(tok);
329 if (c)
330 pwaff = isl_pw_aff_ceil(pwaff);
331 else
332 pwaff = isl_pw_aff_floor(pwaff);
334 if (f || c) {
335 if (isl_stream_eat(s, ')'))
336 goto error;
337 } else {
338 if (isl_stream_eat(s, ']'))
339 goto error;
342 isl_space_free(dim);
343 return pwaff;
344 error:
345 isl_space_free(dim);
346 isl_pw_aff_free(pwaff);
347 return NULL;
350 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
351 __isl_take isl_space *dim, struct vars *v)
353 struct isl_token *tok = NULL;
354 isl_pw_aff *res = NULL;
356 tok = next_token(s);
357 if (!tok) {
358 isl_stream_error(s, NULL, "unexpected EOF");
359 goto error;
362 if (tok->type == ISL_TOKEN_AFF) {
363 res = isl_pw_aff_copy(tok->u.pwaff);
364 isl_token_free(tok);
365 } else if (tok->type == ISL_TOKEN_IDENT) {
366 int n = v->n;
367 int pos = vars_pos(v, tok->u.s, -1);
368 isl_aff *aff;
370 if (pos < 0)
371 goto error;
372 if (pos >= n) {
373 isl_stream_error(s, tok, "unknown identifier");
374 goto error;
377 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
378 if (!aff)
379 goto error;
380 isl_int_set_si(aff->v->el[2 + pos], 1);
381 res = isl_pw_aff_from_aff(aff);
382 isl_token_free(tok);
383 } else if (tok->type == ISL_TOKEN_VALUE) {
384 if (isl_stream_eat_if_available(s, '*')) {
385 res = accept_affine_factor(s, isl_space_copy(dim), v);
386 res = isl_pw_aff_scale(res, tok->u.v);
387 } else {
388 isl_local_space *ls;
389 isl_aff *aff;
390 ls = isl_local_space_from_space(isl_space_copy(dim));
391 aff = isl_aff_zero_on_domain(ls);
392 aff = isl_aff_add_constant(aff, tok->u.v);
393 res = isl_pw_aff_from_aff(aff);
395 isl_token_free(tok);
396 } else if (tok->type == '(') {
397 isl_token_free(tok);
398 tok = NULL;
399 res = accept_affine(s, isl_space_copy(dim), v);
400 if (!res)
401 goto error;
402 if (isl_stream_eat(s, ')'))
403 goto error;
404 } else if (tok->type == '[' ||
405 tok->type == ISL_TOKEN_FLOORD ||
406 tok->type == ISL_TOKEN_CEILD) {
407 isl_stream_push_token(s, tok);
408 tok = NULL;
409 res = accept_div(s, isl_space_copy(dim), v);
410 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
411 isl_stream_push_token(s, tok);
412 tok = NULL;
413 res = accept_minmax(s, isl_space_copy(dim), v);
414 } else {
415 isl_stream_error(s, tok, "expecting factor");
416 goto error;
418 if (isl_stream_eat_if_available(s, '%') ||
419 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
420 isl_space_free(dim);
421 return affine_mod(s, v, res);
423 if (isl_stream_eat_if_available(s, '*')) {
424 isl_int f;
425 isl_int_init(f);
426 isl_int_set_si(f, 1);
427 if (accept_cst_factor(s, &f) < 0) {
428 isl_int_clear(f);
429 goto error2;
431 res = isl_pw_aff_scale(res, f);
432 isl_int_clear(f);
435 isl_space_free(dim);
436 return res;
437 error:
438 isl_token_free(tok);
439 error2:
440 isl_pw_aff_free(res);
441 isl_space_free(dim);
442 return NULL;
445 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
447 isl_aff *aff;
448 isl_space *space;
450 space = isl_pw_aff_get_domain_space(pwaff);
451 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
452 aff = isl_aff_add_constant(aff, v);
454 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
457 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
458 __isl_take isl_space *dim, struct vars *v)
460 struct isl_token *tok = NULL;
461 isl_local_space *ls;
462 isl_pw_aff *res;
463 int sign = 1;
465 ls = isl_local_space_from_space(isl_space_copy(dim));
466 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
467 if (!res)
468 goto error;
470 for (;;) {
471 tok = next_token(s);
472 if (!tok) {
473 isl_stream_error(s, NULL, "unexpected EOF");
474 goto error;
476 if (tok->type == '-') {
477 sign = -sign;
478 isl_token_free(tok);
479 continue;
481 if (tok->type == '(' || tok->type == '[' ||
482 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
483 tok->type == ISL_TOKEN_FLOORD ||
484 tok->type == ISL_TOKEN_CEILD ||
485 tok->type == ISL_TOKEN_IDENT ||
486 tok->type == ISL_TOKEN_AFF) {
487 isl_pw_aff *term;
488 isl_stream_push_token(s, tok);
489 tok = NULL;
490 term = accept_affine_factor(s, isl_space_copy(dim), v);
491 if (sign < 0)
492 res = isl_pw_aff_sub(res, term);
493 else
494 res = isl_pw_aff_add(res, term);
495 if (!res)
496 goto error;
497 sign = 1;
498 } else if (tok->type == ISL_TOKEN_VALUE) {
499 if (sign < 0)
500 isl_int_neg(tok->u.v, tok->u.v);
501 if (isl_stream_eat_if_available(s, '*') ||
502 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
503 isl_pw_aff *term;
504 term = accept_affine_factor(s,
505 isl_space_copy(dim), v);
506 term = isl_pw_aff_scale(term, tok->u.v);
507 res = isl_pw_aff_add(res, term);
508 if (!res)
509 goto error;
510 } else {
511 res = add_cst(res, tok->u.v);
513 sign = 1;
514 } else {
515 isl_stream_error(s, tok, "unexpected isl_token");
516 isl_stream_push_token(s, tok);
517 isl_pw_aff_free(res);
518 isl_space_free(dim);
519 return NULL;
521 isl_token_free(tok);
523 tok = next_token(s);
524 if (tok && tok->type == '-') {
525 sign = -sign;
526 isl_token_free(tok);
527 } else if (tok && tok->type == '+') {
528 /* nothing */
529 isl_token_free(tok);
530 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
531 isl_int_is_neg(tok->u.v)) {
532 isl_stream_push_token(s, tok);
533 } else {
534 if (tok)
535 isl_stream_push_token(s, tok);
536 break;
540 isl_space_free(dim);
541 return res;
542 error:
543 isl_space_free(dim);
544 isl_token_free(tok);
545 isl_pw_aff_free(res);
546 return NULL;
549 static int is_comparator(struct isl_token *tok)
551 if (!tok)
552 return 0;
554 switch (tok->type) {
555 case ISL_TOKEN_LT:
556 case ISL_TOKEN_GT:
557 case ISL_TOKEN_LE:
558 case ISL_TOKEN_GE:
559 case ISL_TOKEN_NE:
560 case '=':
561 return 1;
562 default:
563 return 0;
567 static struct isl_map *read_disjuncts(struct isl_stream *s,
568 struct vars *v, __isl_take isl_map *map);
569 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
570 __isl_take isl_space *dim, struct vars *v);
572 /* Accept a ternary operator, given the first argument.
574 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
575 __isl_take isl_map *cond, struct vars *v)
577 isl_space *dim;
578 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL;
580 if (!cond)
581 return NULL;
583 if (isl_stream_eat(s, '?'))
584 goto error;
586 dim = isl_space_wrap(isl_map_get_space(cond));
587 pwaff1 = accept_extended_affine(s, dim, v);
588 if (!pwaff1)
589 goto error;
591 if (isl_stream_eat(s, ':'))
592 goto error;
594 dim = isl_pw_aff_get_domain_space(pwaff1);
595 pwaff2 = accept_extended_affine(s, dim, v);
596 if (!pwaff1)
597 goto error;
599 return isl_pw_aff_cond(isl_map_wrap(cond), pwaff1, pwaff2);
600 error:
601 isl_map_free(cond);
602 isl_pw_aff_free(pwaff1);
603 isl_pw_aff_free(pwaff2);
604 return NULL;
607 /* Accept an affine expression that may involve ternary operators.
608 * We first read an affine expression.
609 * If it is not followed by a comparison operator, we simply return it.
610 * Otherwise, we assume the affine epxression is part of the first
611 * argument of a ternary operator and try to parse that.
613 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
614 __isl_take isl_space *dim, struct vars *v)
616 isl_space *space;
617 isl_map *cond;
618 isl_pw_aff *pwaff;
619 struct isl_token *tok;
620 int line = -1, col = -1;
621 int is_comp;
623 tok = isl_stream_next_token(s);
624 if (tok) {
625 line = tok->line;
626 col = tok->col;
627 isl_stream_push_token(s, tok);
630 pwaff = accept_affine(s, dim, v);
631 if (!pwaff)
632 return NULL;
634 tok = isl_stream_next_token(s);
635 if (!tok)
636 return isl_pw_aff_free(pwaff);
638 is_comp = is_comparator(tok);
639 isl_stream_push_token(s, tok);
640 if (!is_comp)
641 return pwaff;
643 tok = isl_token_new(s->ctx, line, col, 0);
644 if (!tok)
645 return isl_pw_aff_free(pwaff);
646 tok->type = ISL_TOKEN_AFF;
647 tok->u.pwaff = pwaff;
649 space = isl_pw_aff_get_domain_space(pwaff);
650 cond = isl_map_universe(isl_space_unwrap(space));
652 isl_stream_push_token(s, tok);
654 cond = read_disjuncts(s, v, cond);
656 return accept_ternary(s, cond, v);
659 static __isl_give isl_map *read_var_def(struct isl_stream *s,
660 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
662 isl_pw_aff *def;
663 int pos;
664 isl_map *def_map;
666 if (type == isl_dim_param)
667 pos = isl_map_dim(map, isl_dim_param);
668 else {
669 pos = isl_map_dim(map, isl_dim_in);
670 if (type == isl_dim_out)
671 pos += isl_map_dim(map, isl_dim_out);
672 type = isl_dim_in;
674 --pos;
676 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
677 def_map = isl_map_from_pw_aff(def);
678 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
679 def_map = isl_set_unwrap(isl_map_domain(def_map));
681 map = isl_map_intersect(map, def_map);
683 return map;
686 static __isl_give isl_map *read_var_list(struct isl_stream *s,
687 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
689 int i = 0;
690 struct isl_token *tok;
692 if (isl_stream_next_token_is(s, ']'))
693 return isl_map_add_dims(map, type, 0);
695 while ((tok = next_token(s)) != NULL) {
696 int new_name = 0;
698 if (tok->type == ISL_TOKEN_IDENT) {
699 int n = v->n;
700 int p = vars_pos(v, tok->u.s, -1);
701 if (p < 0)
702 goto error;
703 new_name = p >= n;
706 if (new_name) {
707 map = isl_map_add_dims(map, type, 1);
708 map = set_name(map, type, i, v->v->name);
709 isl_token_free(tok);
710 if (isl_stream_eat_if_available(s, '='))
711 map = read_var_def(s, map, type, v);
712 } else {
713 if (type == isl_dim_param) {
714 isl_stream_error(s, tok,
715 "expecting unique identifier");
716 goto error;
718 isl_stream_push_token(s, tok);
719 tok = NULL;
720 if (vars_add_anon(v) < 0)
721 goto error;
722 map = isl_map_add_dims(map, type, 1);
723 map = read_var_def(s, map, type, v);
726 tok = isl_stream_next_token(s);
727 if (tok && tok->type == ']' &&
728 isl_stream_next_token_is(s, '[')) {
729 isl_token_free(tok);
730 tok = isl_stream_next_token(s);
731 } else if (!tok || tok->type != ',')
732 break;
734 isl_token_free(tok);
735 i++;
737 if (tok)
738 isl_stream_push_token(s, tok);
740 return map;
741 error:
742 isl_token_free(tok);
743 isl_map_free(map);
744 return NULL;
747 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
748 __isl_take isl_space *dim, struct vars *v)
750 isl_pw_aff *pwaff;
751 isl_pw_aff_list *list;
752 struct isl_token *tok = NULL;
754 pwaff = accept_affine(s, isl_space_copy(dim), v);
755 list = isl_pw_aff_list_from_pw_aff(pwaff);
756 if (!list)
757 goto error;
759 for (;;) {
760 tok = isl_stream_next_token(s);
761 if (!tok) {
762 isl_stream_error(s, NULL, "unexpected EOF");
763 goto error;
765 if (tok->type != ',') {
766 isl_stream_push_token(s, tok);
767 break;
769 isl_token_free(tok);
771 pwaff = accept_affine(s, isl_space_copy(dim), v);
772 list = isl_pw_aff_list_concat(list,
773 isl_pw_aff_list_from_pw_aff(pwaff));
774 if (!list)
775 return NULL;
778 isl_space_free(dim);
779 return list;
780 error:
781 isl_space_free(dim);
782 isl_pw_aff_list_free(list);
783 return NULL;
786 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
787 struct vars *v, __isl_take isl_map *map)
789 struct isl_token *tok;
791 while ((tok = isl_stream_next_token(s)) != NULL) {
792 int p;
793 int n = v->n;
795 if (tok->type != ISL_TOKEN_IDENT)
796 break;
798 p = vars_pos(v, tok->u.s, -1);
799 if (p < 0)
800 goto error;
801 if (p < n) {
802 isl_stream_error(s, tok, "expecting unique identifier");
803 goto error;
806 map = isl_map_add_dims(map, isl_dim_out, 1);
808 isl_token_free(tok);
809 tok = isl_stream_next_token(s);
810 if (tok && tok->type == '=') {
811 isl_token_free(tok);
812 map = read_var_def(s, map, isl_dim_out, v);
813 tok = isl_stream_next_token(s);
816 if (!tok || tok->type != ',')
817 break;
819 isl_token_free(tok);
821 if (tok)
822 isl_stream_push_token(s, tok);
824 return map;
825 error:
826 isl_token_free(tok);
827 isl_map_free(map);
828 return NULL;
831 static int next_is_tuple(struct isl_stream *s)
833 struct isl_token *tok;
834 int is_tuple;
836 tok = isl_stream_next_token(s);
837 if (!tok)
838 return 0;
839 if (tok->type == '[') {
840 isl_stream_push_token(s, tok);
841 return 1;
843 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
844 isl_stream_push_token(s, tok);
845 return 0;
848 is_tuple = isl_stream_next_token_is(s, '[');
850 isl_stream_push_token(s, tok);
852 return is_tuple;
855 static __isl_give isl_map *read_tuple(struct isl_stream *s,
856 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v);
858 static __isl_give isl_set *read_nested_tuple(struct isl_stream *s,
859 __isl_take isl_map *map, struct vars *v)
861 map = read_tuple(s, map, isl_dim_in, v);
862 if (isl_stream_eat(s, ISL_TOKEN_TO))
863 goto error;
864 map = read_tuple(s, map, isl_dim_out, v);
865 return isl_map_wrap(map);
866 error:
867 isl_map_free(map);
868 return NULL;
871 static __isl_give isl_map *read_tuple(struct isl_stream *s,
872 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v)
874 struct isl_token *tok;
875 char *name = NULL;
877 tok = isl_stream_next_token(s);
878 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
879 name = strdup(tok->u.s);
880 if (!name)
881 goto error;
882 isl_token_free(tok);
883 tok = isl_stream_next_token(s);
885 if (!tok || tok->type != '[') {
886 isl_stream_error(s, tok, "expecting '['");
887 goto error;
889 isl_token_free(tok);
890 if (type != isl_dim_param && next_is_tuple(s)) {
891 isl_space *dim = isl_map_get_space(map);
892 int nparam = isl_space_dim(dim, isl_dim_param);
893 int n_in = isl_space_dim(dim, isl_dim_in);
894 isl_set *nested;
895 if (type == isl_dim_out) {
896 dim = isl_space_move_dims(dim, isl_dim_param, nparam,
897 isl_dim_in, 0, n_in);
898 dim = isl_space_params(dim);
900 nested = read_nested_tuple(s, isl_map_universe(dim), v);
901 if (type == isl_dim_in) {
902 nested = isl_map_reverse(nested);
903 map = isl_map_intersect(nested, map);
904 } else {
905 isl_set *set;
906 dim = isl_set_get_space(nested);
907 dim = isl_space_drop_dims(dim, isl_dim_param, nparam, n_in);
908 dim = isl_space_join(isl_map_get_space(map), dim);
909 set = isl_map_domain(map);
910 nested = isl_map_reset_space(nested, dim);
911 map = isl_map_intersect_domain(nested, set);
913 } else
914 map = read_var_list(s, map, type, v);
915 tok = isl_stream_next_token(s);
916 if (!tok || tok->type != ']') {
917 isl_stream_error(s, tok, "expecting ']'");
918 goto error;
920 isl_token_free(tok);
922 if (name) {
923 map = isl_map_set_tuple_name(map, type, name);
924 free(name);
927 return map;
928 error:
929 if (tok)
930 isl_token_free(tok);
931 isl_map_free(map);
932 return NULL;
935 static __isl_give isl_set *construct_constraints(
936 __isl_take isl_set *set, enum isl_token_type type,
937 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right)
939 isl_set *cond;
941 if (type == ISL_TOKEN_LE)
942 cond = isl_pw_aff_list_le_set(isl_pw_aff_list_copy(left),
943 isl_pw_aff_list_copy(right));
944 else if (type == ISL_TOKEN_GE)
945 cond = isl_pw_aff_list_ge_set(isl_pw_aff_list_copy(left),
946 isl_pw_aff_list_copy(right));
947 else if (type == ISL_TOKEN_LT)
948 cond = isl_pw_aff_list_lt_set(isl_pw_aff_list_copy(left),
949 isl_pw_aff_list_copy(right));
950 else if (type == ISL_TOKEN_GT)
951 cond = isl_pw_aff_list_gt_set(isl_pw_aff_list_copy(left),
952 isl_pw_aff_list_copy(right));
953 else if (type == ISL_TOKEN_NE)
954 cond = isl_pw_aff_list_ne_set(isl_pw_aff_list_copy(left),
955 isl_pw_aff_list_copy(right));
956 else
957 cond = isl_pw_aff_list_eq_set(isl_pw_aff_list_copy(left),
958 isl_pw_aff_list_copy(right));
960 return isl_set_intersect(set, cond);
963 static __isl_give isl_map *add_constraint(struct isl_stream *s,
964 struct vars *v, __isl_take isl_map *map)
966 struct isl_token *tok = NULL;
967 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
968 isl_set *set;
970 set = isl_map_wrap(map);
971 list1 = accept_affine_list(s, isl_set_get_space(set), v);
972 if (!list1)
973 goto error;
974 tok = isl_stream_next_token(s);
975 if (!is_comparator(tok)) {
976 isl_stream_error(s, tok, "missing operator");
977 if (tok)
978 isl_stream_push_token(s, tok);
979 tok = NULL;
980 goto error;
982 for (;;) {
983 list2 = accept_affine_list(s, isl_set_get_space(set), v);
984 if (!list2)
985 goto error;
987 set = construct_constraints(set, tok->type, list1, list2);
988 isl_token_free(tok);
989 isl_pw_aff_list_free(list1);
990 list1 = list2;
992 tok = isl_stream_next_token(s);
993 if (!is_comparator(tok)) {
994 if (tok)
995 isl_stream_push_token(s, tok);
996 break;
999 isl_pw_aff_list_free(list1);
1001 return isl_set_unwrap(set);
1002 error:
1003 if (tok)
1004 isl_token_free(tok);
1005 isl_pw_aff_list_free(list1);
1006 isl_pw_aff_list_free(list2);
1007 isl_set_free(set);
1008 return NULL;
1011 static __isl_give isl_map *read_exists(struct isl_stream *s,
1012 struct vars *v, __isl_take isl_map *map)
1014 int n = v->n;
1015 int seen_paren = isl_stream_eat_if_available(s, '(');
1017 map = isl_map_from_domain(isl_map_wrap(map));
1018 map = read_defined_var_list(s, v, map);
1020 if (isl_stream_eat(s, ':'))
1021 goto error;
1023 map = read_disjuncts(s, v, map);
1024 map = isl_set_unwrap(isl_map_domain(map));
1026 vars_drop(v, v->n - n);
1027 if (seen_paren && isl_stream_eat(s, ')'))
1028 goto error;
1030 return map;
1031 error:
1032 isl_map_free(map);
1033 return NULL;
1036 /* Parse an expression between parentheses and push the result
1037 * back on the stream.
1039 * The parsed expression may be either an affine expression
1040 * or a condition. The first type is pushed onto the stream
1041 * as an isl_pw_aff, while the second is pushed as an isl_map.
1043 * If the initial token indicates the start of a condition,
1044 * we parse it as such.
1045 * Otherwise, we first parse an affine expression and push
1046 * that onto the stream. If the affine expression covers the
1047 * entire expression between parentheses, we return.
1048 * Otherwise, we assume that the affine expression is the
1049 * start of a condition and continue parsing.
1051 static int resolve_paren_expr(struct isl_stream *s,
1052 struct vars *v, __isl_take isl_map *map)
1054 struct isl_token *tok, *tok2;
1055 int line, col;
1056 isl_pw_aff *pwaff;
1058 tok = isl_stream_next_token(s);
1059 if (!tok || tok->type != '(')
1060 goto error;
1062 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1063 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1064 isl_stream_next_token_is(s, ISL_TOKEN_FALSE)) {
1065 map = read_disjuncts(s, v, map);
1066 if (isl_stream_eat(s, ')'))
1067 goto error;
1068 tok->type = ISL_TOKEN_MAP;
1069 tok->u.map = map;
1070 isl_stream_push_token(s, tok);
1071 return 0;
1074 tok2 = isl_stream_next_token(s);
1075 if (!tok2)
1076 goto error;
1077 line = tok2->line;
1078 col = tok2->col;
1079 isl_stream_push_token(s, tok2);
1081 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1082 if (!pwaff)
1083 goto error;
1085 tok2 = isl_token_new(s->ctx, line, col, 0);
1086 if (!tok2)
1087 goto error2;
1088 tok2->type = ISL_TOKEN_AFF;
1089 tok2->u.pwaff = pwaff;
1091 if (isl_stream_eat_if_available(s, ')')) {
1092 isl_stream_push_token(s, tok2);
1093 isl_token_free(tok);
1094 isl_map_free(map);
1095 return 0;
1098 isl_stream_push_token(s, tok2);
1100 map = read_disjuncts(s, v, map);
1101 if (isl_stream_eat(s, ')'))
1102 goto error;
1104 tok->type = ISL_TOKEN_MAP;
1105 tok->u.map = map;
1106 isl_stream_push_token(s, tok);
1108 return 0;
1109 error2:
1110 isl_pw_aff_free(pwaff);
1111 error:
1112 isl_token_free(tok);
1113 isl_map_free(map);
1114 return -1;
1117 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1118 struct vars *v, __isl_take isl_map *map)
1120 if (isl_stream_next_token_is(s, '('))
1121 if (resolve_paren_expr(s, v, isl_map_copy(map)))
1122 goto error;
1124 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1125 struct isl_token *tok;
1126 tok = isl_stream_next_token(s);
1127 if (!tok)
1128 goto error;
1129 isl_map_free(map);
1130 map = isl_map_copy(tok->u.map);
1131 isl_token_free(tok);
1132 return map;
1135 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1136 return read_exists(s, v, map);
1138 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1139 return map;
1141 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1142 isl_space *dim = isl_map_get_space(map);
1143 isl_map_free(map);
1144 return isl_map_empty(dim);
1147 return add_constraint(s, v, map);
1148 error:
1149 isl_map_free(map);
1150 return NULL;
1153 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1154 struct vars *v, __isl_take isl_map *map)
1156 isl_map *res;
1157 int negate;
1159 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1160 res = read_conjunct(s, v, isl_map_copy(map));
1161 if (negate)
1162 res = isl_map_subtract(isl_map_copy(map), res);
1164 while (isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1165 isl_map *res_i;
1167 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1168 res_i = read_conjunct(s, v, isl_map_copy(map));
1169 if (negate)
1170 res = isl_map_subtract(res, res_i);
1171 else
1172 res = isl_map_intersect(res, res_i);
1175 isl_map_free(map);
1176 return res;
1179 static struct isl_map *read_disjuncts(struct isl_stream *s,
1180 struct vars *v, __isl_take isl_map *map)
1182 isl_map *res;
1184 if (isl_stream_next_token_is(s, '}')) {
1185 isl_space *dim = isl_map_get_space(map);
1186 isl_map_free(map);
1187 return isl_map_universe(dim);
1190 res = read_conjuncts(s, v, isl_map_copy(map));
1191 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1192 isl_map *res_i;
1194 res_i = read_conjuncts(s, v, isl_map_copy(map));
1195 res = isl_map_union(res, res_i);
1198 isl_map_free(map);
1199 return res;
1202 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1204 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1205 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1206 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1207 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1209 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1210 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1211 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1213 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1214 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1215 isl_basic_map_dim(bmap, isl_dim_in) +
1216 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1217 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1219 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1220 return 1 + pos;
1222 return 0;
1225 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1226 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1228 int j;
1229 struct isl_token *tok;
1230 int type;
1231 int k;
1232 isl_int *c;
1233 unsigned nparam;
1234 unsigned dim;
1236 if (!bmap)
1237 return NULL;
1239 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1240 dim = isl_basic_map_dim(bmap, isl_dim_out);
1242 tok = isl_stream_next_token(s);
1243 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1244 isl_stream_error(s, tok, "expecting coefficient");
1245 if (tok)
1246 isl_stream_push_token(s, tok);
1247 goto error;
1249 if (!tok->on_new_line) {
1250 isl_stream_error(s, tok, "coefficient should appear on new line");
1251 isl_stream_push_token(s, tok);
1252 goto error;
1255 type = isl_int_get_si(tok->u.v);
1256 isl_token_free(tok);
1258 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1259 if (type == 0) {
1260 k = isl_basic_map_alloc_equality(bmap);
1261 c = bmap->eq[k];
1262 } else {
1263 k = isl_basic_map_alloc_inequality(bmap);
1264 c = bmap->ineq[k];
1266 if (k < 0)
1267 goto error;
1269 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1270 int pos;
1271 tok = isl_stream_next_token(s);
1272 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1273 isl_stream_error(s, tok, "expecting coefficient");
1274 if (tok)
1275 isl_stream_push_token(s, tok);
1276 goto error;
1278 if (tok->on_new_line) {
1279 isl_stream_error(s, tok,
1280 "coefficient should not appear on new line");
1281 isl_stream_push_token(s, tok);
1282 goto error;
1284 pos = polylib_pos_to_isl_pos(bmap, j);
1285 isl_int_set(c[pos], tok->u.v);
1286 isl_token_free(tok);
1289 return bmap;
1290 error:
1291 isl_basic_map_free(bmap);
1292 return NULL;
1295 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
1296 int nparam)
1298 int i;
1299 struct isl_token *tok;
1300 struct isl_token *tok2;
1301 int n_row, n_col;
1302 int on_new_line;
1303 unsigned in = 0, out, local = 0;
1304 struct isl_basic_map *bmap = NULL;
1306 if (nparam < 0)
1307 nparam = 0;
1309 tok = isl_stream_next_token(s);
1310 if (!tok) {
1311 isl_stream_error(s, NULL, "unexpected EOF");
1312 return NULL;
1314 tok2 = isl_stream_next_token(s);
1315 if (!tok2) {
1316 isl_token_free(tok);
1317 isl_stream_error(s, NULL, "unexpected EOF");
1318 return NULL;
1320 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1321 isl_stream_push_token(s, tok2);
1322 isl_stream_push_token(s, tok);
1323 isl_stream_error(s, NULL,
1324 "expecting constraint matrix dimensions");
1325 return NULL;
1327 n_row = isl_int_get_si(tok->u.v);
1328 n_col = isl_int_get_si(tok2->u.v);
1329 on_new_line = tok2->on_new_line;
1330 isl_token_free(tok2);
1331 isl_token_free(tok);
1332 isl_assert(s->ctx, !on_new_line, return NULL);
1333 isl_assert(s->ctx, n_row >= 0, return NULL);
1334 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1335 tok = isl_stream_next_token_on_same_line(s);
1336 if (tok) {
1337 if (tok->type != ISL_TOKEN_VALUE) {
1338 isl_stream_error(s, tok,
1339 "expecting number of output dimensions");
1340 isl_stream_push_token(s, tok);
1341 goto error;
1343 out = isl_int_get_si(tok->u.v);
1344 isl_token_free(tok);
1346 tok = isl_stream_next_token_on_same_line(s);
1347 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1348 isl_stream_error(s, tok,
1349 "expecting number of input dimensions");
1350 if (tok)
1351 isl_stream_push_token(s, tok);
1352 goto error;
1354 in = isl_int_get_si(tok->u.v);
1355 isl_token_free(tok);
1357 tok = isl_stream_next_token_on_same_line(s);
1358 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1359 isl_stream_error(s, tok,
1360 "expecting number of existentials");
1361 if (tok)
1362 isl_stream_push_token(s, tok);
1363 goto error;
1365 local = isl_int_get_si(tok->u.v);
1366 isl_token_free(tok);
1368 tok = isl_stream_next_token_on_same_line(s);
1369 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1370 isl_stream_error(s, tok,
1371 "expecting number of parameters");
1372 if (tok)
1373 isl_stream_push_token(s, tok);
1374 goto error;
1376 nparam = isl_int_get_si(tok->u.v);
1377 isl_token_free(tok);
1378 if (n_col != 1 + out + in + local + nparam + 1) {
1379 isl_stream_error(s, NULL,
1380 "dimensions don't match");
1381 goto error;
1383 } else
1384 out = n_col - 2 - nparam;
1385 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1386 if (!bmap)
1387 return NULL;
1389 for (i = 0; i < local; ++i) {
1390 int k = isl_basic_map_alloc_div(bmap);
1391 if (k < 0)
1392 goto error;
1393 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1396 for (i = 0; i < n_row; ++i)
1397 bmap = basic_map_read_polylib_constraint(s, bmap);
1399 tok = isl_stream_next_token_on_same_line(s);
1400 if (tok) {
1401 isl_stream_error(s, tok, "unexpected extra token on line");
1402 isl_stream_push_token(s, tok);
1403 goto error;
1406 bmap = isl_basic_map_simplify(bmap);
1407 bmap = isl_basic_map_finalize(bmap);
1408 return bmap;
1409 error:
1410 isl_basic_map_free(bmap);
1411 return NULL;
1414 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
1416 struct isl_token *tok;
1417 struct isl_token *tok2;
1418 int i, n;
1419 struct isl_map *map;
1421 tok = isl_stream_next_token(s);
1422 if (!tok) {
1423 isl_stream_error(s, NULL, "unexpected EOF");
1424 return NULL;
1426 tok2 = isl_stream_next_token_on_same_line(s);
1427 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1428 isl_stream_push_token(s, tok2);
1429 isl_stream_push_token(s, tok);
1430 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1432 if (tok2) {
1433 isl_stream_error(s, tok2, "unexpected token");
1434 isl_stream_push_token(s, tok2);
1435 isl_stream_push_token(s, tok);
1436 return NULL;
1438 n = isl_int_get_si(tok->u.v);
1439 isl_token_free(tok);
1441 isl_assert(s->ctx, n >= 1, return NULL);
1443 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
1445 for (i = 1; map && i < n; ++i)
1446 map = isl_map_union(map,
1447 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
1449 return map;
1452 static int optional_power(struct isl_stream *s)
1454 int pow;
1455 struct isl_token *tok;
1457 tok = isl_stream_next_token(s);
1458 if (!tok)
1459 return 1;
1460 if (tok->type != '^') {
1461 isl_stream_push_token(s, tok);
1462 return 1;
1464 isl_token_free(tok);
1465 tok = isl_stream_next_token(s);
1466 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1467 isl_stream_error(s, tok, "expecting exponent");
1468 if (tok)
1469 isl_stream_push_token(s, tok);
1470 return 1;
1472 pow = isl_int_get_si(tok->u.v);
1473 isl_token_free(tok);
1474 return pow;
1477 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1478 __isl_keep isl_map *map, struct vars *v);
1480 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1481 __isl_keep isl_map *map, struct vars *v)
1483 isl_pw_qpolynomial *pwqp;
1484 struct isl_token *tok;
1486 tok = next_token(s);
1487 if (!tok) {
1488 isl_stream_error(s, NULL, "unexpected EOF");
1489 return NULL;
1491 if (tok->type == '(') {
1492 int pow;
1494 isl_token_free(tok);
1495 pwqp = read_term(s, map, v);
1496 if (!pwqp)
1497 return NULL;
1498 if (isl_stream_eat(s, ')'))
1499 goto error;
1500 pow = optional_power(s);
1501 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1502 } else if (tok->type == ISL_TOKEN_VALUE) {
1503 struct isl_token *tok2;
1504 tok2 = isl_stream_next_token(s);
1505 isl_qpolynomial *qp;
1506 if (tok2 && tok2->type == '/') {
1507 isl_token_free(tok2);
1508 tok2 = next_token(s);
1509 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1510 isl_stream_error(s, tok2, "expected denominator");
1511 isl_token_free(tok);
1512 isl_token_free(tok2);
1513 return NULL;
1515 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1516 tok->u.v, tok2->u.v);
1517 isl_token_free(tok2);
1518 } else {
1519 isl_stream_push_token(s, tok2);
1520 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1521 tok->u.v);
1523 isl_token_free(tok);
1524 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1525 } else if (tok->type == ISL_TOKEN_INFTY) {
1526 isl_qpolynomial *qp;
1527 isl_token_free(tok);
1528 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1529 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1530 } else if (tok->type == ISL_TOKEN_NAN) {
1531 isl_qpolynomial *qp;
1532 isl_token_free(tok);
1533 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1534 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1535 } else if (tok->type == ISL_TOKEN_IDENT) {
1536 int n = v->n;
1537 int pos = vars_pos(v, tok->u.s, -1);
1538 int pow;
1539 isl_qpolynomial *qp;
1540 if (pos < 0) {
1541 isl_token_free(tok);
1542 return NULL;
1544 if (pos >= n) {
1545 vars_drop(v, v->n - n);
1546 isl_stream_error(s, tok, "unknown identifier");
1547 isl_token_free(tok);
1548 return NULL;
1550 isl_token_free(tok);
1551 pow = optional_power(s);
1552 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1553 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1554 } else if (tok->type == '[') {
1555 isl_pw_aff *pwaff;
1556 int pow;
1558 isl_stream_push_token(s, tok);
1559 pwaff = accept_affine(s, isl_map_get_space(map), v);
1560 pow = optional_power(s);
1561 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1562 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1563 } else if (tok->type == '-') {
1564 isl_token_free(tok);
1565 pwqp = read_factor(s, map, v);
1566 pwqp = isl_pw_qpolynomial_neg(pwqp);
1567 } else {
1568 isl_stream_error(s, tok, "unexpected isl_token");
1569 isl_stream_push_token(s, tok);
1570 return NULL;
1573 if (isl_stream_eat_if_available(s, '*') ||
1574 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1575 isl_pw_qpolynomial *pwqp2;
1577 pwqp2 = read_factor(s, map, v);
1578 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1581 return pwqp;
1582 error:
1583 isl_pw_qpolynomial_free(pwqp);
1584 return NULL;
1587 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1588 __isl_keep isl_map *map, struct vars *v)
1590 struct isl_token *tok;
1591 isl_pw_qpolynomial *pwqp;
1593 pwqp = read_factor(s, map, v);
1595 for (;;) {
1596 tok = next_token(s);
1597 if (!tok)
1598 return pwqp;
1600 if (tok->type == '+') {
1601 isl_pw_qpolynomial *pwqp2;
1603 isl_token_free(tok);
1604 pwqp2 = read_factor(s, map, v);
1605 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1606 } else if (tok->type == '-') {
1607 isl_pw_qpolynomial *pwqp2;
1609 isl_token_free(tok);
1610 pwqp2 = read_factor(s, map, v);
1611 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1612 } else if (tok->type == ISL_TOKEN_VALUE &&
1613 isl_int_is_neg(tok->u.v)) {
1614 isl_pw_qpolynomial *pwqp2;
1616 isl_stream_push_token(s, tok);
1617 pwqp2 = read_factor(s, map, v);
1618 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1619 } else {
1620 isl_stream_push_token(s, tok);
1621 break;
1625 return pwqp;
1628 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1629 __isl_take isl_map *map, struct vars *v)
1631 struct isl_token *tok;
1633 tok = isl_stream_next_token(s);
1634 if (!tok) {
1635 isl_stream_error(s, NULL, "unexpected EOF");
1636 goto error;
1638 if (tok->type == ':' ||
1639 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1640 isl_token_free(tok);
1641 map = read_disjuncts(s, v, map);
1642 } else
1643 isl_stream_push_token(s, tok);
1645 return map;
1646 error:
1647 isl_map_free(map);
1648 return NULL;
1651 static struct isl_obj obj_read_poly(struct isl_stream *s,
1652 __isl_take isl_map *map, struct vars *v, int n)
1654 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1655 isl_pw_qpolynomial *pwqp;
1656 struct isl_set *set;
1658 pwqp = read_term(s, map, v);
1659 map = read_optional_disjuncts(s, map, v);
1660 set = isl_map_range(map);
1662 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1664 vars_drop(v, v->n - n);
1666 obj.v = pwqp;
1667 return obj;
1670 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1671 __isl_take isl_set *set, struct vars *v, int n)
1673 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1674 isl_pw_qpolynomial *pwqp;
1675 isl_pw_qpolynomial_fold *pwf = NULL;
1677 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1678 return obj_read_poly(s, set, v, n);
1680 if (isl_stream_eat(s, '('))
1681 goto error;
1683 pwqp = read_term(s, set, v);
1684 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1686 while (isl_stream_eat_if_available(s, ',')) {
1687 isl_pw_qpolynomial_fold *pwf_i;
1688 pwqp = read_term(s, set, v);
1689 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1690 pwqp);
1691 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1694 if (isl_stream_eat(s, ')'))
1695 goto error;
1697 set = read_optional_disjuncts(s, set, v);
1698 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1700 vars_drop(v, v->n - n);
1702 obj.v = pwf;
1703 return obj;
1704 error:
1705 isl_set_free(set);
1706 isl_pw_qpolynomial_fold_free(pwf);
1707 obj.type = isl_obj_none;
1708 return obj;
1711 static int is_rational(struct isl_stream *s)
1713 struct isl_token *tok;
1715 tok = isl_stream_next_token(s);
1716 if (!tok)
1717 return 0;
1718 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1719 isl_token_free(tok);
1720 isl_stream_eat(s, ':');
1721 return 1;
1724 isl_stream_push_token(s, tok);
1726 return 0;
1729 static struct isl_obj obj_read_body(struct isl_stream *s,
1730 __isl_take isl_map *map, struct vars *v)
1732 struct isl_token *tok;
1733 struct isl_obj obj = { isl_obj_set, NULL };
1734 int n = v->n;
1736 if (is_rational(s))
1737 map = isl_map_set_rational(map);
1739 if (isl_stream_next_token_is(s, ':')) {
1740 obj.type = isl_obj_set;
1741 obj.v = read_optional_disjuncts(s, map, v);
1742 return obj;
1745 if (!next_is_tuple(s))
1746 return obj_read_poly_or_fold(s, map, v, n);
1748 map = read_tuple(s, map, isl_dim_in, v);
1749 if (!map)
1750 goto error;
1751 tok = isl_stream_next_token(s);
1752 if (tok && tok->type == ISL_TOKEN_TO) {
1753 obj.type = isl_obj_map;
1754 isl_token_free(tok);
1755 if (!next_is_tuple(s)) {
1756 isl_set *set = isl_map_domain(map);
1757 return obj_read_poly_or_fold(s, set, v, n);
1759 map = read_tuple(s, map, isl_dim_out, v);
1760 if (!map)
1761 goto error;
1762 } else {
1763 map = isl_map_reverse(map);
1764 if (tok)
1765 isl_stream_push_token(s, tok);
1768 map = read_optional_disjuncts(s, map, v);
1770 vars_drop(v, v->n - n);
1772 obj.v = map;
1773 return obj;
1774 error:
1775 isl_map_free(map);
1776 obj.type = isl_obj_none;
1777 return obj;
1780 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
1782 if (obj.type == isl_obj_map) {
1783 obj.v = isl_union_map_from_map(obj.v);
1784 obj.type = isl_obj_union_map;
1785 } else if (obj.type == isl_obj_set) {
1786 obj.v = isl_union_set_from_set(obj.v);
1787 obj.type = isl_obj_union_set;
1788 } else if (obj.type == isl_obj_pw_qpolynomial) {
1789 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
1790 obj.type = isl_obj_union_pw_qpolynomial;
1791 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
1792 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
1793 obj.type = isl_obj_union_pw_qpolynomial_fold;
1794 } else
1795 isl_assert(ctx, 0, goto error);
1796 return obj;
1797 error:
1798 obj.type->free(obj.v);
1799 obj.type = isl_obj_none;
1800 return obj;
1803 static struct isl_obj obj_add(struct isl_ctx *ctx,
1804 struct isl_obj obj1, struct isl_obj obj2)
1806 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
1807 obj1 = to_union(ctx, obj1);
1808 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
1809 obj2 = to_union(ctx, obj2);
1810 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
1811 obj1 = to_union(ctx, obj1);
1812 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
1813 obj2 = to_union(ctx, obj2);
1814 if (obj1.type == isl_obj_pw_qpolynomial &&
1815 obj2.type == isl_obj_union_pw_qpolynomial)
1816 obj1 = to_union(ctx, obj1);
1817 if (obj1.type == isl_obj_union_pw_qpolynomial &&
1818 obj2.type == isl_obj_pw_qpolynomial)
1819 obj2 = to_union(ctx, obj2);
1820 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1821 obj2.type == isl_obj_union_pw_qpolynomial_fold)
1822 obj1 = to_union(ctx, obj1);
1823 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
1824 obj2.type == isl_obj_pw_qpolynomial_fold)
1825 obj2 = to_union(ctx, obj2);
1826 isl_assert(ctx, obj1.type == obj2.type, goto error);
1827 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
1828 obj1 = to_union(ctx, obj1);
1829 obj2 = to_union(ctx, obj2);
1831 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
1832 obj1 = to_union(ctx, obj1);
1833 obj2 = to_union(ctx, obj2);
1835 if (obj1.type == isl_obj_pw_qpolynomial &&
1836 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
1837 obj1 = to_union(ctx, obj1);
1838 obj2 = to_union(ctx, obj2);
1840 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
1841 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
1842 obj1 = to_union(ctx, obj1);
1843 obj2 = to_union(ctx, obj2);
1845 obj1.v = obj1.type->add(obj1.v, obj2.v);
1846 return obj1;
1847 error:
1848 obj1.type->free(obj1.v);
1849 obj2.type->free(obj2.v);
1850 obj1.type = isl_obj_none;
1851 obj1.v = NULL;
1852 return obj1;
1855 static struct isl_obj obj_read(struct isl_stream *s, int nparam)
1857 isl_map *map = NULL;
1858 struct isl_token *tok;
1859 struct vars *v = NULL;
1860 struct isl_obj obj = { isl_obj_set, NULL };
1862 tok = next_token(s);
1863 if (!tok) {
1864 isl_stream_error(s, NULL, "unexpected EOF");
1865 goto error;
1867 if (tok->type == ISL_TOKEN_VALUE) {
1868 struct isl_token *tok2;
1869 struct isl_map *map;
1871 tok2 = isl_stream_next_token(s);
1872 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
1873 isl_int_is_neg(tok2->u.v)) {
1874 if (tok2)
1875 isl_stream_push_token(s, tok2);
1876 obj.type = isl_obj_int;
1877 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
1878 isl_token_free(tok);
1879 return obj;
1881 isl_stream_push_token(s, tok2);
1882 isl_stream_push_token(s, tok);
1883 map = map_read_polylib(s, nparam);
1884 if (!map)
1885 goto error;
1886 if (isl_map_may_be_set(map))
1887 obj.v = isl_map_range(map);
1888 else {
1889 obj.type = isl_obj_map;
1890 obj.v = map;
1892 return obj;
1894 v = vars_new(s->ctx);
1895 if (!v) {
1896 isl_stream_push_token(s, tok);
1897 goto error;
1899 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
1900 if (tok->type == '[') {
1901 isl_stream_push_token(s, tok);
1902 map = read_tuple(s, map, isl_dim_param, v);
1903 if (!map)
1904 goto error;
1905 if (nparam >= 0)
1906 isl_assert(s->ctx, nparam == v->n, goto error);
1907 tok = isl_stream_next_token(s);
1908 if (!tok || tok->type != ISL_TOKEN_TO) {
1909 isl_stream_error(s, tok, "expecting '->'");
1910 if (tok)
1911 isl_stream_push_token(s, tok);
1912 goto error;
1914 isl_token_free(tok);
1915 tok = isl_stream_next_token(s);
1916 } else if (nparam > 0)
1917 map = isl_map_add_dims(map, isl_dim_param, nparam);
1918 if (!tok || tok->type != '{') {
1919 isl_stream_error(s, tok, "expecting '{'");
1920 if (tok)
1921 isl_stream_push_token(s, tok);
1922 goto error;
1924 isl_token_free(tok);
1926 tok = isl_stream_next_token(s);
1927 if (!tok)
1929 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
1930 isl_token_free(tok);
1931 if (isl_stream_eat(s, '='))
1932 goto error;
1933 map = read_tuple(s, map, isl_dim_param, v);
1934 if (!map)
1935 goto error;
1936 if (nparam >= 0)
1937 isl_assert(s->ctx, nparam == v->n, goto error);
1938 } else if (tok->type == '}') {
1939 obj.type = isl_obj_union_set;
1940 obj.v = isl_union_set_empty(isl_map_get_space(map));
1941 isl_token_free(tok);
1942 goto done;
1943 } else
1944 isl_stream_push_token(s, tok);
1946 for (;;) {
1947 struct isl_obj o;
1948 tok = NULL;
1949 o = obj_read_body(s, isl_map_copy(map), v);
1950 if (o.type == isl_obj_none || !o.v)
1951 goto error;
1952 if (!obj.v)
1953 obj = o;
1954 else {
1955 obj = obj_add(s->ctx, obj, o);
1956 if (obj.type == isl_obj_none || !obj.v)
1957 goto error;
1959 tok = isl_stream_next_token(s);
1960 if (!tok || tok->type != ';')
1961 break;
1962 isl_token_free(tok);
1963 if (isl_stream_next_token_is(s, '}')) {
1964 tok = isl_stream_next_token(s);
1965 break;
1969 if (tok && tok->type == '}') {
1970 isl_token_free(tok);
1971 } else {
1972 isl_stream_error(s, tok, "unexpected isl_token");
1973 if (tok)
1974 isl_token_free(tok);
1975 goto error;
1977 done:
1978 vars_free(v);
1979 isl_map_free(map);
1981 return obj;
1982 error:
1983 isl_map_free(map);
1984 obj.type->free(obj.v);
1985 if (v)
1986 vars_free(v);
1987 obj.v = NULL;
1988 return obj;
1991 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
1993 return obj_read(s, -1);
1996 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s, int nparam)
1998 struct isl_obj obj;
2000 obj = obj_read(s, nparam);
2001 if (obj.v)
2002 isl_assert(s->ctx, obj.type == isl_obj_map ||
2003 obj.type == isl_obj_set, goto error);
2005 return obj.v;
2006 error:
2007 obj.type->free(obj.v);
2008 return NULL;
2011 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s, int nparam)
2013 struct isl_obj obj;
2015 obj = obj_read(s, nparam);
2016 if (obj.v) {
2017 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2018 obj.v = isl_map_range(obj.v);
2019 obj.type = isl_obj_set;
2021 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2024 return obj.v;
2025 error:
2026 obj.type->free(obj.v);
2027 return NULL;
2030 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2032 struct isl_obj obj;
2034 obj = obj_read(s, -1);
2035 if (obj.type == isl_obj_map) {
2036 obj.type = isl_obj_union_map;
2037 obj.v = isl_union_map_from_map(obj.v);
2039 if (obj.type == isl_obj_set) {
2040 obj.type = isl_obj_union_set;
2041 obj.v = isl_union_set_from_set(obj.v);
2043 if (obj.v)
2044 isl_assert(s->ctx, obj.type == isl_obj_union_map ||
2045 obj.type == isl_obj_union_set, goto error);
2047 return obj.v;
2048 error:
2049 obj.type->free(obj.v);
2050 return NULL;
2053 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2055 struct isl_obj obj;
2057 obj = obj_read(s, -1);
2058 if (obj.type == isl_obj_set) {
2059 obj.type = isl_obj_union_set;
2060 obj.v = isl_union_set_from_set(obj.v);
2062 if (obj.v)
2063 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2065 return obj.v;
2066 error:
2067 obj.type->free(obj.v);
2068 return NULL;
2071 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
2073 struct isl_obj obj;
2074 struct isl_map *map;
2075 struct isl_basic_map *bmap;
2077 obj = obj_read(s, nparam);
2078 map = obj.v;
2079 if (!map)
2080 return NULL;
2082 isl_assert(map->ctx, map->n <= 1, goto error);
2084 if (map->n == 0)
2085 bmap = isl_basic_map_empty_like_map(map);
2086 else
2087 bmap = isl_basic_map_copy(map->p[0]);
2089 isl_map_free(map);
2091 return bmap;
2092 error:
2093 isl_map_free(map);
2094 return NULL;
2097 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s,
2098 int nparam)
2100 isl_basic_map *bmap;
2101 bmap = basic_map_read(s, nparam);
2102 if (!bmap)
2103 return NULL;
2104 if (!isl_basic_map_may_be_set(bmap))
2105 isl_die(s->ctx, isl_error_invalid,
2106 "input is not a set", goto error);
2107 return isl_basic_map_range(bmap);
2108 error:
2109 isl_basic_map_free(bmap);
2110 return NULL;
2113 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2114 FILE *input, int nparam)
2116 struct isl_basic_map *bmap;
2117 struct isl_stream *s = isl_stream_new_file(ctx, input);
2118 if (!s)
2119 return NULL;
2120 bmap = basic_map_read(s, nparam);
2121 isl_stream_free(s);
2122 return bmap;
2125 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2126 FILE *input, int nparam)
2128 isl_basic_set *bset;
2129 struct isl_stream *s = isl_stream_new_file(ctx, input);
2130 if (!s)
2131 return NULL;
2132 bset = basic_set_read(s, nparam);
2133 isl_stream_free(s);
2134 return bset;
2137 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2138 const char *str, int nparam)
2140 struct isl_basic_map *bmap;
2141 struct isl_stream *s = isl_stream_new_str(ctx, str);
2142 if (!s)
2143 return NULL;
2144 bmap = basic_map_read(s, nparam);
2145 isl_stream_free(s);
2146 return bmap;
2149 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2150 const char *str, int nparam)
2152 isl_basic_set *bset;
2153 struct isl_stream *s = isl_stream_new_str(ctx, str);
2154 if (!s)
2155 return NULL;
2156 bset = basic_set_read(s, nparam);
2157 isl_stream_free(s);
2158 return bset;
2161 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2162 FILE *input, int nparam)
2164 struct isl_map *map;
2165 struct isl_stream *s = isl_stream_new_file(ctx, input);
2166 if (!s)
2167 return NULL;
2168 map = isl_stream_read_map(s, nparam);
2169 isl_stream_free(s);
2170 return map;
2173 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2174 const char *str, int nparam)
2176 struct isl_map *map;
2177 struct isl_stream *s = isl_stream_new_str(ctx, str);
2178 if (!s)
2179 return NULL;
2180 map = isl_stream_read_map(s, nparam);
2181 isl_stream_free(s);
2182 return map;
2185 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2186 FILE *input, int nparam)
2188 isl_set *set;
2189 struct isl_stream *s = isl_stream_new_file(ctx, input);
2190 if (!s)
2191 return NULL;
2192 set = isl_stream_read_set(s, nparam);
2193 isl_stream_free(s);
2194 return set;
2197 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2198 const char *str, int nparam)
2200 isl_set *set;
2201 struct isl_stream *s = isl_stream_new_str(ctx, str);
2202 if (!s)
2203 return NULL;
2204 set = isl_stream_read_set(s, nparam);
2205 isl_stream_free(s);
2206 return set;
2209 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2210 FILE *input)
2212 isl_union_map *umap;
2213 struct isl_stream *s = isl_stream_new_file(ctx, input);
2214 if (!s)
2215 return NULL;
2216 umap = isl_stream_read_union_map(s);
2217 isl_stream_free(s);
2218 return umap;
2221 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2222 const char *str)
2224 isl_union_map *umap;
2225 struct isl_stream *s = isl_stream_new_str(ctx, str);
2226 if (!s)
2227 return NULL;
2228 umap = isl_stream_read_union_map(s);
2229 isl_stream_free(s);
2230 return umap;
2233 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2234 FILE *input)
2236 isl_union_set *uset;
2237 struct isl_stream *s = isl_stream_new_file(ctx, input);
2238 if (!s)
2239 return NULL;
2240 uset = isl_stream_read_union_set(s);
2241 isl_stream_free(s);
2242 return uset;
2245 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2246 const char *str)
2248 isl_union_set *uset;
2249 struct isl_stream *s = isl_stream_new_str(ctx, str);
2250 if (!s)
2251 return NULL;
2252 uset = isl_stream_read_union_set(s);
2253 isl_stream_free(s);
2254 return uset;
2257 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2259 struct isl_vec *vec = NULL;
2260 struct isl_token *tok;
2261 unsigned size;
2262 int j;
2264 tok = isl_stream_next_token(s);
2265 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2266 isl_stream_error(s, tok, "expecting vector length");
2267 goto error;
2270 size = isl_int_get_si(tok->u.v);
2271 isl_token_free(tok);
2273 vec = isl_vec_alloc(s->ctx, size);
2275 for (j = 0; j < size; ++j) {
2276 tok = isl_stream_next_token(s);
2277 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2278 isl_stream_error(s, tok, "expecting constant value");
2279 goto error;
2281 isl_int_set(vec->el[j], tok->u.v);
2282 isl_token_free(tok);
2285 return vec;
2286 error:
2287 isl_token_free(tok);
2288 isl_vec_free(vec);
2289 return NULL;
2292 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2294 return isl_vec_read_polylib(s);
2297 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2299 isl_vec *v;
2300 struct isl_stream *s = isl_stream_new_file(ctx, input);
2301 if (!s)
2302 return NULL;
2303 v = vec_read(s);
2304 isl_stream_free(s);
2305 return v;
2308 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2309 struct isl_stream *s)
2311 struct isl_obj obj;
2313 obj = obj_read(s, -1);
2314 if (obj.v)
2315 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2316 goto error);
2318 return obj.v;
2319 error:
2320 obj.type->free(obj.v);
2321 return NULL;
2324 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2325 const char *str)
2327 isl_pw_qpolynomial *pwqp;
2328 struct isl_stream *s = isl_stream_new_str(ctx, str);
2329 if (!s)
2330 return NULL;
2331 pwqp = isl_stream_read_pw_qpolynomial(s);
2332 isl_stream_free(s);
2333 return pwqp;
2336 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2337 FILE *input)
2339 isl_pw_qpolynomial *pwqp;
2340 struct isl_stream *s = isl_stream_new_file(ctx, input);
2341 if (!s)
2342 return NULL;
2343 pwqp = isl_stream_read_pw_qpolynomial(s);
2344 isl_stream_free(s);
2345 return pwqp;