add isl_val_list
[isl.git] / isl_input.c
blob164a269f3b1d7283e8612a0f5c804e4745be5a06
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl/set.h>
21 #include <isl/seq.h>
22 #include <isl_stream_private.h>
23 #include <isl/obj.h>
24 #include "isl_polynomial_private.h"
25 #include <isl/union_map.h>
26 #include <isl_mat_private.h>
27 #include <isl_aff_private.h>
28 #include <isl/list.h>
29 #include <isl_val_private.h>
31 struct variable {
32 char *name;
33 int pos;
34 struct variable *next;
37 struct vars {
38 struct isl_ctx *ctx;
39 int n;
40 struct variable *v;
43 static struct vars *vars_new(struct isl_ctx *ctx)
45 struct vars *v;
46 v = isl_alloc_type(ctx, struct vars);
47 if (!v)
48 return NULL;
49 v->ctx = ctx;
50 v->n = 0;
51 v->v = NULL;
52 return v;
55 static void variable_free(struct variable *var)
57 while (var) {
58 struct variable *next = var->next;
59 free(var->name);
60 free(var);
61 var = next;
65 static void vars_free(struct vars *v)
67 if (!v)
68 return;
69 variable_free(v->v);
70 free(v);
73 static void vars_drop(struct vars *v, int n)
75 struct variable *var;
77 if (!v || !v->v)
78 return;
80 v->n -= n;
82 var = v->v;
83 while (--n >= 0) {
84 struct variable *next = var->next;
85 free(var->name);
86 free(var);
87 var = next;
89 v->v = var;
92 static struct variable *variable_new(struct vars *v, const char *name, int len,
93 int pos)
95 struct variable *var;
96 var = isl_calloc_type(v->ctx, struct variable);
97 if (!var)
98 goto error;
99 var->name = strdup(name);
100 var->name[len] = '\0';
101 var->pos = pos;
102 var->next = v->v;
103 return var;
104 error:
105 variable_free(v->v);
106 return NULL;
109 static int vars_pos(struct vars *v, const char *s, int len)
111 int pos;
112 struct variable *q;
114 if (len == -1)
115 len = strlen(s);
116 for (q = v->v; q; q = q->next) {
117 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
118 break;
120 if (q)
121 pos = q->pos;
122 else {
123 pos = v->n;
124 v->v = variable_new(v, s, len, v->n);
125 if (!v->v)
126 return -1;
127 v->n++;
129 return pos;
132 static int vars_add_anon(struct vars *v)
134 v->v = variable_new(v, "", 0, v->n);
136 if (!v->v)
137 return -1;
138 v->n++;
140 return 0;
143 /* Obtain next token, with some preprocessing.
144 * In particular, evaluate expressions of the form x^y,
145 * with x and y values.
147 static struct isl_token *next_token(struct isl_stream *s)
149 struct isl_token *tok, *tok2;
151 tok = isl_stream_next_token(s);
152 if (!tok || tok->type != ISL_TOKEN_VALUE)
153 return tok;
154 if (!isl_stream_eat_if_available(s, '^'))
155 return tok;
156 tok2 = isl_stream_next_token(s);
157 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
158 isl_stream_error(s, tok2, "expecting constant value");
159 goto error;
162 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
164 isl_token_free(tok2);
165 return tok;
166 error:
167 isl_token_free(tok);
168 isl_token_free(tok2);
169 return NULL;
172 /* Read an isl_val from "s".
174 * The following token sequences are recognized
176 * "infty" -> infty
177 * "-" "infty" -> -infty
178 * "NaN" -> NaN
179 * n "/" d -> n/d
180 * v -> v
182 * where n, d and v are integer constants.
184 __isl_give isl_val *isl_stream_read_val(struct isl_stream *s)
186 struct isl_token *tok = NULL;
187 struct isl_token *tok2 = NULL;
188 isl_val *val;
190 tok = next_token(s);
191 if (!tok) {
192 isl_stream_error(s, NULL, "unexpected EOF");
193 goto error;
195 if (tok->type == ISL_TOKEN_INFTY) {
196 isl_token_free(tok);
197 return isl_val_infty(s->ctx);
199 if (tok->type == '-' &&
200 isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
201 isl_token_free(tok);
202 return isl_val_neginfty(s->ctx);
204 if (tok->type == ISL_TOKEN_NAN) {
205 isl_token_free(tok);
206 return isl_val_nan(s->ctx);
208 if (tok->type != ISL_TOKEN_VALUE) {
209 isl_stream_error(s, tok, "expecting value");
210 goto error;
213 if (isl_stream_eat_if_available(s, '/')) {
214 tok2 = next_token(s);
215 if (!tok2) {
216 isl_stream_error(s, NULL, "unexpected EOF");
217 goto error;
219 if (tok2->type != ISL_TOKEN_VALUE) {
220 isl_stream_error(s, tok2, "expecting value");
221 goto error;
223 val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
224 val = isl_val_normalize(val);
225 } else {
226 val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
229 isl_token_free(tok);
230 isl_token_free(tok2);
231 return val;
232 error:
233 isl_token_free(tok);
234 isl_token_free(tok2);
235 return NULL;
238 /* Read an isl_val from "str".
240 struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
241 const char *str)
243 isl_val *val;
244 struct isl_stream *s = isl_stream_new_str(ctx, str);
245 if (!s)
246 return NULL;
247 val = isl_stream_read_val(s);
248 isl_stream_free(s);
249 return val;
252 static int accept_cst_factor(struct isl_stream *s, isl_int *f)
254 struct isl_token *tok;
256 tok = next_token(s);
257 if (!tok || tok->type != ISL_TOKEN_VALUE) {
258 isl_stream_error(s, tok, "expecting constant value");
259 goto error;
262 isl_int_mul(*f, *f, tok->u.v);
264 isl_token_free(tok);
266 if (isl_stream_eat_if_available(s, '*'))
267 return accept_cst_factor(s, f);
269 return 0;
270 error:
271 isl_token_free(tok);
272 return -1;
275 /* Given an affine expression aff, return an affine expression
276 * for aff % d, with d the next token on the stream, which is
277 * assumed to be a constant.
279 * We introduce an integer division q = [aff/d] and the result
280 * is set to aff - d q.
282 static __isl_give isl_pw_aff *affine_mod(struct isl_stream *s,
283 struct vars *v, __isl_take isl_pw_aff *aff)
285 struct isl_token *tok;
286 isl_pw_aff *q;
288 tok = next_token(s);
289 if (!tok || tok->type != ISL_TOKEN_VALUE) {
290 isl_stream_error(s, tok, "expecting constant value");
291 goto error;
294 q = isl_pw_aff_copy(aff);
295 q = isl_pw_aff_scale_down(q, tok->u.v);
296 q = isl_pw_aff_floor(q);
297 q = isl_pw_aff_scale(q, tok->u.v);
299 aff = isl_pw_aff_sub(aff, q);
301 isl_token_free(tok);
302 return aff;
303 error:
304 isl_pw_aff_free(aff);
305 isl_token_free(tok);
306 return NULL;
309 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
310 __isl_take isl_space *dim, struct vars *v);
311 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
312 __isl_take isl_space *dim, struct vars *v);
314 static __isl_give isl_pw_aff *accept_minmax(struct isl_stream *s,
315 __isl_take isl_space *dim, struct vars *v)
317 struct isl_token *tok;
318 isl_pw_aff_list *list = NULL;
319 int min;
321 tok = isl_stream_next_token(s);
322 if (!tok)
323 goto error;
324 min = tok->type == ISL_TOKEN_MIN;
325 isl_token_free(tok);
327 if (isl_stream_eat(s, '('))
328 goto error;
330 list = accept_affine_list(s, isl_space_copy(dim), v);
331 if (!list)
332 goto error;
334 if (isl_stream_eat(s, ')'))
335 goto error;
337 isl_space_free(dim);
338 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
339 error:
340 isl_space_free(dim);
341 isl_pw_aff_list_free(list);
342 return NULL;
345 static __isl_give isl_pw_aff *accept_div(struct isl_stream *s,
346 __isl_take isl_space *dim, struct vars *v)
348 struct isl_token *tok;
349 int f = 0;
350 int c = 0;
351 isl_pw_aff *pwaff = NULL;
353 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
354 f = 1;
355 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
356 c = 1;
357 if (f || c) {
358 if (isl_stream_eat(s, '('))
359 goto error;
360 } else {
361 if (isl_stream_eat(s, '['))
362 goto error;
365 pwaff = accept_affine(s, isl_space_copy(dim), v);
367 if (f || c) {
368 if (isl_stream_eat(s, ','))
369 goto error;
371 tok = next_token(s);
372 if (!tok)
373 goto error;
374 if (tok->type != ISL_TOKEN_VALUE) {
375 isl_stream_error(s, tok, "expected denominator");
376 isl_stream_push_token(s, tok);
377 goto error;
379 isl_pw_aff_scale_down(pwaff, tok->u.v);
380 isl_token_free(tok);
383 if (c)
384 pwaff = isl_pw_aff_ceil(pwaff);
385 else
386 pwaff = isl_pw_aff_floor(pwaff);
388 if (f || c) {
389 if (isl_stream_eat(s, ')'))
390 goto error;
391 } else {
392 if (isl_stream_eat(s, ']'))
393 goto error;
396 isl_space_free(dim);
397 return pwaff;
398 error:
399 isl_space_free(dim);
400 isl_pw_aff_free(pwaff);
401 return NULL;
404 static __isl_give isl_pw_aff *accept_affine_factor(struct isl_stream *s,
405 __isl_take isl_space *dim, struct vars *v)
407 struct isl_token *tok = NULL;
408 isl_pw_aff *res = NULL;
410 tok = next_token(s);
411 if (!tok) {
412 isl_stream_error(s, NULL, "unexpected EOF");
413 goto error;
416 if (tok->type == ISL_TOKEN_AFF) {
417 res = isl_pw_aff_copy(tok->u.pwaff);
418 isl_token_free(tok);
419 } else if (tok->type == ISL_TOKEN_IDENT) {
420 int n = v->n;
421 int pos = vars_pos(v, tok->u.s, -1);
422 isl_aff *aff;
424 if (pos < 0)
425 goto error;
426 if (pos >= n) {
427 vars_drop(v, v->n - n);
428 isl_stream_error(s, tok, "unknown identifier");
429 goto error;
432 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
433 if (!aff)
434 goto error;
435 isl_int_set_si(aff->v->el[2 + pos], 1);
436 res = isl_pw_aff_from_aff(aff);
437 isl_token_free(tok);
438 } else if (tok->type == ISL_TOKEN_VALUE) {
439 if (isl_stream_eat_if_available(s, '*')) {
440 res = accept_affine_factor(s, isl_space_copy(dim), v);
441 res = isl_pw_aff_scale(res, tok->u.v);
442 } else {
443 isl_local_space *ls;
444 isl_aff *aff;
445 ls = isl_local_space_from_space(isl_space_copy(dim));
446 aff = isl_aff_zero_on_domain(ls);
447 aff = isl_aff_add_constant(aff, tok->u.v);
448 res = isl_pw_aff_from_aff(aff);
450 isl_token_free(tok);
451 } else if (tok->type == '(') {
452 isl_token_free(tok);
453 tok = NULL;
454 res = accept_affine(s, isl_space_copy(dim), v);
455 if (!res)
456 goto error;
457 if (isl_stream_eat(s, ')'))
458 goto error;
459 } else if (tok->type == '[' ||
460 tok->type == ISL_TOKEN_FLOORD ||
461 tok->type == ISL_TOKEN_CEILD) {
462 isl_stream_push_token(s, tok);
463 tok = NULL;
464 res = accept_div(s, isl_space_copy(dim), v);
465 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
466 isl_stream_push_token(s, tok);
467 tok = NULL;
468 res = accept_minmax(s, isl_space_copy(dim), v);
469 } else {
470 isl_stream_error(s, tok, "expecting factor");
471 goto error;
473 if (isl_stream_eat_if_available(s, '%') ||
474 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
475 isl_space_free(dim);
476 return affine_mod(s, v, res);
478 if (isl_stream_eat_if_available(s, '*')) {
479 isl_int f;
480 isl_int_init(f);
481 isl_int_set_si(f, 1);
482 if (accept_cst_factor(s, &f) < 0) {
483 isl_int_clear(f);
484 goto error2;
486 res = isl_pw_aff_scale(res, f);
487 isl_int_clear(f);
489 if (isl_stream_eat_if_available(s, '/')) {
490 isl_int f;
491 isl_int_init(f);
492 isl_int_set_si(f, 1);
493 if (accept_cst_factor(s, &f) < 0) {
494 isl_int_clear(f);
495 goto error2;
497 res = isl_pw_aff_scale_down(res, f);
498 isl_int_clear(f);
501 isl_space_free(dim);
502 return res;
503 error:
504 isl_token_free(tok);
505 error2:
506 isl_pw_aff_free(res);
507 isl_space_free(dim);
508 return NULL;
511 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
513 isl_aff *aff;
514 isl_space *space;
516 space = isl_pw_aff_get_domain_space(pwaff);
517 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
518 aff = isl_aff_add_constant(aff, v);
520 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
523 static __isl_give isl_pw_aff *accept_affine(struct isl_stream *s,
524 __isl_take isl_space *dim, struct vars *v)
526 struct isl_token *tok = NULL;
527 isl_local_space *ls;
528 isl_pw_aff *res;
529 int sign = 1;
531 ls = isl_local_space_from_space(isl_space_copy(dim));
532 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
533 if (!res)
534 goto error;
536 for (;;) {
537 tok = next_token(s);
538 if (!tok) {
539 isl_stream_error(s, NULL, "unexpected EOF");
540 goto error;
542 if (tok->type == '-') {
543 sign = -sign;
544 isl_token_free(tok);
545 continue;
547 if (tok->type == '(' || tok->type == '[' ||
548 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
549 tok->type == ISL_TOKEN_FLOORD ||
550 tok->type == ISL_TOKEN_CEILD ||
551 tok->type == ISL_TOKEN_IDENT ||
552 tok->type == ISL_TOKEN_AFF) {
553 isl_pw_aff *term;
554 isl_stream_push_token(s, tok);
555 tok = NULL;
556 term = accept_affine_factor(s, isl_space_copy(dim), v);
557 if (sign < 0)
558 res = isl_pw_aff_sub(res, term);
559 else
560 res = isl_pw_aff_add(res, term);
561 if (!res)
562 goto error;
563 sign = 1;
564 } else if (tok->type == ISL_TOKEN_VALUE) {
565 if (sign < 0)
566 isl_int_neg(tok->u.v, tok->u.v);
567 if (isl_stream_eat_if_available(s, '*') ||
568 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
569 isl_pw_aff *term;
570 term = accept_affine_factor(s,
571 isl_space_copy(dim), v);
572 term = isl_pw_aff_scale(term, tok->u.v);
573 res = isl_pw_aff_add(res, term);
574 if (!res)
575 goto error;
576 } else {
577 res = add_cst(res, tok->u.v);
579 sign = 1;
580 } else {
581 isl_stream_error(s, tok, "unexpected isl_token");
582 isl_stream_push_token(s, tok);
583 isl_pw_aff_free(res);
584 isl_space_free(dim);
585 return NULL;
587 isl_token_free(tok);
589 tok = next_token(s);
590 if (tok && tok->type == '-') {
591 sign = -sign;
592 isl_token_free(tok);
593 } else if (tok && tok->type == '+') {
594 /* nothing */
595 isl_token_free(tok);
596 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
597 isl_int_is_neg(tok->u.v)) {
598 isl_stream_push_token(s, tok);
599 } else {
600 if (tok)
601 isl_stream_push_token(s, tok);
602 break;
606 isl_space_free(dim);
607 return res;
608 error:
609 isl_space_free(dim);
610 isl_token_free(tok);
611 isl_pw_aff_free(res);
612 return NULL;
615 static int is_comparator(struct isl_token *tok)
617 if (!tok)
618 return 0;
620 switch (tok->type) {
621 case ISL_TOKEN_LT:
622 case ISL_TOKEN_GT:
623 case ISL_TOKEN_LE:
624 case ISL_TOKEN_GE:
625 case ISL_TOKEN_NE:
626 case '=':
627 return 1;
628 default:
629 return 0;
633 static struct isl_map *read_disjuncts(struct isl_stream *s,
634 struct vars *v, __isl_take isl_map *map, int rational);
635 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
636 __isl_take isl_space *dim, struct vars *v, int rational);
638 /* Accept a ternary operator, given the first argument.
640 static __isl_give isl_pw_aff *accept_ternary(struct isl_stream *s,
641 __isl_take isl_map *cond, struct vars *v, int rational)
643 isl_space *dim;
644 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
646 if (!cond)
647 return NULL;
649 if (isl_stream_eat(s, '?'))
650 goto error;
652 dim = isl_space_wrap(isl_map_get_space(cond));
653 pwaff1 = accept_extended_affine(s, dim, v, rational);
654 if (!pwaff1)
655 goto error;
657 if (isl_stream_eat(s, ':'))
658 goto error;
660 dim = isl_pw_aff_get_domain_space(pwaff1);
661 pwaff2 = accept_extended_affine(s, dim, v, rational);
662 if (!pwaff1)
663 goto error;
665 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
666 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
667 error:
668 isl_map_free(cond);
669 isl_pw_aff_free(pwaff1);
670 isl_pw_aff_free(pwaff2);
671 return NULL;
674 /* Accept an affine expression that may involve ternary operators.
675 * We first read an affine expression.
676 * If it is not followed by a comparison operator, we simply return it.
677 * Otherwise, we assume the affine epxression is part of the first
678 * argument of a ternary operator and try to parse that.
680 static __isl_give isl_pw_aff *accept_extended_affine(struct isl_stream *s,
681 __isl_take isl_space *dim, struct vars *v, int rational)
683 isl_space *space;
684 isl_map *cond;
685 isl_pw_aff *pwaff;
686 struct isl_token *tok;
687 int line = -1, col = -1;
688 int is_comp;
690 tok = isl_stream_next_token(s);
691 if (tok) {
692 line = tok->line;
693 col = tok->col;
694 isl_stream_push_token(s, tok);
697 pwaff = accept_affine(s, dim, v);
698 if (rational)
699 pwaff = isl_pw_aff_set_rational(pwaff);
700 if (!pwaff)
701 return NULL;
703 tok = isl_stream_next_token(s);
704 if (!tok)
705 return isl_pw_aff_free(pwaff);
707 is_comp = is_comparator(tok);
708 isl_stream_push_token(s, tok);
709 if (!is_comp)
710 return pwaff;
712 tok = isl_token_new(s->ctx, line, col, 0);
713 if (!tok)
714 return isl_pw_aff_free(pwaff);
715 tok->type = ISL_TOKEN_AFF;
716 tok->u.pwaff = pwaff;
718 space = isl_pw_aff_get_domain_space(pwaff);
719 cond = isl_map_universe(isl_space_unwrap(space));
721 isl_stream_push_token(s, tok);
723 cond = read_disjuncts(s, v, cond, rational);
725 return accept_ternary(s, cond, v, rational);
728 static __isl_give isl_map *read_var_def(struct isl_stream *s,
729 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
730 int rational)
732 isl_pw_aff *def;
733 int pos;
734 isl_map *def_map;
736 if (type == isl_dim_param)
737 pos = isl_map_dim(map, isl_dim_param);
738 else {
739 pos = isl_map_dim(map, isl_dim_in);
740 if (type == isl_dim_out)
741 pos += isl_map_dim(map, isl_dim_out);
742 type = isl_dim_in;
744 --pos;
746 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
747 v, rational);
748 def_map = isl_map_from_pw_aff(def);
749 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
750 def_map = isl_set_unwrap(isl_map_domain(def_map));
752 map = isl_map_intersect(map, def_map);
754 return map;
757 static __isl_give isl_pw_aff_list *accept_affine_list(struct isl_stream *s,
758 __isl_take isl_space *dim, struct vars *v)
760 isl_pw_aff *pwaff;
761 isl_pw_aff_list *list;
762 struct isl_token *tok = NULL;
764 pwaff = accept_affine(s, isl_space_copy(dim), v);
765 list = isl_pw_aff_list_from_pw_aff(pwaff);
766 if (!list)
767 goto error;
769 for (;;) {
770 tok = isl_stream_next_token(s);
771 if (!tok) {
772 isl_stream_error(s, NULL, "unexpected EOF");
773 goto error;
775 if (tok->type != ',') {
776 isl_stream_push_token(s, tok);
777 break;
779 isl_token_free(tok);
781 pwaff = accept_affine(s, isl_space_copy(dim), v);
782 list = isl_pw_aff_list_concat(list,
783 isl_pw_aff_list_from_pw_aff(pwaff));
784 if (!list)
785 goto error;
788 isl_space_free(dim);
789 return list;
790 error:
791 isl_space_free(dim);
792 isl_pw_aff_list_free(list);
793 return NULL;
796 static __isl_give isl_map *read_defined_var_list(struct isl_stream *s,
797 struct vars *v, __isl_take isl_map *map, int rational)
799 struct isl_token *tok;
801 while ((tok = isl_stream_next_token(s)) != NULL) {
802 int p;
803 int n = v->n;
805 if (tok->type != ISL_TOKEN_IDENT)
806 break;
808 p = vars_pos(v, tok->u.s, -1);
809 if (p < 0)
810 goto error;
811 if (p < n) {
812 isl_stream_error(s, tok, "expecting unique identifier");
813 goto error;
816 map = isl_map_add_dims(map, isl_dim_out, 1);
818 isl_token_free(tok);
819 tok = isl_stream_next_token(s);
820 if (tok && tok->type == '=') {
821 isl_token_free(tok);
822 map = read_var_def(s, map, isl_dim_out, v, rational);
823 tok = isl_stream_next_token(s);
826 if (!tok || tok->type != ',')
827 break;
829 isl_token_free(tok);
831 if (tok)
832 isl_stream_push_token(s, tok);
834 return map;
835 error:
836 isl_token_free(tok);
837 isl_map_free(map);
838 return NULL;
841 static int next_is_tuple(struct isl_stream *s)
843 struct isl_token *tok;
844 int is_tuple;
846 tok = isl_stream_next_token(s);
847 if (!tok)
848 return 0;
849 if (tok->type == '[') {
850 isl_stream_push_token(s, tok);
851 return 1;
853 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
854 isl_stream_push_token(s, tok);
855 return 0;
858 is_tuple = isl_stream_next_token_is(s, '[');
860 isl_stream_push_token(s, tok);
862 return is_tuple;
865 /* Allocate an initial tuple with zero dimensions and an anonymous,
866 * unstructured space.
867 * A tuple is represented as an isl_multi_pw_aff.
868 * The range space is the space of the tuple.
869 * The domain space is an anonymous space
870 * with a dimension for each variable in the set of variables in "v".
871 * If a given dimension is not defined in terms of earlier dimensions in
872 * the input, then the corresponding isl_pw_aff is set equal to one time
873 * the variable corresponding to the dimension being defined.
875 static __isl_give isl_multi_pw_aff *tuple_alloc(struct vars *v)
877 return isl_multi_pw_aff_alloc(isl_space_alloc(v->ctx, 0, v->n, 0));
880 /* Is "pa" an expression in term of earlier dimensions?
881 * The alternative is that the dimension is defined to be equal to itself,
882 * meaning that it has a universe domain and an expression that depends
883 * on itself. "i" is the position of the expression in a sequence
884 * of "n" expressions. The final dimensions of "pa" correspond to
885 * these "n" expressions.
887 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
889 isl_aff *aff;
891 if (!pa)
892 return -1;
893 if (pa->n != 1)
894 return 1;
895 if (!isl_set_plain_is_universe(pa->p[0].set))
896 return 1;
898 aff = pa->p[0].aff;
899 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
900 return 1;
901 return 0;
904 /* Does the tuple contain any dimensions that are defined
905 * in terms of earlier dimensions?
907 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
909 int i, n;
910 int has_expr = 0;
911 isl_pw_aff *pa;
913 if (!tuple)
914 return -1;
915 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
916 for (i = 0; i < n; ++i) {
917 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
918 has_expr = pw_aff_is_expr(pa, i, n);
919 isl_pw_aff_free(pa);
920 if (has_expr < 0 || has_expr)
921 break;
924 return has_expr;
927 /* Add a dimension to the given tuple.
928 * The dimension is initially undefined, so it is encoded
929 * as one times itself.
931 static __isl_give isl_multi_pw_aff *tuple_add_dim(
932 __isl_take isl_multi_pw_aff *tuple, struct vars *v)
934 isl_space *space;
935 isl_aff *aff;
936 isl_pw_aff *pa;
938 tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
939 space = isl_multi_pw_aff_get_domain_space(tuple);
940 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
941 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
942 pa = isl_pw_aff_from_aff(aff);
943 tuple = isl_multi_pw_aff_flat_range_product(tuple,
944 isl_multi_pw_aff_from_pw_aff(pa));
946 return tuple;
949 /* Set the name of dimension "pos" in "tuple" to "name".
950 * During printing, we add primes if the same name appears more than once
951 * to distinguish the occurrences. Here, we remove those primes from "name"
952 * before setting the name of the dimension.
954 static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
955 __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
957 char *prime;
959 if (!name)
960 return tuple;
962 prime = strchr(name, '\'');
963 if (prime)
964 *prime = '\0';
965 tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
966 if (prime)
967 *prime = '\'';
969 return tuple;
972 /* Read an affine expression from "s" and replace the definition
973 * of dimension "pos" in "tuple" by this expression.
975 * accept_extended_affine requires a wrapped space as input.
976 * The domain space of "tuple", on the other hand is an anonymous space,
977 * so we have to adjust the space of the isl_pw_aff before adding it
978 * to "tuple".
980 static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
981 __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
982 int rational)
984 isl_space *space;
985 isl_pw_aff *def;
987 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
988 def = accept_extended_affine(s, space, v, rational);
989 space = isl_space_set_alloc(s->ctx, 0, v->n);
990 def = isl_pw_aff_reset_domain_space(def, space);
991 tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
993 return tuple;
996 /* Read a list of variables and/or affine expressions and return the list
997 * as an isl_multi_pw_aff.
998 * The elements in the list are separated by either "," or "][".
999 * If "comma" is set then only "," is allowed.
1001 static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
1002 struct vars *v, int rational, int comma)
1004 int i = 0;
1005 struct isl_token *tok;
1006 isl_multi_pw_aff *res;
1008 res = tuple_alloc(v);
1010 if (isl_stream_next_token_is(s, ']'))
1011 return res;
1013 while ((tok = next_token(s)) != NULL) {
1014 int new_name = 0;
1016 res = tuple_add_dim(res, v);
1018 if (tok->type == ISL_TOKEN_IDENT) {
1019 int n = v->n;
1020 int p = vars_pos(v, tok->u.s, -1);
1021 if (p < 0)
1022 goto error;
1023 new_name = p >= n;
1026 if (tok->type == '*') {
1027 if (vars_add_anon(v) < 0)
1028 goto error;
1029 isl_token_free(tok);
1030 } else if (new_name) {
1031 res = tuple_set_dim_name(res, i, v->v->name);
1032 isl_token_free(tok);
1033 if (isl_stream_eat_if_available(s, '='))
1034 res = read_tuple_var_def(s, res, i, v,
1035 rational);
1036 } else {
1037 isl_stream_push_token(s, tok);
1038 tok = NULL;
1039 if (vars_add_anon(v) < 0)
1040 goto error;
1041 res = read_tuple_var_def(s, res, i, v, rational);
1044 tok = isl_stream_next_token(s);
1045 if (!comma && tok && tok->type == ']' &&
1046 isl_stream_next_token_is(s, '[')) {
1047 isl_token_free(tok);
1048 tok = isl_stream_next_token(s);
1049 } else if (!tok || tok->type != ',')
1050 break;
1052 isl_token_free(tok);
1053 i++;
1055 if (tok)
1056 isl_stream_push_token(s, tok);
1058 return res;
1059 error:
1060 isl_token_free(tok);
1061 return isl_multi_pw_aff_free(res);
1064 /* Read a tuple and represent it as an isl_multi_pw_aff. See tuple_alloc.
1066 static __isl_give isl_multi_pw_aff *read_tuple(struct isl_stream *s,
1067 struct vars *v, int rational, int comma)
1069 struct isl_token *tok;
1070 char *name = NULL;
1071 isl_multi_pw_aff *res = NULL;
1073 tok = isl_stream_next_token(s);
1074 if (!tok)
1075 goto error;
1076 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1077 name = strdup(tok->u.s);
1078 isl_token_free(tok);
1079 if (!name)
1080 goto error;
1081 } else
1082 isl_stream_push_token(s, tok);
1083 if (isl_stream_eat(s, '['))
1084 goto error;
1085 if (next_is_tuple(s)) {
1086 isl_multi_pw_aff *out;
1087 int n;
1088 res = read_tuple(s, v, rational, comma);
1089 if (isl_stream_eat(s, ISL_TOKEN_TO))
1090 goto error;
1091 out = read_tuple(s, v, rational, comma);
1092 n = isl_multi_pw_aff_dim(out, isl_dim_out);
1093 res = isl_multi_pw_aff_add_dims(res, isl_dim_in, n);
1094 res = isl_multi_pw_aff_range_product(res, out);
1095 } else
1096 res = read_tuple_var_list(s, v, rational, comma);
1097 if (isl_stream_eat(s, ']'))
1098 goto error;
1100 if (name) {
1101 res = isl_multi_pw_aff_set_tuple_name(res, isl_dim_out, name);
1102 free(name);
1105 return res;
1106 error:
1107 free(name);
1108 return isl_multi_pw_aff_free(res);
1111 /* Read a tuple from "s" and add it to "map".
1112 * The tuple is initially represented as an isl_multi_pw_aff.
1113 * We first create the appropriate space in "map" based on the range
1114 * space of this isl_multi_pw_aff. Then, we add equalities based
1115 * on the affine expressions. These live in an anonymous space,
1116 * however, so we first need to reset the space to that of "map".
1118 static __isl_give isl_map *read_map_tuple(struct isl_stream *s,
1119 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1120 int rational, int comma)
1122 int i, n;
1123 isl_multi_pw_aff *tuple;
1124 isl_space *space = NULL;
1126 tuple = read_tuple(s, v, rational, comma);
1127 if (!tuple)
1128 goto error;
1130 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1131 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1132 if (!space)
1133 goto error;
1135 if (type == isl_dim_param) {
1136 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1137 isl_space_is_wrapping(space)) {
1138 isl_die(s->ctx, isl_error_invalid,
1139 "parameter tuples cannot be named or nested",
1140 goto error);
1142 map = isl_map_add_dims(map, type, n);
1143 for (i = 0; i < n; ++i) {
1144 isl_id *id;
1145 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1146 isl_die(s->ctx, isl_error_invalid,
1147 "parameters must be named",
1148 goto error);
1149 id = isl_space_get_dim_id(space, isl_dim_set, i);
1150 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1152 } else if (type == isl_dim_in) {
1153 isl_set *set;
1155 set = isl_set_universe(isl_space_copy(space));
1156 if (rational)
1157 set = isl_set_set_rational(set);
1158 set = isl_set_intersect_params(set, isl_map_params(map));
1159 map = isl_map_from_domain(set);
1160 } else {
1161 isl_set *set;
1163 set = isl_set_universe(isl_space_copy(space));
1164 if (rational)
1165 set = isl_set_set_rational(set);
1166 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1169 for (i = 0; i < n; ++i) {
1170 isl_pw_aff *pa;
1171 isl_space *space;
1172 isl_aff *aff;
1173 isl_set *set;
1174 isl_map *map_i;
1176 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1177 space = isl_pw_aff_get_domain_space(pa);
1178 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1179 aff = isl_aff_add_coefficient_si(aff,
1180 isl_dim_in, v->n - n + i, -1);
1181 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1182 if (rational)
1183 pa = isl_pw_aff_set_rational(pa);
1184 set = isl_pw_aff_zero_set(pa);
1185 map_i = isl_map_from_range(set);
1186 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1187 map = isl_map_intersect(map, map_i);
1190 isl_space_free(space);
1191 isl_multi_pw_aff_free(tuple);
1192 return map;
1193 error:
1194 isl_space_free(space);
1195 isl_multi_pw_aff_free(tuple);
1196 isl_map_free(map);
1197 return NULL;
1200 static __isl_give isl_set *construct_constraints(
1201 __isl_take isl_set *set, int type,
1202 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1203 int rational)
1205 isl_set *cond;
1207 left = isl_pw_aff_list_copy(left);
1208 right = isl_pw_aff_list_copy(right);
1209 if (rational) {
1210 left = isl_pw_aff_list_set_rational(left);
1211 right = isl_pw_aff_list_set_rational(right);
1213 if (type == ISL_TOKEN_LE)
1214 cond = isl_pw_aff_list_le_set(left, right);
1215 else if (type == ISL_TOKEN_GE)
1216 cond = isl_pw_aff_list_ge_set(left, right);
1217 else if (type == ISL_TOKEN_LT)
1218 cond = isl_pw_aff_list_lt_set(left, right);
1219 else if (type == ISL_TOKEN_GT)
1220 cond = isl_pw_aff_list_gt_set(left, right);
1221 else if (type == ISL_TOKEN_NE)
1222 cond = isl_pw_aff_list_ne_set(left, right);
1223 else
1224 cond = isl_pw_aff_list_eq_set(left, right);
1226 return isl_set_intersect(set, cond);
1229 static __isl_give isl_map *add_constraint(struct isl_stream *s,
1230 struct vars *v, __isl_take isl_map *map, int rational)
1232 struct isl_token *tok = NULL;
1233 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1234 isl_set *set;
1236 set = isl_map_wrap(map);
1237 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1238 if (!list1)
1239 goto error;
1240 tok = isl_stream_next_token(s);
1241 if (!is_comparator(tok)) {
1242 isl_stream_error(s, tok, "missing operator");
1243 if (tok)
1244 isl_stream_push_token(s, tok);
1245 tok = NULL;
1246 goto error;
1248 for (;;) {
1249 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1250 if (!list2)
1251 goto error;
1253 set = construct_constraints(set, tok->type, list1, list2,
1254 rational);
1255 isl_token_free(tok);
1256 isl_pw_aff_list_free(list1);
1257 list1 = list2;
1259 tok = isl_stream_next_token(s);
1260 if (!is_comparator(tok)) {
1261 if (tok)
1262 isl_stream_push_token(s, tok);
1263 break;
1266 isl_pw_aff_list_free(list1);
1268 return isl_set_unwrap(set);
1269 error:
1270 if (tok)
1271 isl_token_free(tok);
1272 isl_pw_aff_list_free(list1);
1273 isl_pw_aff_list_free(list2);
1274 isl_set_free(set);
1275 return NULL;
1278 static __isl_give isl_map *read_exists(struct isl_stream *s,
1279 struct vars *v, __isl_take isl_map *map, int rational)
1281 int n = v->n;
1282 int seen_paren = isl_stream_eat_if_available(s, '(');
1284 map = isl_map_from_domain(isl_map_wrap(map));
1285 map = read_defined_var_list(s, v, map, rational);
1287 if (isl_stream_eat(s, ':'))
1288 goto error;
1290 map = read_disjuncts(s, v, map, rational);
1291 map = isl_set_unwrap(isl_map_domain(map));
1293 vars_drop(v, v->n - n);
1294 if (seen_paren && isl_stream_eat(s, ')'))
1295 goto error;
1297 return map;
1298 error:
1299 isl_map_free(map);
1300 return NULL;
1303 /* Parse an expression between parentheses and push the result
1304 * back on the stream.
1306 * The parsed expression may be either an affine expression
1307 * or a condition. The first type is pushed onto the stream
1308 * as an isl_pw_aff, while the second is pushed as an isl_map.
1310 * If the initial token indicates the start of a condition,
1311 * we parse it as such.
1312 * Otherwise, we first parse an affine expression and push
1313 * that onto the stream. If the affine expression covers the
1314 * entire expression between parentheses, we return.
1315 * Otherwise, we assume that the affine expression is the
1316 * start of a condition and continue parsing.
1318 static int resolve_paren_expr(struct isl_stream *s,
1319 struct vars *v, __isl_take isl_map *map, int rational)
1321 struct isl_token *tok, *tok2;
1322 int line, col;
1323 isl_pw_aff *pwaff;
1325 tok = isl_stream_next_token(s);
1326 if (!tok || tok->type != '(')
1327 goto error;
1329 if (isl_stream_next_token_is(s, '('))
1330 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1331 goto error;
1333 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1334 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1335 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1336 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1337 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1338 map = read_disjuncts(s, v, map, rational);
1339 if (isl_stream_eat(s, ')'))
1340 goto error;
1341 tok->type = ISL_TOKEN_MAP;
1342 tok->u.map = map;
1343 isl_stream_push_token(s, tok);
1344 return 0;
1347 tok2 = isl_stream_next_token(s);
1348 if (!tok2)
1349 goto error;
1350 line = tok2->line;
1351 col = tok2->col;
1352 isl_stream_push_token(s, tok2);
1354 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1355 if (!pwaff)
1356 goto error;
1358 tok2 = isl_token_new(s->ctx, line, col, 0);
1359 if (!tok2)
1360 goto error2;
1361 tok2->type = ISL_TOKEN_AFF;
1362 tok2->u.pwaff = pwaff;
1364 if (isl_stream_eat_if_available(s, ')')) {
1365 isl_stream_push_token(s, tok2);
1366 isl_token_free(tok);
1367 isl_map_free(map);
1368 return 0;
1371 isl_stream_push_token(s, tok2);
1373 map = read_disjuncts(s, v, map, rational);
1374 if (isl_stream_eat(s, ')'))
1375 goto error;
1377 tok->type = ISL_TOKEN_MAP;
1378 tok->u.map = map;
1379 isl_stream_push_token(s, tok);
1381 return 0;
1382 error2:
1383 isl_pw_aff_free(pwaff);
1384 error:
1385 isl_token_free(tok);
1386 isl_map_free(map);
1387 return -1;
1390 static __isl_give isl_map *read_conjunct(struct isl_stream *s,
1391 struct vars *v, __isl_take isl_map *map, int rational)
1393 if (isl_stream_next_token_is(s, '('))
1394 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1395 goto error;
1397 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1398 struct isl_token *tok;
1399 tok = isl_stream_next_token(s);
1400 if (!tok)
1401 goto error;
1402 isl_map_free(map);
1403 map = isl_map_copy(tok->u.map);
1404 isl_token_free(tok);
1405 return map;
1408 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1409 return read_exists(s, v, map, rational);
1411 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1412 return map;
1414 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1415 isl_space *dim = isl_map_get_space(map);
1416 isl_map_free(map);
1417 return isl_map_empty(dim);
1420 return add_constraint(s, v, map, rational);
1421 error:
1422 isl_map_free(map);
1423 return NULL;
1426 static __isl_give isl_map *read_conjuncts(struct isl_stream *s,
1427 struct vars *v, __isl_take isl_map *map, int rational)
1429 isl_map *res;
1430 int negate;
1432 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1433 res = read_conjunct(s, v, isl_map_copy(map), rational);
1434 if (negate)
1435 res = isl_map_subtract(isl_map_copy(map), res);
1437 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1438 isl_map *res_i;
1440 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1441 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1442 if (negate)
1443 res = isl_map_subtract(res, res_i);
1444 else
1445 res = isl_map_intersect(res, res_i);
1448 isl_map_free(map);
1449 return res;
1452 static struct isl_map *read_disjuncts(struct isl_stream *s,
1453 struct vars *v, __isl_take isl_map *map, int rational)
1455 isl_map *res;
1457 if (isl_stream_next_token_is(s, '}')) {
1458 isl_space *dim = isl_map_get_space(map);
1459 isl_map_free(map);
1460 return isl_map_universe(dim);
1463 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1464 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1465 isl_map *res_i;
1467 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1468 res = isl_map_union(res, res_i);
1471 isl_map_free(map);
1472 return res;
1475 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1477 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1478 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1479 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1480 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1482 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1483 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1484 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1486 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1487 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1488 isl_basic_map_dim(bmap, isl_dim_in) +
1489 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1490 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1492 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1493 return 1 + pos;
1495 return 0;
1498 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1499 struct isl_stream *s, __isl_take isl_basic_map *bmap)
1501 int j;
1502 struct isl_token *tok;
1503 int type;
1504 int k;
1505 isl_int *c;
1506 unsigned nparam;
1507 unsigned dim;
1509 if (!bmap)
1510 return NULL;
1512 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1513 dim = isl_basic_map_dim(bmap, isl_dim_out);
1515 tok = isl_stream_next_token(s);
1516 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1517 isl_stream_error(s, tok, "expecting coefficient");
1518 if (tok)
1519 isl_stream_push_token(s, tok);
1520 goto error;
1522 if (!tok->on_new_line) {
1523 isl_stream_error(s, tok, "coefficient should appear on new line");
1524 isl_stream_push_token(s, tok);
1525 goto error;
1528 type = isl_int_get_si(tok->u.v);
1529 isl_token_free(tok);
1531 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1532 if (type == 0) {
1533 k = isl_basic_map_alloc_equality(bmap);
1534 c = bmap->eq[k];
1535 } else {
1536 k = isl_basic_map_alloc_inequality(bmap);
1537 c = bmap->ineq[k];
1539 if (k < 0)
1540 goto error;
1542 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1543 int pos;
1544 tok = isl_stream_next_token(s);
1545 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1546 isl_stream_error(s, tok, "expecting coefficient");
1547 if (tok)
1548 isl_stream_push_token(s, tok);
1549 goto error;
1551 if (tok->on_new_line) {
1552 isl_stream_error(s, tok,
1553 "coefficient should not appear on new line");
1554 isl_stream_push_token(s, tok);
1555 goto error;
1557 pos = polylib_pos_to_isl_pos(bmap, j);
1558 isl_int_set(c[pos], tok->u.v);
1559 isl_token_free(tok);
1562 return bmap;
1563 error:
1564 isl_basic_map_free(bmap);
1565 return NULL;
1568 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s)
1570 int i;
1571 struct isl_token *tok;
1572 struct isl_token *tok2;
1573 int n_row, n_col;
1574 int on_new_line;
1575 unsigned in = 0, out, local = 0;
1576 struct isl_basic_map *bmap = NULL;
1577 int nparam = 0;
1579 tok = isl_stream_next_token(s);
1580 if (!tok) {
1581 isl_stream_error(s, NULL, "unexpected EOF");
1582 return NULL;
1584 tok2 = isl_stream_next_token(s);
1585 if (!tok2) {
1586 isl_token_free(tok);
1587 isl_stream_error(s, NULL, "unexpected EOF");
1588 return NULL;
1590 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1591 isl_stream_push_token(s, tok2);
1592 isl_stream_push_token(s, tok);
1593 isl_stream_error(s, NULL,
1594 "expecting constraint matrix dimensions");
1595 return NULL;
1597 n_row = isl_int_get_si(tok->u.v);
1598 n_col = isl_int_get_si(tok2->u.v);
1599 on_new_line = tok2->on_new_line;
1600 isl_token_free(tok2);
1601 isl_token_free(tok);
1602 isl_assert(s->ctx, !on_new_line, return NULL);
1603 isl_assert(s->ctx, n_row >= 0, return NULL);
1604 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1605 tok = isl_stream_next_token_on_same_line(s);
1606 if (tok) {
1607 if (tok->type != ISL_TOKEN_VALUE) {
1608 isl_stream_error(s, tok,
1609 "expecting number of output dimensions");
1610 isl_stream_push_token(s, tok);
1611 goto error;
1613 out = isl_int_get_si(tok->u.v);
1614 isl_token_free(tok);
1616 tok = isl_stream_next_token_on_same_line(s);
1617 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1618 isl_stream_error(s, tok,
1619 "expecting number of input dimensions");
1620 if (tok)
1621 isl_stream_push_token(s, tok);
1622 goto error;
1624 in = isl_int_get_si(tok->u.v);
1625 isl_token_free(tok);
1627 tok = isl_stream_next_token_on_same_line(s);
1628 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1629 isl_stream_error(s, tok,
1630 "expecting number of existentials");
1631 if (tok)
1632 isl_stream_push_token(s, tok);
1633 goto error;
1635 local = isl_int_get_si(tok->u.v);
1636 isl_token_free(tok);
1638 tok = isl_stream_next_token_on_same_line(s);
1639 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1640 isl_stream_error(s, tok,
1641 "expecting number of parameters");
1642 if (tok)
1643 isl_stream_push_token(s, tok);
1644 goto error;
1646 nparam = isl_int_get_si(tok->u.v);
1647 isl_token_free(tok);
1648 if (n_col != 1 + out + in + local + nparam + 1) {
1649 isl_stream_error(s, NULL,
1650 "dimensions don't match");
1651 goto error;
1653 } else
1654 out = n_col - 2 - nparam;
1655 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
1656 if (!bmap)
1657 return NULL;
1659 for (i = 0; i < local; ++i) {
1660 int k = isl_basic_map_alloc_div(bmap);
1661 if (k < 0)
1662 goto error;
1663 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
1666 for (i = 0; i < n_row; ++i)
1667 bmap = basic_map_read_polylib_constraint(s, bmap);
1669 tok = isl_stream_next_token_on_same_line(s);
1670 if (tok) {
1671 isl_stream_error(s, tok, "unexpected extra token on line");
1672 isl_stream_push_token(s, tok);
1673 goto error;
1676 bmap = isl_basic_map_simplify(bmap);
1677 bmap = isl_basic_map_finalize(bmap);
1678 return bmap;
1679 error:
1680 isl_basic_map_free(bmap);
1681 return NULL;
1684 static struct isl_map *map_read_polylib(struct isl_stream *s)
1686 struct isl_token *tok;
1687 struct isl_token *tok2;
1688 int i, n;
1689 struct isl_map *map;
1691 tok = isl_stream_next_token(s);
1692 if (!tok) {
1693 isl_stream_error(s, NULL, "unexpected EOF");
1694 return NULL;
1696 tok2 = isl_stream_next_token_on_same_line(s);
1697 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
1698 isl_stream_push_token(s, tok2);
1699 isl_stream_push_token(s, tok);
1700 return isl_map_from_basic_map(basic_map_read_polylib(s));
1702 if (tok2) {
1703 isl_stream_error(s, tok2, "unexpected token");
1704 isl_stream_push_token(s, tok2);
1705 isl_stream_push_token(s, tok);
1706 return NULL;
1708 n = isl_int_get_si(tok->u.v);
1709 isl_token_free(tok);
1711 isl_assert(s->ctx, n >= 1, return NULL);
1713 map = isl_map_from_basic_map(basic_map_read_polylib(s));
1715 for (i = 1; map && i < n; ++i)
1716 map = isl_map_union(map,
1717 isl_map_from_basic_map(basic_map_read_polylib(s)));
1719 return map;
1722 static int optional_power(struct isl_stream *s)
1724 int pow;
1725 struct isl_token *tok;
1727 tok = isl_stream_next_token(s);
1728 if (!tok)
1729 return 1;
1730 if (tok->type != '^') {
1731 isl_stream_push_token(s, tok);
1732 return 1;
1734 isl_token_free(tok);
1735 tok = isl_stream_next_token(s);
1736 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1737 isl_stream_error(s, tok, "expecting exponent");
1738 if (tok)
1739 isl_stream_push_token(s, tok);
1740 return 1;
1742 pow = isl_int_get_si(tok->u.v);
1743 isl_token_free(tok);
1744 return pow;
1747 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1748 __isl_keep isl_map *map, struct vars *v);
1750 static __isl_give isl_pw_qpolynomial *read_factor(struct isl_stream *s,
1751 __isl_keep isl_map *map, struct vars *v)
1753 isl_pw_qpolynomial *pwqp;
1754 struct isl_token *tok;
1756 tok = next_token(s);
1757 if (!tok) {
1758 isl_stream_error(s, NULL, "unexpected EOF");
1759 return NULL;
1761 if (tok->type == '(') {
1762 int pow;
1764 isl_token_free(tok);
1765 pwqp = read_term(s, map, v);
1766 if (!pwqp)
1767 return NULL;
1768 if (isl_stream_eat(s, ')'))
1769 goto error;
1770 pow = optional_power(s);
1771 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1772 } else if (tok->type == ISL_TOKEN_VALUE) {
1773 struct isl_token *tok2;
1774 isl_qpolynomial *qp;
1776 tok2 = isl_stream_next_token(s);
1777 if (tok2 && tok2->type == '/') {
1778 isl_token_free(tok2);
1779 tok2 = next_token(s);
1780 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
1781 isl_stream_error(s, tok2, "expected denominator");
1782 isl_token_free(tok);
1783 isl_token_free(tok2);
1784 return NULL;
1786 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
1787 tok->u.v, tok2->u.v);
1788 isl_token_free(tok2);
1789 } else {
1790 isl_stream_push_token(s, tok2);
1791 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
1792 tok->u.v);
1794 isl_token_free(tok);
1795 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1796 } else if (tok->type == ISL_TOKEN_INFTY) {
1797 isl_qpolynomial *qp;
1798 isl_token_free(tok);
1799 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
1800 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1801 } else if (tok->type == ISL_TOKEN_NAN) {
1802 isl_qpolynomial *qp;
1803 isl_token_free(tok);
1804 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
1805 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1806 } else if (tok->type == ISL_TOKEN_IDENT) {
1807 int n = v->n;
1808 int pos = vars_pos(v, tok->u.s, -1);
1809 int pow;
1810 isl_qpolynomial *qp;
1811 if (pos < 0) {
1812 isl_token_free(tok);
1813 return NULL;
1815 if (pos >= n) {
1816 vars_drop(v, v->n - n);
1817 isl_stream_error(s, tok, "unknown identifier");
1818 isl_token_free(tok);
1819 return NULL;
1821 isl_token_free(tok);
1822 pow = optional_power(s);
1823 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
1824 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
1825 } else if (tok->type == '[') {
1826 isl_pw_aff *pwaff;
1827 int pow;
1829 isl_stream_push_token(s, tok);
1830 pwaff = accept_div(s, isl_map_get_space(map), v);
1831 pow = optional_power(s);
1832 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
1833 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
1834 } else if (tok->type == '-') {
1835 isl_token_free(tok);
1836 pwqp = read_factor(s, map, v);
1837 pwqp = isl_pw_qpolynomial_neg(pwqp);
1838 } else {
1839 isl_stream_error(s, tok, "unexpected isl_token");
1840 isl_stream_push_token(s, tok);
1841 return NULL;
1844 if (isl_stream_eat_if_available(s, '*') ||
1845 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
1846 isl_pw_qpolynomial *pwqp2;
1848 pwqp2 = read_factor(s, map, v);
1849 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
1852 return pwqp;
1853 error:
1854 isl_pw_qpolynomial_free(pwqp);
1855 return NULL;
1858 static __isl_give isl_pw_qpolynomial *read_term(struct isl_stream *s,
1859 __isl_keep isl_map *map, struct vars *v)
1861 struct isl_token *tok;
1862 isl_pw_qpolynomial *pwqp;
1864 pwqp = read_factor(s, map, v);
1866 for (;;) {
1867 tok = next_token(s);
1868 if (!tok)
1869 return pwqp;
1871 if (tok->type == '+') {
1872 isl_pw_qpolynomial *pwqp2;
1874 isl_token_free(tok);
1875 pwqp2 = read_factor(s, map, v);
1876 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1877 } else if (tok->type == '-') {
1878 isl_pw_qpolynomial *pwqp2;
1880 isl_token_free(tok);
1881 pwqp2 = read_factor(s, map, v);
1882 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
1883 } else if (tok->type == ISL_TOKEN_VALUE &&
1884 isl_int_is_neg(tok->u.v)) {
1885 isl_pw_qpolynomial *pwqp2;
1887 isl_stream_push_token(s, tok);
1888 pwqp2 = read_factor(s, map, v);
1889 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
1890 } else {
1891 isl_stream_push_token(s, tok);
1892 break;
1896 return pwqp;
1899 static __isl_give isl_map *read_optional_disjuncts(struct isl_stream *s,
1900 __isl_take isl_map *map, struct vars *v, int rational)
1902 struct isl_token *tok;
1904 tok = isl_stream_next_token(s);
1905 if (!tok) {
1906 isl_stream_error(s, NULL, "unexpected EOF");
1907 goto error;
1909 if (tok->type == ':' ||
1910 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
1911 isl_token_free(tok);
1912 map = read_disjuncts(s, v, map, rational);
1913 } else
1914 isl_stream_push_token(s, tok);
1916 return map;
1917 error:
1918 isl_map_free(map);
1919 return NULL;
1922 static struct isl_obj obj_read_poly(struct isl_stream *s,
1923 __isl_take isl_map *map, struct vars *v, int n)
1925 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
1926 isl_pw_qpolynomial *pwqp;
1927 struct isl_set *set;
1929 pwqp = read_term(s, map, v);
1930 map = read_optional_disjuncts(s, map, v, 0);
1931 set = isl_map_range(map);
1933 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
1935 vars_drop(v, v->n - n);
1937 obj.v = pwqp;
1938 return obj;
1941 static struct isl_obj obj_read_poly_or_fold(struct isl_stream *s,
1942 __isl_take isl_set *set, struct vars *v, int n)
1944 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
1945 isl_pw_qpolynomial *pwqp;
1946 isl_pw_qpolynomial_fold *pwf = NULL;
1948 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
1949 return obj_read_poly(s, set, v, n);
1951 if (isl_stream_eat(s, '('))
1952 goto error;
1954 pwqp = read_term(s, set, v);
1955 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
1957 while (isl_stream_eat_if_available(s, ',')) {
1958 isl_pw_qpolynomial_fold *pwf_i;
1959 pwqp = read_term(s, set, v);
1960 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
1961 pwqp);
1962 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
1965 if (isl_stream_eat(s, ')'))
1966 goto error;
1968 set = read_optional_disjuncts(s, set, v, 0);
1969 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
1971 vars_drop(v, v->n - n);
1973 obj.v = pwf;
1974 return obj;
1975 error:
1976 isl_set_free(set);
1977 isl_pw_qpolynomial_fold_free(pwf);
1978 obj.type = isl_obj_none;
1979 return obj;
1982 static int is_rational(struct isl_stream *s)
1984 struct isl_token *tok;
1986 tok = isl_stream_next_token(s);
1987 if (!tok)
1988 return 0;
1989 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
1990 isl_token_free(tok);
1991 isl_stream_eat(s, ':');
1992 return 1;
1995 isl_stream_push_token(s, tok);
1997 return 0;
2000 static struct isl_obj obj_read_body(struct isl_stream *s,
2001 __isl_take isl_map *map, struct vars *v)
2003 struct isl_token *tok;
2004 struct isl_obj obj = { isl_obj_set, NULL };
2005 int n = v->n;
2006 int rational;
2008 rational = is_rational(s);
2009 if (rational)
2010 map = isl_map_set_rational(map);
2012 if (isl_stream_next_token_is(s, ':')) {
2013 obj.type = isl_obj_set;
2014 obj.v = read_optional_disjuncts(s, map, v, rational);
2015 return obj;
2018 if (!next_is_tuple(s))
2019 return obj_read_poly_or_fold(s, map, v, n);
2021 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2022 if (!map)
2023 goto error;
2024 tok = isl_stream_next_token(s);
2025 if (!tok)
2026 goto error;
2027 if (tok->type == ISL_TOKEN_TO) {
2028 obj.type = isl_obj_map;
2029 isl_token_free(tok);
2030 if (!next_is_tuple(s)) {
2031 isl_set *set = isl_map_domain(map);
2032 return obj_read_poly_or_fold(s, set, v, n);
2034 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2035 if (!map)
2036 goto error;
2037 } else {
2038 map = isl_map_domain(map);
2039 isl_stream_push_token(s, tok);
2042 map = read_optional_disjuncts(s, map, v, rational);
2044 vars_drop(v, v->n - n);
2046 obj.v = map;
2047 return obj;
2048 error:
2049 isl_map_free(map);
2050 obj.type = isl_obj_none;
2051 return obj;
2054 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2056 if (obj.type == isl_obj_map) {
2057 obj.v = isl_union_map_from_map(obj.v);
2058 obj.type = isl_obj_union_map;
2059 } else if (obj.type == isl_obj_set) {
2060 obj.v = isl_union_set_from_set(obj.v);
2061 obj.type = isl_obj_union_set;
2062 } else if (obj.type == isl_obj_pw_qpolynomial) {
2063 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2064 obj.type = isl_obj_union_pw_qpolynomial;
2065 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2066 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2067 obj.type = isl_obj_union_pw_qpolynomial_fold;
2068 } else
2069 isl_assert(ctx, 0, goto error);
2070 return obj;
2071 error:
2072 obj.type->free(obj.v);
2073 obj.type = isl_obj_none;
2074 return obj;
2077 static struct isl_obj obj_add(struct isl_ctx *ctx,
2078 struct isl_obj obj1, struct isl_obj obj2)
2080 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2081 obj1 = to_union(ctx, obj1);
2082 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2083 obj2 = to_union(ctx, obj2);
2084 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2085 obj1 = to_union(ctx, obj1);
2086 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2087 obj2 = to_union(ctx, obj2);
2088 if (obj1.type == isl_obj_pw_qpolynomial &&
2089 obj2.type == isl_obj_union_pw_qpolynomial)
2090 obj1 = to_union(ctx, obj1);
2091 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2092 obj2.type == isl_obj_pw_qpolynomial)
2093 obj2 = to_union(ctx, obj2);
2094 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2095 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2096 obj1 = to_union(ctx, obj1);
2097 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2098 obj2.type == isl_obj_pw_qpolynomial_fold)
2099 obj2 = to_union(ctx, obj2);
2100 isl_assert(ctx, obj1.type == obj2.type, goto error);
2101 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2102 obj1 = to_union(ctx, obj1);
2103 obj2 = to_union(ctx, obj2);
2105 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2106 obj1 = to_union(ctx, obj1);
2107 obj2 = to_union(ctx, obj2);
2109 if (obj1.type == isl_obj_pw_qpolynomial &&
2110 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2111 obj1 = to_union(ctx, obj1);
2112 obj2 = to_union(ctx, obj2);
2114 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2115 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2116 obj1 = to_union(ctx, obj1);
2117 obj2 = to_union(ctx, obj2);
2119 obj1.v = obj1.type->add(obj1.v, obj2.v);
2120 return obj1;
2121 error:
2122 obj1.type->free(obj1.v);
2123 obj2.type->free(obj2.v);
2124 obj1.type = isl_obj_none;
2125 obj1.v = NULL;
2126 return obj1;
2129 static struct isl_obj obj_read(struct isl_stream *s)
2131 isl_map *map = NULL;
2132 struct isl_token *tok;
2133 struct vars *v = NULL;
2134 struct isl_obj obj = { isl_obj_set, NULL };
2136 tok = next_token(s);
2137 if (!tok) {
2138 isl_stream_error(s, NULL, "unexpected EOF");
2139 goto error;
2141 if (tok->type == ISL_TOKEN_VALUE) {
2142 struct isl_token *tok2;
2143 struct isl_map *map;
2145 tok2 = isl_stream_next_token(s);
2146 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2147 isl_int_is_neg(tok2->u.v)) {
2148 if (tok2)
2149 isl_stream_push_token(s, tok2);
2150 obj.type = isl_obj_int;
2151 obj.v = isl_int_obj_alloc(s->ctx, tok->u.v);
2152 isl_token_free(tok);
2153 return obj;
2155 isl_stream_push_token(s, tok2);
2156 isl_stream_push_token(s, tok);
2157 map = map_read_polylib(s);
2158 if (!map)
2159 goto error;
2160 if (isl_map_may_be_set(map))
2161 obj.v = isl_map_range(map);
2162 else {
2163 obj.type = isl_obj_map;
2164 obj.v = map;
2166 return obj;
2168 v = vars_new(s->ctx);
2169 if (!v) {
2170 isl_stream_push_token(s, tok);
2171 goto error;
2173 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2174 if (tok->type == '[') {
2175 isl_stream_push_token(s, tok);
2176 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2177 if (!map)
2178 goto error;
2179 tok = isl_stream_next_token(s);
2180 if (!tok || tok->type != ISL_TOKEN_TO) {
2181 isl_stream_error(s, tok, "expecting '->'");
2182 if (tok)
2183 isl_stream_push_token(s, tok);
2184 goto error;
2186 isl_token_free(tok);
2187 tok = isl_stream_next_token(s);
2189 if (!tok || tok->type != '{') {
2190 isl_stream_error(s, tok, "expecting '{'");
2191 if (tok)
2192 isl_stream_push_token(s, tok);
2193 goto error;
2195 isl_token_free(tok);
2197 tok = isl_stream_next_token(s);
2198 if (!tok)
2200 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2201 isl_token_free(tok);
2202 if (isl_stream_eat(s, '='))
2203 goto error;
2204 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2205 if (!map)
2206 goto error;
2207 } else if (tok->type == '}') {
2208 obj.type = isl_obj_union_set;
2209 obj.v = isl_union_set_empty(isl_map_get_space(map));
2210 isl_token_free(tok);
2211 goto done;
2212 } else
2213 isl_stream_push_token(s, tok);
2215 for (;;) {
2216 struct isl_obj o;
2217 tok = NULL;
2218 o = obj_read_body(s, isl_map_copy(map), v);
2219 if (o.type == isl_obj_none || !o.v)
2220 goto error;
2221 if (!obj.v)
2222 obj = o;
2223 else {
2224 obj = obj_add(s->ctx, obj, o);
2225 if (obj.type == isl_obj_none || !obj.v)
2226 goto error;
2228 tok = isl_stream_next_token(s);
2229 if (!tok || tok->type != ';')
2230 break;
2231 isl_token_free(tok);
2232 if (isl_stream_next_token_is(s, '}')) {
2233 tok = isl_stream_next_token(s);
2234 break;
2238 if (tok && tok->type == '}') {
2239 isl_token_free(tok);
2240 } else {
2241 isl_stream_error(s, tok, "unexpected isl_token");
2242 if (tok)
2243 isl_token_free(tok);
2244 goto error;
2246 done:
2247 vars_free(v);
2248 isl_map_free(map);
2250 return obj;
2251 error:
2252 isl_map_free(map);
2253 obj.type->free(obj.v);
2254 if (v)
2255 vars_free(v);
2256 obj.v = NULL;
2257 return obj;
2260 struct isl_obj isl_stream_read_obj(struct isl_stream *s)
2262 return obj_read(s);
2265 __isl_give isl_map *isl_stream_read_map(struct isl_stream *s)
2267 struct isl_obj obj;
2269 obj = obj_read(s);
2270 if (obj.v)
2271 isl_assert(s->ctx, obj.type == isl_obj_map ||
2272 obj.type == isl_obj_set, goto error);
2274 if (obj.type == isl_obj_set)
2275 obj.v = isl_map_from_range(obj.v);
2277 return obj.v;
2278 error:
2279 obj.type->free(obj.v);
2280 return NULL;
2283 __isl_give isl_set *isl_stream_read_set(struct isl_stream *s)
2285 struct isl_obj obj;
2287 obj = obj_read(s);
2288 if (obj.v) {
2289 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2290 obj.v = isl_map_range(obj.v);
2291 obj.type = isl_obj_set;
2293 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2296 return obj.v;
2297 error:
2298 obj.type->free(obj.v);
2299 return NULL;
2302 __isl_give isl_union_map *isl_stream_read_union_map(struct isl_stream *s)
2304 struct isl_obj obj;
2306 obj = obj_read(s);
2307 if (obj.type == isl_obj_map) {
2308 obj.type = isl_obj_union_map;
2309 obj.v = isl_union_map_from_map(obj.v);
2311 if (obj.type == isl_obj_set) {
2312 obj.type = isl_obj_union_set;
2313 obj.v = isl_union_set_from_set(obj.v);
2315 if (obj.v && obj.type == isl_obj_union_set &&
2316 isl_union_set_is_empty(obj.v))
2317 obj.type = isl_obj_union_map;
2318 if (obj.v && obj.type != isl_obj_union_map)
2319 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2321 return obj.v;
2322 error:
2323 obj.type->free(obj.v);
2324 return NULL;
2327 __isl_give isl_union_set *isl_stream_read_union_set(struct isl_stream *s)
2329 struct isl_obj obj;
2331 obj = obj_read(s);
2332 if (obj.type == isl_obj_set) {
2333 obj.type = isl_obj_union_set;
2334 obj.v = isl_union_set_from_set(obj.v);
2336 if (obj.v)
2337 isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2339 return obj.v;
2340 error:
2341 obj.type->free(obj.v);
2342 return NULL;
2345 static __isl_give isl_basic_map *basic_map_read(struct isl_stream *s)
2347 struct isl_obj obj;
2348 struct isl_map *map;
2349 struct isl_basic_map *bmap;
2351 obj = obj_read(s);
2352 map = obj.v;
2353 if (!map)
2354 return NULL;
2356 isl_assert(map->ctx, map->n <= 1, goto error);
2358 if (map->n == 0)
2359 bmap = isl_basic_map_empty_like_map(map);
2360 else
2361 bmap = isl_basic_map_copy(map->p[0]);
2363 isl_map_free(map);
2365 return bmap;
2366 error:
2367 isl_map_free(map);
2368 return NULL;
2371 static __isl_give isl_basic_set *basic_set_read(struct isl_stream *s)
2373 isl_basic_map *bmap;
2374 bmap = basic_map_read(s);
2375 if (!bmap)
2376 return NULL;
2377 if (!isl_basic_map_may_be_set(bmap))
2378 isl_die(s->ctx, isl_error_invalid,
2379 "input is not a set", goto error);
2380 return isl_basic_map_range(bmap);
2381 error:
2382 isl_basic_map_free(bmap);
2383 return NULL;
2386 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2387 FILE *input)
2389 struct isl_basic_map *bmap;
2390 struct isl_stream *s = isl_stream_new_file(ctx, input);
2391 if (!s)
2392 return NULL;
2393 bmap = basic_map_read(s);
2394 isl_stream_free(s);
2395 return bmap;
2398 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2399 FILE *input)
2401 isl_basic_set *bset;
2402 struct isl_stream *s = isl_stream_new_file(ctx, input);
2403 if (!s)
2404 return NULL;
2405 bset = basic_set_read(s);
2406 isl_stream_free(s);
2407 return bset;
2410 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2411 const char *str)
2413 struct isl_basic_map *bmap;
2414 struct isl_stream *s = isl_stream_new_str(ctx, str);
2415 if (!s)
2416 return NULL;
2417 bmap = basic_map_read(s);
2418 isl_stream_free(s);
2419 return bmap;
2422 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2423 const char *str)
2425 isl_basic_set *bset;
2426 struct isl_stream *s = isl_stream_new_str(ctx, str);
2427 if (!s)
2428 return NULL;
2429 bset = basic_set_read(s);
2430 isl_stream_free(s);
2431 return bset;
2434 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2435 FILE *input)
2437 struct isl_map *map;
2438 struct isl_stream *s = isl_stream_new_file(ctx, input);
2439 if (!s)
2440 return NULL;
2441 map = isl_stream_read_map(s);
2442 isl_stream_free(s);
2443 return map;
2446 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2447 const char *str)
2449 struct isl_map *map;
2450 struct isl_stream *s = isl_stream_new_str(ctx, str);
2451 if (!s)
2452 return NULL;
2453 map = isl_stream_read_map(s);
2454 isl_stream_free(s);
2455 return map;
2458 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2459 FILE *input)
2461 isl_set *set;
2462 struct isl_stream *s = isl_stream_new_file(ctx, input);
2463 if (!s)
2464 return NULL;
2465 set = isl_stream_read_set(s);
2466 isl_stream_free(s);
2467 return set;
2470 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2471 const char *str)
2473 isl_set *set;
2474 struct isl_stream *s = isl_stream_new_str(ctx, str);
2475 if (!s)
2476 return NULL;
2477 set = isl_stream_read_set(s);
2478 isl_stream_free(s);
2479 return set;
2482 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2483 FILE *input)
2485 isl_union_map *umap;
2486 struct isl_stream *s = isl_stream_new_file(ctx, input);
2487 if (!s)
2488 return NULL;
2489 umap = isl_stream_read_union_map(s);
2490 isl_stream_free(s);
2491 return umap;
2494 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2495 const char *str)
2497 isl_union_map *umap;
2498 struct isl_stream *s = isl_stream_new_str(ctx, str);
2499 if (!s)
2500 return NULL;
2501 umap = isl_stream_read_union_map(s);
2502 isl_stream_free(s);
2503 return umap;
2506 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2507 FILE *input)
2509 isl_union_set *uset;
2510 struct isl_stream *s = isl_stream_new_file(ctx, input);
2511 if (!s)
2512 return NULL;
2513 uset = isl_stream_read_union_set(s);
2514 isl_stream_free(s);
2515 return uset;
2518 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2519 const char *str)
2521 isl_union_set *uset;
2522 struct isl_stream *s = isl_stream_new_str(ctx, str);
2523 if (!s)
2524 return NULL;
2525 uset = isl_stream_read_union_set(s);
2526 isl_stream_free(s);
2527 return uset;
2530 static __isl_give isl_vec *isl_vec_read_polylib(struct isl_stream *s)
2532 struct isl_vec *vec = NULL;
2533 struct isl_token *tok;
2534 unsigned size;
2535 int j;
2537 tok = isl_stream_next_token(s);
2538 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2539 isl_stream_error(s, tok, "expecting vector length");
2540 goto error;
2543 size = isl_int_get_si(tok->u.v);
2544 isl_token_free(tok);
2546 vec = isl_vec_alloc(s->ctx, size);
2548 for (j = 0; j < size; ++j) {
2549 tok = isl_stream_next_token(s);
2550 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2551 isl_stream_error(s, tok, "expecting constant value");
2552 goto error;
2554 isl_int_set(vec->el[j], tok->u.v);
2555 isl_token_free(tok);
2558 return vec;
2559 error:
2560 isl_token_free(tok);
2561 isl_vec_free(vec);
2562 return NULL;
2565 static __isl_give isl_vec *vec_read(struct isl_stream *s)
2567 return isl_vec_read_polylib(s);
2570 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
2572 isl_vec *v;
2573 struct isl_stream *s = isl_stream_new_file(ctx, input);
2574 if (!s)
2575 return NULL;
2576 v = vec_read(s);
2577 isl_stream_free(s);
2578 return v;
2581 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
2582 struct isl_stream *s)
2584 struct isl_obj obj;
2586 obj = obj_read(s);
2587 if (obj.v)
2588 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
2589 goto error);
2591 return obj.v;
2592 error:
2593 obj.type->free(obj.v);
2594 return NULL;
2597 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
2598 const char *str)
2600 isl_pw_qpolynomial *pwqp;
2601 struct isl_stream *s = isl_stream_new_str(ctx, str);
2602 if (!s)
2603 return NULL;
2604 pwqp = isl_stream_read_pw_qpolynomial(s);
2605 isl_stream_free(s);
2606 return pwqp;
2609 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
2610 FILE *input)
2612 isl_pw_qpolynomial *pwqp;
2613 struct isl_stream *s = isl_stream_new_file(ctx, input);
2614 if (!s)
2615 return NULL;
2616 pwqp = isl_stream_read_pw_qpolynomial(s);
2617 isl_stream_free(s);
2618 return pwqp;
2621 /* Is the next token an identifer not in "v"?
2623 static int next_is_fresh_ident(struct isl_stream *s, struct vars *v)
2625 int n = v->n;
2626 int fresh;
2627 struct isl_token *tok;
2629 tok = isl_stream_next_token(s);
2630 if (!tok)
2631 return 0;
2632 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
2633 isl_stream_push_token(s, tok);
2635 vars_drop(v, v->n - n);
2637 return fresh;
2640 /* First read the domain of the affine expression, which may be
2641 * a parameter space or a set.
2642 * The tricky part is that we don't know if the domain is a set or not,
2643 * so when we are trying to read the domain, we may actually be reading
2644 * the affine expression itself (defined on a parameter domains)
2645 * If the tuple we are reading is named, we assume it's the domain.
2646 * Also, if inside the tuple, the first thing we find is a nested tuple
2647 * or a new identifier, we again assume it's the domain.
2648 * Otherwise, we assume we are reading an affine expression.
2650 static __isl_give isl_set *read_aff_domain(struct isl_stream *s,
2651 __isl_take isl_set *dom, struct vars *v)
2653 struct isl_token *tok;
2655 tok = isl_stream_next_token(s);
2656 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
2657 isl_stream_push_token(s, tok);
2658 return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2660 if (!tok || tok->type != '[') {
2661 isl_stream_error(s, tok, "expecting '['");
2662 goto error;
2664 if (next_is_tuple(s) || next_is_fresh_ident(s, v)) {
2665 isl_stream_push_token(s, tok);
2666 dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
2667 } else
2668 isl_stream_push_token(s, tok);
2670 return dom;
2671 error:
2672 if (tok)
2673 isl_stream_push_token(s, tok);
2674 isl_set_free(dom);
2675 return NULL;
2678 /* Read an affine expression from "s".
2680 __isl_give isl_aff *isl_stream_read_aff(struct isl_stream *s)
2682 isl_aff *aff;
2683 isl_multi_aff *ma;
2685 ma = isl_stream_read_multi_aff(s);
2686 if (!ma)
2687 return NULL;
2688 if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
2689 isl_die(s->ctx, isl_error_invalid,
2690 "expecting single affine expression",
2691 goto error);
2693 aff = isl_multi_aff_get_aff(ma, 0);
2694 isl_multi_aff_free(ma);
2695 return aff;
2696 error:
2697 isl_multi_aff_free(ma);
2698 return NULL;
2701 /* Read a piecewise affine expression from "s" with domain (space) "dom".
2703 static __isl_give isl_pw_aff *read_pw_aff_with_dom(struct isl_stream *s,
2704 __isl_take isl_set *dom, struct vars *v)
2706 isl_pw_aff *pwaff = NULL;
2708 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
2709 goto error;
2711 if (isl_stream_eat(s, '['))
2712 goto error;
2714 pwaff = accept_affine(s, isl_set_get_space(dom), v);
2716 if (isl_stream_eat(s, ']'))
2717 goto error;
2719 dom = read_optional_disjuncts(s, dom, v, 0);
2720 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
2722 return pwaff;
2723 error:
2724 isl_set_free(dom);
2725 isl_pw_aff_free(pwaff);
2726 return NULL;
2729 __isl_give isl_pw_aff *isl_stream_read_pw_aff(struct isl_stream *s)
2731 struct vars *v;
2732 isl_set *dom = NULL;
2733 isl_set *aff_dom;
2734 isl_pw_aff *pa = NULL;
2735 int n;
2737 v = vars_new(s->ctx);
2738 if (!v)
2739 return NULL;
2741 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2742 if (next_is_tuple(s)) {
2743 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2744 if (isl_stream_eat(s, ISL_TOKEN_TO))
2745 goto error;
2747 if (isl_stream_eat(s, '{'))
2748 goto error;
2750 n = v->n;
2751 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2752 pa = read_pw_aff_with_dom(s, aff_dom, v);
2753 vars_drop(v, v->n - n);
2755 while (isl_stream_eat_if_available(s, ';')) {
2756 isl_pw_aff *pa_i;
2758 n = v->n;
2759 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
2760 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
2761 vars_drop(v, v->n - n);
2763 pa = isl_pw_aff_union_add(pa, pa_i);
2766 if (isl_stream_eat(s, '}'))
2767 goto error;
2769 vars_free(v);
2770 isl_set_free(dom);
2771 return pa;
2772 error:
2773 vars_free(v);
2774 isl_set_free(dom);
2775 isl_pw_aff_free(pa);
2776 return NULL;
2779 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
2781 isl_aff *aff;
2782 struct isl_stream *s = isl_stream_new_str(ctx, str);
2783 if (!s)
2784 return NULL;
2785 aff = isl_stream_read_aff(s);
2786 isl_stream_free(s);
2787 return aff;
2790 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
2792 isl_pw_aff *pa;
2793 struct isl_stream *s = isl_stream_new_str(ctx, str);
2794 if (!s)
2795 return NULL;
2796 pa = isl_stream_read_pw_aff(s);
2797 isl_stream_free(s);
2798 return pa;
2801 /* Read an isl_pw_multi_aff from "s".
2802 * We currently read a generic object and if it turns out to be a set or
2803 * a map, we convert that to an isl_pw_multi_aff.
2804 * It would be more efficient if we were to construct the isl_pw_multi_aff
2805 * directly.
2807 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(struct isl_stream *s)
2809 struct isl_obj obj;
2811 obj = obj_read(s);
2812 if (!obj.v)
2813 return NULL;
2815 if (obj.type == isl_obj_map)
2816 return isl_pw_multi_aff_from_map(obj.v);
2817 if (obj.type == isl_obj_set)
2818 return isl_pw_multi_aff_from_set(obj.v);
2820 obj.type->free(obj.v);
2821 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2822 return NULL);
2825 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
2826 const char *str)
2828 isl_pw_multi_aff *pma;
2829 struct isl_stream *s = isl_stream_new_str(ctx, str);
2830 if (!s)
2831 return NULL;
2832 pma = isl_stream_read_pw_multi_aff(s);
2833 isl_stream_free(s);
2834 return pma;
2837 /* Read an isl_union_pw_multi_aff from "s".
2838 * We currently read a generic object and if it turns out to be a set or
2839 * a map, we convert that to an isl_union_pw_multi_aff.
2840 * It would be more efficient if we were to construct
2841 * the isl_union_pw_multi_aff directly.
2843 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
2844 struct isl_stream *s)
2846 struct isl_obj obj;
2848 obj = obj_read(s);
2849 if (!obj.v)
2850 return NULL;
2852 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
2853 obj = to_union(s->ctx, obj);
2854 if (obj.type == isl_obj_union_map)
2855 return isl_union_pw_multi_aff_from_union_map(obj.v);
2856 if (obj.type == isl_obj_union_set)
2857 return isl_union_pw_multi_aff_from_union_set(obj.v);
2859 obj.type->free(obj.v);
2860 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
2861 return NULL);
2864 /* Read an isl_union_pw_multi_aff from "str".
2866 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
2867 isl_ctx *ctx, const char *str)
2869 isl_union_pw_multi_aff *upma;
2870 struct isl_stream *s = isl_stream_new_str(ctx, str);
2871 if (!s)
2872 return NULL;
2873 upma = isl_stream_read_union_pw_multi_aff(s);
2874 isl_stream_free(s);
2875 return upma;
2878 /* Assuming "pa" represents a single affine expression defined on a universe
2879 * domain, extract this affine expression.
2881 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
2883 isl_aff *aff;
2885 if (!pa)
2886 return NULL;
2887 if (pa->n != 1)
2888 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2889 "expecting single affine expression",
2890 goto error);
2891 if (!isl_set_plain_is_universe(pa->p[0].set))
2892 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2893 "expecting universe domain",
2894 goto error);
2896 aff = isl_aff_copy(pa->p[0].aff);
2897 isl_pw_aff_free(pa);
2898 return aff;
2899 error:
2900 isl_pw_aff_free(pa);
2901 return NULL;
2904 /* Read a multi-affine expression from "s".
2905 * If the multi-affine expression has a domain, then then tuple
2906 * representing this domain cannot involve any affine expressions.
2907 * The tuple representing the actual expressions needs to consist
2908 * of only affine expressions. Moreover, these expressions can
2909 * only depend on parameters and input dimensions and not on other
2910 * output dimensions.
2912 __isl_give isl_multi_aff *isl_stream_read_multi_aff(struct isl_stream *s)
2914 struct vars *v;
2915 isl_set *dom = NULL;
2916 isl_multi_pw_aff *tuple = NULL;
2917 int dim, i, n;
2918 isl_space *space, *dom_space;
2919 isl_multi_aff *ma = NULL;
2921 v = vars_new(s->ctx);
2922 if (!v)
2923 return NULL;
2925 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
2926 if (next_is_tuple(s)) {
2927 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
2928 if (isl_stream_eat(s, ISL_TOKEN_TO))
2929 goto error;
2931 if (!isl_set_plain_is_universe(dom))
2932 isl_die(s->ctx, isl_error_invalid,
2933 "expecting universe parameter domain", goto error);
2934 if (isl_stream_eat(s, '{'))
2935 goto error;
2937 tuple = read_tuple(s, v, 0, 0);
2938 if (!tuple)
2939 goto error;
2940 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
2941 isl_set *set;
2942 isl_space *space;
2943 int has_expr;
2945 has_expr = tuple_has_expr(tuple);
2946 if (has_expr < 0)
2947 goto error;
2948 if (has_expr)
2949 isl_die(s->ctx, isl_error_invalid,
2950 "expecting universe domain", goto error);
2951 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2952 set = isl_set_universe(space);
2953 dom = isl_set_intersect_params(set, dom);
2954 isl_multi_pw_aff_free(tuple);
2955 tuple = read_tuple(s, v, 0, 0);
2956 if (!tuple)
2957 goto error;
2960 if (isl_stream_eat(s, '}'))
2961 goto error;
2963 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
2964 dim = isl_set_dim(dom, isl_dim_all);
2965 dom_space = isl_set_get_space(dom);
2966 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
2967 space = isl_space_align_params(space, isl_space_copy(dom_space));
2968 if (!isl_space_is_params(dom_space))
2969 space = isl_space_map_from_domain_and_range(
2970 isl_space_copy(dom_space), space);
2971 isl_space_free(dom_space);
2972 ma = isl_multi_aff_alloc(space);
2974 for (i = 0; i < n; ++i) {
2975 isl_pw_aff *pa;
2976 isl_aff *aff;
2977 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
2978 aff = aff_from_pw_aff(pa);
2979 if (!aff)
2980 goto error;
2981 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
2982 isl_aff_free(aff);
2983 isl_die(s->ctx, isl_error_invalid,
2984 "not an affine expression", goto error);
2986 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
2987 space = isl_multi_aff_get_domain_space(ma);
2988 aff = isl_aff_reset_domain_space(aff, space);
2989 ma = isl_multi_aff_set_aff(ma, i, aff);
2992 isl_multi_pw_aff_free(tuple);
2993 vars_free(v);
2994 isl_set_free(dom);
2995 return ma;
2996 error:
2997 isl_multi_pw_aff_free(tuple);
2998 vars_free(v);
2999 isl_set_free(dom);
3000 isl_multi_aff_free(ma);
3001 return NULL;
3004 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3005 const char *str)
3007 isl_multi_aff *maff;
3008 struct isl_stream *s = isl_stream_new_str(ctx, str);
3009 if (!s)
3010 return NULL;
3011 maff = isl_stream_read_multi_aff(s);
3012 isl_stream_free(s);
3013 return maff;
3016 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
3017 struct isl_stream *s)
3019 struct isl_obj obj;
3021 obj = obj_read(s);
3022 if (obj.type == isl_obj_pw_qpolynomial) {
3023 obj.type = isl_obj_union_pw_qpolynomial;
3024 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
3026 if (obj.v)
3027 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
3028 goto error);
3030 return obj.v;
3031 error:
3032 obj.type->free(obj.v);
3033 return NULL;
3036 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
3037 isl_ctx *ctx, const char *str)
3039 isl_union_pw_qpolynomial *upwqp;
3040 struct isl_stream *s = isl_stream_new_str(ctx, str);
3041 if (!s)
3042 return NULL;
3043 upwqp = isl_stream_read_union_pw_qpolynomial(s);
3044 isl_stream_free(s);
3045 return upwqp;