privately export isl_map_is_params
[isl.git] / isl_input.c
bloba6ba79e26f1854dc0474060f8a17939f814fcce5
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 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_set.h>
26 #include <isl/union_map.h>
27 #include <isl_mat_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_vec_private.h>
30 #include <isl/list.h>
31 #include <isl_val_private.h>
33 struct variable {
34 char *name;
35 int pos;
36 struct variable *next;
39 struct vars {
40 struct isl_ctx *ctx;
41 int n;
42 struct variable *v;
45 static struct vars *vars_new(struct isl_ctx *ctx)
47 struct vars *v;
48 v = isl_alloc_type(ctx, struct vars);
49 if (!v)
50 return NULL;
51 v->ctx = ctx;
52 v->n = 0;
53 v->v = NULL;
54 return v;
57 static void variable_free(struct variable *var)
59 while (var) {
60 struct variable *next = var->next;
61 free(var->name);
62 free(var);
63 var = next;
67 static void vars_free(struct vars *v)
69 if (!v)
70 return;
71 variable_free(v->v);
72 free(v);
75 static void vars_drop(struct vars *v, int n)
77 struct variable *var;
79 if (!v || !v->v)
80 return;
82 v->n -= n;
84 var = v->v;
85 while (--n >= 0) {
86 struct variable *next = var->next;
87 free(var->name);
88 free(var);
89 var = next;
91 v->v = var;
94 static struct variable *variable_new(struct vars *v, const char *name, int len,
95 int pos)
97 struct variable *var;
98 var = isl_calloc_type(v->ctx, struct variable);
99 if (!var)
100 goto error;
101 var->name = strdup(name);
102 var->name[len] = '\0';
103 var->pos = pos;
104 var->next = v->v;
105 return var;
106 error:
107 variable_free(v->v);
108 return NULL;
111 static int vars_pos(struct vars *v, const char *s, int len)
113 int pos;
114 struct variable *q;
116 if (len == -1)
117 len = strlen(s);
118 for (q = v->v; q; q = q->next) {
119 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
120 break;
122 if (q)
123 pos = q->pos;
124 else {
125 pos = v->n;
126 v->v = variable_new(v, s, len, v->n);
127 if (!v->v)
128 return -1;
129 v->n++;
131 return pos;
134 static int vars_add_anon(struct vars *v)
136 v->v = variable_new(v, "", 0, v->n);
138 if (!v->v)
139 return -1;
140 v->n++;
142 return 0;
145 /* Obtain next token, with some preprocessing.
146 * In particular, evaluate expressions of the form x^y,
147 * with x and y values.
149 static struct isl_token *next_token(__isl_keep isl_stream *s)
151 struct isl_token *tok, *tok2;
153 tok = isl_stream_next_token(s);
154 if (!tok || tok->type != ISL_TOKEN_VALUE)
155 return tok;
156 if (!isl_stream_eat_if_available(s, '^'))
157 return tok;
158 tok2 = isl_stream_next_token(s);
159 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
160 isl_stream_error(s, tok2, "expecting constant value");
161 goto error;
164 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
166 isl_token_free(tok2);
167 return tok;
168 error:
169 isl_token_free(tok);
170 isl_token_free(tok2);
171 return NULL;
174 /* Read an isl_val from "s".
176 * The following token sequences are recognized
178 * "infty" -> infty
179 * "-" "infty" -> -infty
180 * "NaN" -> NaN
181 * n "/" d -> n/d
182 * v -> v
184 * where n, d and v are integer constants.
186 __isl_give isl_val *isl_stream_read_val(__isl_keep isl_stream *s)
188 struct isl_token *tok = NULL;
189 struct isl_token *tok2 = NULL;
190 isl_val *val;
192 tok = next_token(s);
193 if (!tok) {
194 isl_stream_error(s, NULL, "unexpected EOF");
195 goto error;
197 if (tok->type == ISL_TOKEN_INFTY) {
198 isl_token_free(tok);
199 return isl_val_infty(s->ctx);
201 if (tok->type == '-' &&
202 isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
203 isl_token_free(tok);
204 return isl_val_neginfty(s->ctx);
206 if (tok->type == ISL_TOKEN_NAN) {
207 isl_token_free(tok);
208 return isl_val_nan(s->ctx);
210 if (tok->type != ISL_TOKEN_VALUE) {
211 isl_stream_error(s, tok, "expecting value");
212 goto error;
215 if (isl_stream_eat_if_available(s, '/')) {
216 tok2 = next_token(s);
217 if (!tok2) {
218 isl_stream_error(s, NULL, "unexpected EOF");
219 goto error;
221 if (tok2->type != ISL_TOKEN_VALUE) {
222 isl_stream_error(s, tok2, "expecting value");
223 goto error;
225 val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
226 val = isl_val_normalize(val);
227 } else {
228 val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
231 isl_token_free(tok);
232 isl_token_free(tok2);
233 return val;
234 error:
235 isl_token_free(tok);
236 isl_token_free(tok2);
237 return NULL;
240 /* Read an isl_val from "str".
242 struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
243 const char *str)
245 isl_val *val;
246 isl_stream *s = isl_stream_new_str(ctx, str);
247 if (!s)
248 return NULL;
249 val = isl_stream_read_val(s);
250 isl_stream_free(s);
251 return val;
254 static int accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
256 struct isl_token *tok;
258 tok = next_token(s);
259 if (!tok || tok->type != ISL_TOKEN_VALUE) {
260 isl_stream_error(s, tok, "expecting constant value");
261 goto error;
264 isl_int_mul(*f, *f, tok->u.v);
266 isl_token_free(tok);
268 if (isl_stream_eat_if_available(s, '*'))
269 return accept_cst_factor(s, f);
271 return 0;
272 error:
273 isl_token_free(tok);
274 return -1;
277 /* Given an affine expression aff, return an affine expression
278 * for aff % d, with d the next token on the stream, which is
279 * assumed to be a constant.
281 * We introduce an integer division q = [aff/d] and the result
282 * is set to aff - d q.
284 static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
285 struct vars *v, __isl_take isl_pw_aff *aff)
287 struct isl_token *tok;
288 isl_pw_aff *q;
290 tok = next_token(s);
291 if (!tok || tok->type != ISL_TOKEN_VALUE) {
292 isl_stream_error(s, tok, "expecting constant value");
293 goto error;
296 q = isl_pw_aff_copy(aff);
297 q = isl_pw_aff_scale_down(q, tok->u.v);
298 q = isl_pw_aff_floor(q);
299 q = isl_pw_aff_scale(q, tok->u.v);
301 aff = isl_pw_aff_sub(aff, q);
303 isl_token_free(tok);
304 return aff;
305 error:
306 isl_pw_aff_free(aff);
307 isl_token_free(tok);
308 return NULL;
311 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
312 __isl_take isl_space *space, struct vars *v);
313 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
314 __isl_take isl_space *dim, struct vars *v);
316 static __isl_give isl_pw_aff *accept_minmax(__isl_keep isl_stream *s,
317 __isl_take isl_space *dim, struct vars *v)
319 struct isl_token *tok;
320 isl_pw_aff_list *list = NULL;
321 int min;
323 tok = isl_stream_next_token(s);
324 if (!tok)
325 goto error;
326 min = tok->type == ISL_TOKEN_MIN;
327 isl_token_free(tok);
329 if (isl_stream_eat(s, '('))
330 goto error;
332 list = accept_affine_list(s, isl_space_copy(dim), v);
333 if (!list)
334 goto error;
336 if (isl_stream_eat(s, ')'))
337 goto error;
339 isl_space_free(dim);
340 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
341 error:
342 isl_space_free(dim);
343 isl_pw_aff_list_free(list);
344 return NULL;
347 /* Is "tok" the start of an integer division?
349 static int is_start_of_div(struct isl_token *tok)
351 if (!tok)
352 return 0;
353 if (tok->type == '[')
354 return 1;
355 if (tok->type == ISL_TOKEN_FLOOR)
356 return 1;
357 if (tok->type == ISL_TOKEN_CEIL)
358 return 1;
359 if (tok->type == ISL_TOKEN_FLOORD)
360 return 1;
361 if (tok->type == ISL_TOKEN_CEILD)
362 return 1;
363 return 0;
366 /* Read an integer division from "s" and return it as an isl_pw_aff.
368 * The integer division can be of the form
370 * [<affine expression>]
371 * floor(<affine expression>)
372 * ceil(<affine expression>)
373 * floord(<affine expression>,<denominator>)
374 * ceild(<affine expression>,<denominator>)
376 static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
377 __isl_take isl_space *dim, struct vars *v)
379 struct isl_token *tok;
380 int f = 0;
381 int c = 0;
382 int extra = 0;
383 isl_pw_aff *pwaff = NULL;
385 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
386 extra = f = 1;
387 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
388 extra = c = 1;
389 else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
390 f = 1;
391 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
392 c = 1;
393 if (f || c) {
394 if (isl_stream_eat(s, '('))
395 goto error;
396 } else {
397 if (isl_stream_eat(s, '['))
398 goto error;
401 pwaff = accept_affine(s, isl_space_copy(dim), v);
403 if (extra) {
404 if (isl_stream_eat(s, ','))
405 goto error;
407 tok = next_token(s);
408 if (!tok)
409 goto error;
410 if (tok->type != ISL_TOKEN_VALUE) {
411 isl_stream_error(s, tok, "expected denominator");
412 isl_stream_push_token(s, tok);
413 goto error;
415 pwaff = isl_pw_aff_scale_down(pwaff, tok->u.v);
416 isl_token_free(tok);
419 if (c)
420 pwaff = isl_pw_aff_ceil(pwaff);
421 else
422 pwaff = isl_pw_aff_floor(pwaff);
424 if (f || c) {
425 if (isl_stream_eat(s, ')'))
426 goto error;
427 } else {
428 if (isl_stream_eat(s, ']'))
429 goto error;
432 isl_space_free(dim);
433 return pwaff;
434 error:
435 isl_space_free(dim);
436 isl_pw_aff_free(pwaff);
437 return NULL;
440 static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
441 __isl_take isl_space *dim, struct vars *v)
443 struct isl_token *tok = NULL;
444 isl_pw_aff *res = NULL;
446 tok = next_token(s);
447 if (!tok) {
448 isl_stream_error(s, NULL, "unexpected EOF");
449 goto error;
452 if (tok->type == ISL_TOKEN_AFF) {
453 res = isl_pw_aff_copy(tok->u.pwaff);
454 isl_token_free(tok);
455 } else if (tok->type == ISL_TOKEN_IDENT) {
456 int n = v->n;
457 int pos = vars_pos(v, tok->u.s, -1);
458 isl_aff *aff;
460 if (pos < 0)
461 goto error;
462 if (pos >= n) {
463 vars_drop(v, v->n - n);
464 isl_stream_error(s, tok, "unknown identifier");
465 goto error;
468 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
469 if (!aff)
470 goto error;
471 isl_int_set_si(aff->v->el[2 + pos], 1);
472 res = isl_pw_aff_from_aff(aff);
473 isl_token_free(tok);
474 } else if (tok->type == ISL_TOKEN_VALUE) {
475 if (isl_stream_eat_if_available(s, '*')) {
476 res = accept_affine_factor(s, isl_space_copy(dim), v);
477 res = isl_pw_aff_scale(res, tok->u.v);
478 } else {
479 isl_local_space *ls;
480 isl_aff *aff;
481 ls = isl_local_space_from_space(isl_space_copy(dim));
482 aff = isl_aff_zero_on_domain(ls);
483 aff = isl_aff_add_constant(aff, tok->u.v);
484 res = isl_pw_aff_from_aff(aff);
486 isl_token_free(tok);
487 } else if (tok->type == '(') {
488 isl_token_free(tok);
489 tok = NULL;
490 res = accept_affine(s, isl_space_copy(dim), v);
491 if (!res)
492 goto error;
493 if (isl_stream_eat(s, ')'))
494 goto error;
495 } else if (is_start_of_div(tok)) {
496 isl_stream_push_token(s, tok);
497 tok = NULL;
498 res = accept_div(s, isl_space_copy(dim), v);
499 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
500 isl_stream_push_token(s, tok);
501 tok = NULL;
502 res = accept_minmax(s, isl_space_copy(dim), v);
503 } else {
504 isl_stream_error(s, tok, "expecting factor");
505 goto error;
507 if (isl_stream_eat_if_available(s, '%') ||
508 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
509 isl_space_free(dim);
510 return affine_mod(s, v, res);
512 if (isl_stream_eat_if_available(s, '*')) {
513 isl_int f;
514 isl_int_init(f);
515 isl_int_set_si(f, 1);
516 if (accept_cst_factor(s, &f) < 0) {
517 isl_int_clear(f);
518 goto error2;
520 res = isl_pw_aff_scale(res, f);
521 isl_int_clear(f);
523 if (isl_stream_eat_if_available(s, '/')) {
524 isl_int f;
525 isl_int_init(f);
526 isl_int_set_si(f, 1);
527 if (accept_cst_factor(s, &f) < 0) {
528 isl_int_clear(f);
529 goto error2;
531 res = isl_pw_aff_scale_down(res, f);
532 isl_int_clear(f);
535 isl_space_free(dim);
536 return res;
537 error:
538 isl_token_free(tok);
539 error2:
540 isl_pw_aff_free(res);
541 isl_space_free(dim);
542 return NULL;
545 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
547 isl_aff *aff;
548 isl_space *space;
550 space = isl_pw_aff_get_domain_space(pwaff);
551 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
552 aff = isl_aff_add_constant(aff, v);
554 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
557 /* Return a piecewise affine expression defined on the specified domain
558 * that represents NaN.
560 static __isl_give isl_pw_aff *nan_on_domain(__isl_keep isl_space *space)
562 isl_local_space *ls;
564 ls = isl_local_space_from_space(isl_space_copy(space));
565 return isl_pw_aff_nan_on_domain(ls);
568 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
569 __isl_take isl_space *space, struct vars *v)
571 struct isl_token *tok = NULL;
572 isl_local_space *ls;
573 isl_pw_aff *res;
574 int sign = 1;
576 ls = isl_local_space_from_space(isl_space_copy(space));
577 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
578 if (!res)
579 goto error;
581 for (;;) {
582 tok = next_token(s);
583 if (!tok) {
584 isl_stream_error(s, NULL, "unexpected EOF");
585 goto error;
587 if (tok->type == '-') {
588 sign = -sign;
589 isl_token_free(tok);
590 continue;
592 if (tok->type == '(' || is_start_of_div(tok) ||
593 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
594 tok->type == ISL_TOKEN_IDENT ||
595 tok->type == ISL_TOKEN_AFF) {
596 isl_pw_aff *term;
597 isl_stream_push_token(s, tok);
598 tok = NULL;
599 term = accept_affine_factor(s,
600 isl_space_copy(space), v);
601 if (sign < 0)
602 res = isl_pw_aff_sub(res, term);
603 else
604 res = isl_pw_aff_add(res, term);
605 if (!res)
606 goto error;
607 sign = 1;
608 } else if (tok->type == ISL_TOKEN_VALUE) {
609 if (sign < 0)
610 isl_int_neg(tok->u.v, tok->u.v);
611 if (isl_stream_eat_if_available(s, '*') ||
612 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
613 isl_pw_aff *term;
614 term = accept_affine_factor(s,
615 isl_space_copy(space), v);
616 term = isl_pw_aff_scale(term, tok->u.v);
617 res = isl_pw_aff_add(res, term);
618 if (!res)
619 goto error;
620 } else {
621 res = add_cst(res, tok->u.v);
623 sign = 1;
624 } else if (tok->type == ISL_TOKEN_NAN) {
625 res = isl_pw_aff_add(res, nan_on_domain(space));
626 } else {
627 isl_stream_error(s, tok, "unexpected isl_token");
628 isl_stream_push_token(s, tok);
629 isl_pw_aff_free(res);
630 isl_space_free(space);
631 return NULL;
633 isl_token_free(tok);
635 tok = next_token(s);
636 if (tok && tok->type == '-') {
637 sign = -sign;
638 isl_token_free(tok);
639 } else if (tok && tok->type == '+') {
640 /* nothing */
641 isl_token_free(tok);
642 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
643 isl_int_is_neg(tok->u.v)) {
644 isl_stream_push_token(s, tok);
645 } else {
646 if (tok)
647 isl_stream_push_token(s, tok);
648 break;
652 isl_space_free(space);
653 return res;
654 error:
655 isl_space_free(space);
656 isl_token_free(tok);
657 isl_pw_aff_free(res);
658 return NULL;
661 /* Is "type" the type of a comparison operator between lists
662 * of affine expressions?
664 static int is_list_comparator_type(int type)
666 switch (type) {
667 case ISL_TOKEN_LEX_LT:
668 case ISL_TOKEN_LEX_GT:
669 case ISL_TOKEN_LEX_LE:
670 case ISL_TOKEN_LEX_GE:
671 return 1;
672 default:
673 return 0;
677 static int is_comparator(struct isl_token *tok)
679 if (!tok)
680 return 0;
681 if (is_list_comparator_type(tok->type))
682 return 1;
684 switch (tok->type) {
685 case ISL_TOKEN_LT:
686 case ISL_TOKEN_GT:
687 case ISL_TOKEN_LE:
688 case ISL_TOKEN_GE:
689 case ISL_TOKEN_NE:
690 case '=':
691 return 1;
692 default:
693 return 0;
697 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
698 struct vars *v, __isl_take isl_map *map, int rational);
699 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
700 __isl_take isl_space *dim, struct vars *v, int rational);
702 /* Accept a ternary operator, given the first argument.
704 static __isl_give isl_pw_aff *accept_ternary(__isl_keep isl_stream *s,
705 __isl_take isl_map *cond, struct vars *v, int rational)
707 isl_space *dim;
708 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
710 if (!cond)
711 return NULL;
713 if (isl_stream_eat(s, '?'))
714 goto error;
716 dim = isl_space_wrap(isl_map_get_space(cond));
717 pwaff1 = accept_extended_affine(s, dim, v, rational);
718 if (!pwaff1)
719 goto error;
721 if (isl_stream_eat(s, ':'))
722 goto error;
724 dim = isl_pw_aff_get_domain_space(pwaff1);
725 pwaff2 = accept_extended_affine(s, dim, v, rational);
726 if (!pwaff1)
727 goto error;
729 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
730 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
731 error:
732 isl_map_free(cond);
733 isl_pw_aff_free(pwaff1);
734 isl_pw_aff_free(pwaff2);
735 return NULL;
738 /* Set *line and *col to those of the next token, if any.
740 static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
742 struct isl_token *tok;
744 tok = isl_stream_next_token(s);
745 if (!tok)
746 return;
748 *line = tok->line;
749 *col = tok->col;
750 isl_stream_push_token(s, tok);
753 /* Push a token encapsulating "pa" onto "s", with the given
754 * line and column.
756 static int push_aff(__isl_keep isl_stream *s, int line, int col,
757 __isl_take isl_pw_aff *pa)
759 struct isl_token *tok;
761 tok = isl_token_new(s->ctx, line, col, 0);
762 if (!tok)
763 goto error;
764 tok->type = ISL_TOKEN_AFF;
765 tok->u.pwaff = pa;
766 isl_stream_push_token(s, tok);
768 return 0;
769 error:
770 isl_pw_aff_free(pa);
771 return -1;
774 /* Accept an affine expression that may involve ternary operators.
775 * We first read an affine expression.
776 * If it is not followed by a comparison operator, we simply return it.
777 * Otherwise, we assume the affine expression is part of the first
778 * argument of a ternary operator and try to parse that.
780 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
781 __isl_take isl_space *dim, struct vars *v, int rational)
783 isl_space *space;
784 isl_map *cond;
785 isl_pw_aff *pwaff;
786 struct isl_token *tok;
787 int line = -1, col = -1;
788 int is_comp;
790 set_current_line_col(s, &line, &col);
792 pwaff = accept_affine(s, dim, v);
793 if (rational)
794 pwaff = isl_pw_aff_set_rational(pwaff);
795 if (!pwaff)
796 return NULL;
798 tok = isl_stream_next_token(s);
799 if (!tok)
800 return isl_pw_aff_free(pwaff);
802 is_comp = is_comparator(tok);
803 isl_stream_push_token(s, tok);
804 if (!is_comp)
805 return pwaff;
807 space = isl_pw_aff_get_domain_space(pwaff);
808 cond = isl_map_universe(isl_space_unwrap(space));
810 if (push_aff(s, line, col, pwaff) < 0)
811 cond = isl_map_free(cond);
812 if (!cond)
813 return NULL;
815 cond = read_formula(s, v, cond, rational);
817 return accept_ternary(s, cond, v, rational);
820 static __isl_give isl_map *read_var_def(__isl_keep isl_stream *s,
821 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
822 int rational)
824 isl_pw_aff *def;
825 int pos;
826 isl_map *def_map;
828 if (type == isl_dim_param)
829 pos = isl_map_dim(map, isl_dim_param);
830 else {
831 pos = isl_map_dim(map, isl_dim_in);
832 if (type == isl_dim_out)
833 pos += isl_map_dim(map, isl_dim_out);
834 type = isl_dim_in;
836 --pos;
838 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
839 v, rational);
840 def_map = isl_map_from_pw_aff(def);
841 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
842 def_map = isl_set_unwrap(isl_map_domain(def_map));
844 map = isl_map_intersect(map, def_map);
846 return map;
849 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
850 __isl_take isl_space *dim, struct vars *v)
852 isl_pw_aff *pwaff;
853 isl_pw_aff_list *list;
854 struct isl_token *tok = NULL;
856 pwaff = accept_affine(s, isl_space_copy(dim), v);
857 list = isl_pw_aff_list_from_pw_aff(pwaff);
858 if (!list)
859 goto error;
861 for (;;) {
862 tok = isl_stream_next_token(s);
863 if (!tok) {
864 isl_stream_error(s, NULL, "unexpected EOF");
865 goto error;
867 if (tok->type != ',') {
868 isl_stream_push_token(s, tok);
869 break;
871 isl_token_free(tok);
873 pwaff = accept_affine(s, isl_space_copy(dim), v);
874 list = isl_pw_aff_list_concat(list,
875 isl_pw_aff_list_from_pw_aff(pwaff));
876 if (!list)
877 goto error;
880 isl_space_free(dim);
881 return list;
882 error:
883 isl_space_free(dim);
884 isl_pw_aff_list_free(list);
885 return NULL;
888 static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
889 struct vars *v, __isl_take isl_map *map, int rational)
891 struct isl_token *tok;
893 while ((tok = isl_stream_next_token(s)) != NULL) {
894 int p;
895 int n = v->n;
897 if (tok->type != ISL_TOKEN_IDENT)
898 break;
900 p = vars_pos(v, tok->u.s, -1);
901 if (p < 0)
902 goto error;
903 if (p < n) {
904 isl_stream_error(s, tok, "expecting unique identifier");
905 goto error;
908 map = isl_map_add_dims(map, isl_dim_out, 1);
910 isl_token_free(tok);
911 tok = isl_stream_next_token(s);
912 if (tok && tok->type == '=') {
913 isl_token_free(tok);
914 map = read_var_def(s, map, isl_dim_out, v, rational);
915 tok = isl_stream_next_token(s);
918 if (!tok || tok->type != ',')
919 break;
921 isl_token_free(tok);
923 if (tok)
924 isl_stream_push_token(s, tok);
926 return map;
927 error:
928 isl_token_free(tok);
929 isl_map_free(map);
930 return NULL;
933 static int next_is_tuple(__isl_keep isl_stream *s)
935 struct isl_token *tok;
936 int is_tuple;
938 tok = isl_stream_next_token(s);
939 if (!tok)
940 return 0;
941 if (tok->type == '[') {
942 isl_stream_push_token(s, tok);
943 return 1;
945 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
946 isl_stream_push_token(s, tok);
947 return 0;
950 is_tuple = isl_stream_next_token_is(s, '[');
952 isl_stream_push_token(s, tok);
954 return is_tuple;
957 /* Is "pa" an expression in term of earlier dimensions?
958 * The alternative is that the dimension is defined to be equal to itself,
959 * meaning that it has a universe domain and an expression that depends
960 * on itself. "i" is the position of the expression in a sequence
961 * of "n" expressions. The final dimensions of "pa" correspond to
962 * these "n" expressions.
964 static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
966 isl_aff *aff;
968 if (!pa)
969 return -1;
970 if (pa->n != 1)
971 return 1;
972 if (!isl_set_plain_is_universe(pa->p[0].set))
973 return 1;
975 aff = pa->p[0].aff;
976 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
977 return 1;
978 return 0;
981 /* Does the tuple contain any dimensions that are defined
982 * in terms of earlier dimensions?
984 static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
986 int i, n;
987 int has_expr = 0;
988 isl_pw_aff *pa;
990 if (!tuple)
991 return -1;
992 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
993 for (i = 0; i < n; ++i) {
994 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
995 has_expr = pw_aff_is_expr(pa, i, n);
996 isl_pw_aff_free(pa);
997 if (has_expr < 0 || has_expr)
998 break;
1001 return has_expr;
1004 /* Set the name of dimension "pos" in "space" to "name".
1005 * During printing, we add primes if the same name appears more than once
1006 * to distinguish the occurrences. Here, we remove those primes from "name"
1007 * before setting the name of the dimension.
1009 static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
1010 int pos, char *name)
1012 char *prime;
1014 if (!name)
1015 return space;
1017 prime = strchr(name, '\'');
1018 if (prime)
1019 *prime = '\0';
1020 space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1021 if (prime)
1022 *prime = '\'';
1024 return space;
1027 /* Accept a piecewise affine expression.
1029 * At the outer level, the piecewise affine expression may be of the form
1031 * aff1 : condition1; aff2 : conditions2; ...
1033 * or simply
1035 * aff
1037 * each of the affine expressions may in turn include ternary operators.
1039 * There may be parentheses around some subexpression of "aff1"
1040 * around "aff1" itself, around "aff1 : condition1" and/or
1041 * around the entire piecewise affine expression.
1042 * We therefore remove the opening parenthesis (if any) from the stream
1043 * in case the closing parenthesis follows the colon, but if the closing
1044 * parenthesis is the first thing in the stream after the parsed affine
1045 * expression, we push the parsed expression onto the stream and parse
1046 * again in case the parentheses enclose some subexpression of "aff1".
1048 static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1049 __isl_take isl_space *space, struct vars *v, int rational)
1051 isl_pw_aff *res;
1052 isl_space *res_space;
1054 res_space = isl_space_from_domain(isl_space_copy(space));
1055 res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1056 res = isl_pw_aff_empty(res_space);
1057 do {
1058 isl_pw_aff *pa;
1059 int seen_paren;
1060 int line = -1, col = -1;
1062 set_current_line_col(s, &line, &col);
1063 seen_paren = isl_stream_eat_if_available(s, '(');
1064 if (seen_paren)
1065 pa = accept_piecewise_affine(s, isl_space_copy(space),
1066 v, rational);
1067 else
1068 pa = accept_extended_affine(s, isl_space_copy(space),
1069 v, rational);
1070 if (seen_paren && isl_stream_eat_if_available(s, ')')) {
1071 seen_paren = 0;
1072 if (push_aff(s, line, col, pa) < 0)
1073 goto error;
1074 pa = accept_extended_affine(s, isl_space_copy(space),
1075 v, rational);
1077 if (isl_stream_eat_if_available(s, ':')) {
1078 isl_space *dom_space;
1079 isl_set *dom;
1081 dom_space = isl_pw_aff_get_domain_space(pa);
1082 dom = isl_set_universe(dom_space);
1083 dom = read_formula(s, v, dom, rational);
1084 pa = isl_pw_aff_intersect_domain(pa, dom);
1087 res = isl_pw_aff_union_add(res, pa);
1089 if (seen_paren && isl_stream_eat(s, ')'))
1090 goto error;
1091 } while (isl_stream_eat_if_available(s, ';'));
1093 isl_space_free(space);
1095 return res;
1096 error:
1097 isl_space_free(space);
1098 return isl_pw_aff_free(res);
1101 /* Read an affine expression from "s" for use in read_tuple.
1103 * accept_extended_affine requires a wrapped space as input.
1104 * read_tuple on the other hand expects each isl_pw_aff
1105 * to have an anonymous space. We therefore adjust the space
1106 * of the isl_pw_aff before returning it.
1108 static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1109 struct vars *v, int rational)
1111 isl_space *space;
1112 isl_pw_aff *def;
1114 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1116 def = accept_piecewise_affine(s, space, v, rational);
1118 space = isl_space_set_alloc(s->ctx, 0, v->n);
1119 def = isl_pw_aff_reset_domain_space(def, space);
1121 return def;
1124 /* Read a list of tuple elements by calling "read_el" on each of them and
1125 * return a space with the same number of set dimensions derived from
1126 * the parameter space "space" and possibly updated by "read_el".
1127 * The elements in the list are separated by either "," or "][".
1128 * If "comma" is set then only "," is allowed.
1130 static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1131 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1132 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1133 struct vars *v, __isl_take isl_space *space, int rational,
1134 void *user),
1135 void *user)
1137 if (!space)
1138 return NULL;
1140 space = isl_space_set_from_params(space);
1142 if (isl_stream_next_token_is(s, ']'))
1143 return space;
1145 for (;;) {
1146 struct isl_token *tok;
1148 space = isl_space_add_dims(space, isl_dim_set, 1);
1150 space = read_el(s, v, space, rational, user);
1151 if (!space)
1152 return NULL;
1154 tok = isl_stream_next_token(s);
1155 if (!comma && tok && tok->type == ']' &&
1156 isl_stream_next_token_is(s, '[')) {
1157 isl_token_free(tok);
1158 tok = isl_stream_next_token(s);
1159 } else if (!tok || tok->type != ',') {
1160 if (tok)
1161 isl_stream_push_token(s, tok);
1162 break;
1165 isl_token_free(tok);
1168 return space;
1171 /* Read a tuple space from "s" derived from the parameter space "space".
1172 * Call "read_el" on each element in the tuples.
1174 static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1175 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1176 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1177 struct vars *v, __isl_take isl_space *space, int rational,
1178 void *user),
1179 void *user)
1181 struct isl_token *tok;
1182 char *name = NULL;
1183 isl_space *res = NULL;
1185 tok = isl_stream_next_token(s);
1186 if (!tok)
1187 goto error;
1188 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1189 name = strdup(tok->u.s);
1190 isl_token_free(tok);
1191 if (!name)
1192 goto error;
1193 } else
1194 isl_stream_push_token(s, tok);
1195 if (isl_stream_eat(s, '['))
1196 goto error;
1197 if (next_is_tuple(s)) {
1198 isl_space *out;
1199 res = read_tuple_space(s, v, isl_space_copy(space),
1200 rational, comma, read_el, user);
1201 if (isl_stream_eat(s, ISL_TOKEN_TO))
1202 goto error;
1203 out = read_tuple_space(s, v, isl_space_copy(space),
1204 rational, comma, read_el, user);
1205 res = isl_space_product(res, out);
1206 } else
1207 res = read_tuple_list(s, v, isl_space_copy(space),
1208 rational, comma, read_el, user);
1209 if (isl_stream_eat(s, ']'))
1210 goto error;
1212 if (name) {
1213 res = isl_space_set_tuple_name(res, isl_dim_set, name);
1214 free(name);
1217 isl_space_free(space);
1218 return res;
1219 error:
1220 free(name);
1221 isl_space_free(res);
1222 isl_space_free(space);
1223 return NULL;
1226 /* Construct an isl_pw_aff defined on a space with v->n variables
1227 * that is equal to the last of those variables.
1229 static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1231 isl_space *space;
1232 isl_aff *aff;
1234 space = isl_space_set_alloc(v->ctx, 0, v->n);
1235 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1236 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1237 return isl_pw_aff_from_aff(aff);
1240 /* This function is called for each element in a tuple inside read_tuple.
1241 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1242 * over a space containing all variables in "v" defined so far.
1243 * The isl_pw_aff expresses the new variable in terms of earlier variables
1244 * if a definition is provided. Otherwise, it is represented as being
1245 * equal to itself.
1246 * Add the isl_pw_aff to *list.
1247 * If the new variable was named, then adjust "space" accordingly and
1248 * return the updated space.
1250 static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1251 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1253 isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1254 isl_pw_aff *pa;
1255 struct isl_token *tok;
1256 int new_name = 0;
1258 tok = next_token(s);
1259 if (!tok) {
1260 isl_stream_error(s, NULL, "unexpected EOF");
1261 return isl_space_free(space);
1264 if (tok->type == ISL_TOKEN_IDENT) {
1265 int n = v->n;
1266 int p = vars_pos(v, tok->u.s, -1);
1267 if (p < 0)
1268 goto error;
1269 new_name = p >= n;
1272 if (tok->type == '*') {
1273 if (vars_add_anon(v) < 0)
1274 goto error;
1275 isl_token_free(tok);
1276 pa = identity_tuple_el(v);
1277 } else if (new_name) {
1278 int pos = isl_space_dim(space, isl_dim_out) - 1;
1279 space = space_set_dim_name(space, pos, v->v->name);
1280 isl_token_free(tok);
1281 if (isl_stream_eat_if_available(s, '='))
1282 pa = read_tuple_var_def(s, v, rational);
1283 else
1284 pa = identity_tuple_el(v);
1285 } else {
1286 isl_stream_push_token(s, tok);
1287 tok = NULL;
1288 if (vars_add_anon(v) < 0)
1289 goto error;
1290 pa = read_tuple_var_def(s, v, rational);
1293 *list = isl_pw_aff_list_add(*list, pa);
1294 if (!*list)
1295 return isl_space_free(space);
1297 return space;
1298 error:
1299 isl_token_free(tok);
1300 return isl_space_free(space);
1303 /* Read a tuple and represent it as an isl_multi_pw_aff.
1304 * The range space of the isl_multi_pw_aff is the space of the tuple.
1305 * The domain space is an anonymous space
1306 * with a dimension for each variable in the set of variables in "v",
1307 * including the variables in the range.
1308 * If a given dimension is not defined in terms of earlier dimensions in
1309 * the input, then the corresponding isl_pw_aff is set equal to one time
1310 * the variable corresponding to the dimension being defined.
1312 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1313 * Each element in this list is defined over a space representing
1314 * the variables defined so far. We need to adjust the earlier
1315 * elements to have as many variables in the domain as the final
1316 * element in the list.
1318 static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1319 struct vars *v, int rational, int comma)
1321 int i, n;
1322 isl_space *space;
1323 isl_pw_aff_list *list;
1325 space = isl_space_params_alloc(v->ctx, 0);
1326 list = isl_pw_aff_list_alloc(s->ctx, 0);
1327 space = read_tuple_space(s, v, space, rational, comma,
1328 &read_tuple_pw_aff_el, &list);
1329 n = isl_space_dim(space, isl_dim_set);
1330 for (i = 0; i + 1 < n; ++i) {
1331 isl_pw_aff *pa;
1333 pa = isl_pw_aff_list_get_pw_aff(list, i);
1334 pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1335 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1338 space = isl_space_from_range(space);
1339 space = isl_space_add_dims(space, isl_dim_in, v->n);
1340 return isl_multi_pw_aff_from_pw_aff_list(space, list);
1343 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1344 * We first create the appropriate space in "map" based on the range
1345 * space of this isl_multi_pw_aff. Then, we add equalities based
1346 * on the affine expressions. These live in an anonymous space,
1347 * however, so we first need to reset the space to that of "map".
1349 static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1350 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1351 int rational)
1353 int i, n;
1354 isl_ctx *ctx;
1355 isl_space *space = NULL;
1357 if (!map || !tuple)
1358 goto error;
1359 ctx = isl_multi_pw_aff_get_ctx(tuple);
1360 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1361 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1362 if (!space)
1363 goto error;
1365 if (type == isl_dim_param) {
1366 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1367 isl_space_is_wrapping(space)) {
1368 isl_die(ctx, isl_error_invalid,
1369 "parameter tuples cannot be named or nested",
1370 goto error);
1372 map = isl_map_add_dims(map, type, n);
1373 for (i = 0; i < n; ++i) {
1374 isl_id *id;
1375 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1376 isl_die(ctx, isl_error_invalid,
1377 "parameters must be named",
1378 goto error);
1379 id = isl_space_get_dim_id(space, isl_dim_set, i);
1380 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1382 } else if (type == isl_dim_in) {
1383 isl_set *set;
1385 set = isl_set_universe(isl_space_copy(space));
1386 if (rational)
1387 set = isl_set_set_rational(set);
1388 set = isl_set_intersect_params(set, isl_map_params(map));
1389 map = isl_map_from_domain(set);
1390 } else {
1391 isl_set *set;
1393 set = isl_set_universe(isl_space_copy(space));
1394 if (rational)
1395 set = isl_set_set_rational(set);
1396 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1399 for (i = 0; i < n; ++i) {
1400 isl_pw_aff *pa;
1401 isl_space *space;
1402 isl_aff *aff;
1403 isl_set *set;
1404 isl_map *map_i;
1406 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1407 space = isl_pw_aff_get_domain_space(pa);
1408 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1409 aff = isl_aff_add_coefficient_si(aff,
1410 isl_dim_in, v->n - n + i, -1);
1411 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1412 if (rational)
1413 pa = isl_pw_aff_set_rational(pa);
1414 set = isl_pw_aff_zero_set(pa);
1415 map_i = isl_map_from_range(set);
1416 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1417 map = isl_map_intersect(map, map_i);
1420 isl_space_free(space);
1421 isl_multi_pw_aff_free(tuple);
1422 return map;
1423 error:
1424 isl_space_free(space);
1425 isl_multi_pw_aff_free(tuple);
1426 isl_map_free(map);
1427 return NULL;
1430 /* Read a tuple from "s" and add it to "map".
1431 * The tuple is initially represented as an isl_multi_pw_aff and
1432 * then added to "map".
1434 static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1435 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1436 int rational, int comma)
1438 isl_multi_pw_aff *tuple;
1440 tuple = read_tuple(s, v, rational, comma);
1441 if (!tuple)
1442 return isl_map_free(map);
1444 return map_from_tuple(tuple, map, type, v, rational);
1447 /* Given two equal-length lists of piecewise affine expression with the space
1448 * of "set" as domain, construct a set in the same space that expresses
1449 * that "left" and "right" satisfy the comparison "type".
1451 * A space is constructed of the same dimension as the number of elements
1452 * in the two lists. The comparison is then expressed in a map from
1453 * this space to itself and wrapped into a set. Finally the two lists
1454 * of piecewise affine expressions are plugged into this set.
1456 * Let S be the space of "set" and T the constructed space.
1457 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1458 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1459 * while the comparison is first expressed in T -> T, then [T -> T]
1460 * and finally in S.
1462 static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1463 __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1465 isl_space *space;
1466 int n;
1467 isl_multi_pw_aff *mpa1, *mpa2;
1469 if (!set || !left || !right)
1470 goto error;
1472 space = isl_set_get_space(set);
1473 n = isl_pw_aff_list_n_pw_aff(left);
1474 space = isl_space_from_domain(space);
1475 space = isl_space_add_dims(space, isl_dim_out, n);
1476 mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1477 mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1478 mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1480 space = isl_space_range(space);
1481 switch (type) {
1482 case ISL_TOKEN_LEX_LT:
1483 set = isl_map_wrap(isl_map_lex_lt(space));
1484 break;
1485 case ISL_TOKEN_LEX_GT:
1486 set = isl_map_wrap(isl_map_lex_gt(space));
1487 break;
1488 case ISL_TOKEN_LEX_LE:
1489 set = isl_map_wrap(isl_map_lex_le(space));
1490 break;
1491 case ISL_TOKEN_LEX_GE:
1492 set = isl_map_wrap(isl_map_lex_ge(space));
1493 break;
1494 default:
1495 isl_multi_pw_aff_free(mpa1);
1496 isl_space_free(space);
1497 isl_die(isl_set_get_ctx(set), isl_error_internal,
1498 "unhandled list comparison type", return NULL);
1500 set = isl_set_preimage_multi_pw_aff(set, mpa1);
1501 return set;
1502 error:
1503 isl_pw_aff_list_free(left);
1504 isl_pw_aff_list_free(right);
1505 return NULL;
1508 /* Construct constraints of the form
1510 * a op b
1512 * where a is an element in "left", op is an operator of type "type" and
1513 * b is an element in "right", add the constraints to "set" and return
1514 * the result.
1515 * "rational" is set if the constraints should be treated as
1516 * a rational constraints.
1518 * If "type" is the type of a comparison operator between lists
1519 * of affine expressions, then a single (compound) constraint
1520 * is constructed by list_cmp instead.
1522 static __isl_give isl_set *construct_constraints(
1523 __isl_take isl_set *set, int type,
1524 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1525 int rational)
1527 isl_set *cond;
1529 left = isl_pw_aff_list_copy(left);
1530 right = isl_pw_aff_list_copy(right);
1531 if (rational) {
1532 left = isl_pw_aff_list_set_rational(left);
1533 right = isl_pw_aff_list_set_rational(right);
1535 if (is_list_comparator_type(type))
1536 cond = list_cmp(set, type, left, right);
1537 else if (type == ISL_TOKEN_LE)
1538 cond = isl_pw_aff_list_le_set(left, right);
1539 else if (type == ISL_TOKEN_GE)
1540 cond = isl_pw_aff_list_ge_set(left, right);
1541 else if (type == ISL_TOKEN_LT)
1542 cond = isl_pw_aff_list_lt_set(left, right);
1543 else if (type == ISL_TOKEN_GT)
1544 cond = isl_pw_aff_list_gt_set(left, right);
1545 else if (type == ISL_TOKEN_NE)
1546 cond = isl_pw_aff_list_ne_set(left, right);
1547 else
1548 cond = isl_pw_aff_list_eq_set(left, right);
1550 return isl_set_intersect(set, cond);
1553 /* Read a constraint from "s", add it to "map" and return the result.
1554 * "v" contains a description of the identifiers parsed so far.
1555 * "rational" is set if the constraint should be treated as
1556 * a rational constraint.
1557 * The constraint read from "s" may be applied to multiple pairs
1558 * of affine expressions and may be chained.
1559 * In particular, a list of affine expressions is read, followed
1560 * by a comparison operator and another list of affine expressions.
1561 * The comparison operator is then applied to each pair of elements
1562 * in the two lists and the results are added to "map".
1563 * However, if the operator expects two lists of affine expressions,
1564 * then it is applied directly to those lists and the two lists
1565 * are required to have the same length.
1566 * If the next token is another comparison operator, then another
1567 * list of affine expressions is read and the process repeats.
1569 * The processing is performed on a wrapped copy of "map" because
1570 * an affine expression cannot have a binary relation as domain.
1572 static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1573 struct vars *v, __isl_take isl_map *map, int rational)
1575 struct isl_token *tok;
1576 int type;
1577 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1578 int n1, n2;
1579 isl_set *set;
1581 set = isl_map_wrap(map);
1582 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1583 if (!list1)
1584 goto error;
1585 tok = isl_stream_next_token(s);
1586 if (!is_comparator(tok)) {
1587 isl_stream_error(s, tok, "missing operator");
1588 if (tok)
1589 isl_stream_push_token(s, tok);
1590 goto error;
1592 type = tok->type;
1593 isl_token_free(tok);
1594 for (;;) {
1595 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1596 if (!list2)
1597 goto error;
1598 n1 = isl_pw_aff_list_n_pw_aff(list1);
1599 n2 = isl_pw_aff_list_n_pw_aff(list2);
1600 if (is_list_comparator_type(type) && n1 != n2) {
1601 isl_stream_error(s, NULL,
1602 "list arguments not of same size");
1603 goto error;
1606 set = construct_constraints(set, type, list1, list2, rational);
1607 isl_pw_aff_list_free(list1);
1608 list1 = list2;
1610 tok = isl_stream_next_token(s);
1611 if (!is_comparator(tok)) {
1612 if (tok)
1613 isl_stream_push_token(s, tok);
1614 break;
1616 type = tok->type;
1617 isl_token_free(tok);
1619 isl_pw_aff_list_free(list1);
1621 return isl_set_unwrap(set);
1622 error:
1623 isl_pw_aff_list_free(list1);
1624 isl_pw_aff_list_free(list2);
1625 isl_set_free(set);
1626 return NULL;
1629 static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
1630 struct vars *v, __isl_take isl_map *map, int rational)
1632 int n = v->n;
1633 int seen_paren = isl_stream_eat_if_available(s, '(');
1635 map = isl_map_from_domain(isl_map_wrap(map));
1636 map = read_defined_var_list(s, v, map, rational);
1638 if (isl_stream_eat(s, ':'))
1639 goto error;
1641 map = read_formula(s, v, map, rational);
1642 map = isl_set_unwrap(isl_map_domain(map));
1644 vars_drop(v, v->n - n);
1645 if (seen_paren && isl_stream_eat(s, ')'))
1646 goto error;
1648 return map;
1649 error:
1650 isl_map_free(map);
1651 return NULL;
1654 /* Parse an expression between parentheses and push the result
1655 * back on the stream.
1657 * The parsed expression may be either an affine expression
1658 * or a condition. The first type is pushed onto the stream
1659 * as an isl_pw_aff, while the second is pushed as an isl_map.
1661 * If the initial token indicates the start of a condition,
1662 * we parse it as such.
1663 * Otherwise, we first parse an affine expression and push
1664 * that onto the stream. If the affine expression covers the
1665 * entire expression between parentheses, we return.
1666 * Otherwise, we assume that the affine expression is the
1667 * start of a condition and continue parsing.
1669 static int resolve_paren_expr(__isl_keep isl_stream *s,
1670 struct vars *v, __isl_take isl_map *map, int rational)
1672 struct isl_token *tok, *tok2;
1673 int line, col;
1674 isl_pw_aff *pwaff;
1676 tok = isl_stream_next_token(s);
1677 if (!tok || tok->type != '(')
1678 goto error;
1680 if (isl_stream_next_token_is(s, '('))
1681 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1682 goto error;
1684 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1685 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1686 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1687 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1688 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1689 map = read_formula(s, v, map, rational);
1690 if (isl_stream_eat(s, ')'))
1691 goto error;
1692 tok->type = ISL_TOKEN_MAP;
1693 tok->u.map = map;
1694 isl_stream_push_token(s, tok);
1695 return 0;
1698 tok2 = isl_stream_next_token(s);
1699 if (!tok2)
1700 goto error;
1701 line = tok2->line;
1702 col = tok2->col;
1703 isl_stream_push_token(s, tok2);
1705 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1706 if (!pwaff)
1707 goto error;
1709 tok2 = isl_token_new(s->ctx, line, col, 0);
1710 if (!tok2)
1711 goto error2;
1712 tok2->type = ISL_TOKEN_AFF;
1713 tok2->u.pwaff = pwaff;
1715 if (isl_stream_eat_if_available(s, ')')) {
1716 isl_stream_push_token(s, tok2);
1717 isl_token_free(tok);
1718 isl_map_free(map);
1719 return 0;
1722 isl_stream_push_token(s, tok2);
1724 map = read_formula(s, v, map, rational);
1725 if (isl_stream_eat(s, ')'))
1726 goto error;
1728 tok->type = ISL_TOKEN_MAP;
1729 tok->u.map = map;
1730 isl_stream_push_token(s, tok);
1732 return 0;
1733 error2:
1734 isl_pw_aff_free(pwaff);
1735 error:
1736 isl_token_free(tok);
1737 isl_map_free(map);
1738 return -1;
1741 static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
1742 struct vars *v, __isl_take isl_map *map, int rational)
1744 if (isl_stream_next_token_is(s, '('))
1745 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1746 goto error;
1748 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1749 struct isl_token *tok;
1750 tok = isl_stream_next_token(s);
1751 if (!tok)
1752 goto error;
1753 isl_map_free(map);
1754 map = isl_map_copy(tok->u.map);
1755 isl_token_free(tok);
1756 return map;
1759 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1760 return read_exists(s, v, map, rational);
1762 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1763 return map;
1765 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1766 isl_space *dim = isl_map_get_space(map);
1767 isl_map_free(map);
1768 return isl_map_empty(dim);
1771 return add_constraint(s, v, map, rational);
1772 error:
1773 isl_map_free(map);
1774 return NULL;
1777 static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
1778 struct vars *v, __isl_take isl_map *map, int rational)
1780 isl_map *res;
1781 int negate;
1783 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1784 res = read_conjunct(s, v, isl_map_copy(map), rational);
1785 if (negate)
1786 res = isl_map_subtract(isl_map_copy(map), res);
1788 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1789 isl_map *res_i;
1791 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1792 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1793 if (negate)
1794 res = isl_map_subtract(res, res_i);
1795 else
1796 res = isl_map_intersect(res, res_i);
1799 isl_map_free(map);
1800 return res;
1803 static struct isl_map *read_disjuncts(__isl_keep isl_stream *s,
1804 struct vars *v, __isl_take isl_map *map, int rational)
1806 isl_map *res;
1808 if (isl_stream_next_token_is(s, '}'))
1809 return map;
1811 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1812 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1813 isl_map *res_i;
1815 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1816 res = isl_map_union(res, res_i);
1819 isl_map_free(map);
1820 return res;
1823 /* Read a first order formula from "s", add the corresponding
1824 * constraints to "map" and return the result.
1826 * In particular, read a formula of the form
1830 * or
1832 * a implies b
1834 * where a and b are disjunctions.
1836 * In the first case, map is replaced by
1838 * map \cap { [..] : a }
1840 * In the second case, it is replaced by
1842 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1844 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
1845 struct vars *v, __isl_take isl_map *map, int rational)
1847 isl_map *res;
1849 res = read_disjuncts(s, v, isl_map_copy(map), rational);
1851 if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
1852 isl_map *res2;
1854 res = isl_map_subtract(isl_map_copy(map), res);
1855 res2 = read_disjuncts(s, v, map, rational);
1856 res = isl_map_union(res, res2);
1857 } else
1858 isl_map_free(map);
1860 return res;
1863 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1865 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1866 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1867 isl_basic_map_dim(bmap, isl_dim_in) + pos;
1868 pos -= isl_basic_map_dim(bmap, isl_dim_out);
1870 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1871 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1872 pos -= isl_basic_map_dim(bmap, isl_dim_in);
1874 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1875 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1876 isl_basic_map_dim(bmap, isl_dim_in) +
1877 isl_basic_map_dim(bmap, isl_dim_out) + pos;
1878 pos -= isl_basic_map_dim(bmap, isl_dim_div);
1880 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1881 return 1 + pos;
1883 return 0;
1886 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1887 __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
1889 int j;
1890 struct isl_token *tok;
1891 int type;
1892 int k;
1893 isl_int *c;
1895 if (!bmap)
1896 return NULL;
1898 tok = isl_stream_next_token(s);
1899 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1900 isl_stream_error(s, tok, "expecting coefficient");
1901 if (tok)
1902 isl_stream_push_token(s, tok);
1903 goto error;
1905 if (!tok->on_new_line) {
1906 isl_stream_error(s, tok, "coefficient should appear on new line");
1907 isl_stream_push_token(s, tok);
1908 goto error;
1911 type = isl_int_get_si(tok->u.v);
1912 isl_token_free(tok);
1914 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1915 if (type == 0) {
1916 k = isl_basic_map_alloc_equality(bmap);
1917 c = bmap->eq[k];
1918 } else {
1919 k = isl_basic_map_alloc_inequality(bmap);
1920 c = bmap->ineq[k];
1922 if (k < 0)
1923 goto error;
1925 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
1926 int pos;
1927 tok = isl_stream_next_token(s);
1928 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1929 isl_stream_error(s, tok, "expecting coefficient");
1930 if (tok)
1931 isl_stream_push_token(s, tok);
1932 goto error;
1934 if (tok->on_new_line) {
1935 isl_stream_error(s, tok,
1936 "coefficient should not appear on new line");
1937 isl_stream_push_token(s, tok);
1938 goto error;
1940 pos = polylib_pos_to_isl_pos(bmap, j);
1941 isl_int_set(c[pos], tok->u.v);
1942 isl_token_free(tok);
1945 return bmap;
1946 error:
1947 isl_basic_map_free(bmap);
1948 return NULL;
1951 static __isl_give isl_basic_map *basic_map_read_polylib(
1952 __isl_keep isl_stream *s)
1954 int i;
1955 struct isl_token *tok;
1956 struct isl_token *tok2;
1957 int n_row, n_col;
1958 int on_new_line;
1959 unsigned in = 0, out, local = 0;
1960 struct isl_basic_map *bmap = NULL;
1961 int nparam = 0;
1963 tok = isl_stream_next_token(s);
1964 if (!tok) {
1965 isl_stream_error(s, NULL, "unexpected EOF");
1966 return NULL;
1968 tok2 = isl_stream_next_token(s);
1969 if (!tok2) {
1970 isl_token_free(tok);
1971 isl_stream_error(s, NULL, "unexpected EOF");
1972 return NULL;
1974 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1975 isl_stream_push_token(s, tok2);
1976 isl_stream_push_token(s, tok);
1977 isl_stream_error(s, NULL,
1978 "expecting constraint matrix dimensions");
1979 return NULL;
1981 n_row = isl_int_get_si(tok->u.v);
1982 n_col = isl_int_get_si(tok2->u.v);
1983 on_new_line = tok2->on_new_line;
1984 isl_token_free(tok2);
1985 isl_token_free(tok);
1986 isl_assert(s->ctx, !on_new_line, return NULL);
1987 isl_assert(s->ctx, n_row >= 0, return NULL);
1988 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1989 tok = isl_stream_next_token_on_same_line(s);
1990 if (tok) {
1991 if (tok->type != ISL_TOKEN_VALUE) {
1992 isl_stream_error(s, tok,
1993 "expecting number of output dimensions");
1994 isl_stream_push_token(s, tok);
1995 goto error;
1997 out = isl_int_get_si(tok->u.v);
1998 isl_token_free(tok);
2000 tok = isl_stream_next_token_on_same_line(s);
2001 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2002 isl_stream_error(s, tok,
2003 "expecting number of input dimensions");
2004 if (tok)
2005 isl_stream_push_token(s, tok);
2006 goto error;
2008 in = isl_int_get_si(tok->u.v);
2009 isl_token_free(tok);
2011 tok = isl_stream_next_token_on_same_line(s);
2012 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2013 isl_stream_error(s, tok,
2014 "expecting number of existentials");
2015 if (tok)
2016 isl_stream_push_token(s, tok);
2017 goto error;
2019 local = isl_int_get_si(tok->u.v);
2020 isl_token_free(tok);
2022 tok = isl_stream_next_token_on_same_line(s);
2023 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2024 isl_stream_error(s, tok,
2025 "expecting number of parameters");
2026 if (tok)
2027 isl_stream_push_token(s, tok);
2028 goto error;
2030 nparam = isl_int_get_si(tok->u.v);
2031 isl_token_free(tok);
2032 if (n_col != 1 + out + in + local + nparam + 1) {
2033 isl_stream_error(s, NULL,
2034 "dimensions don't match");
2035 goto error;
2037 } else
2038 out = n_col - 2 - nparam;
2039 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2040 if (!bmap)
2041 return NULL;
2043 for (i = 0; i < local; ++i) {
2044 int k = isl_basic_map_alloc_div(bmap);
2045 if (k < 0)
2046 goto error;
2047 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2050 for (i = 0; i < n_row; ++i)
2051 bmap = basic_map_read_polylib_constraint(s, bmap);
2053 tok = isl_stream_next_token_on_same_line(s);
2054 if (tok) {
2055 isl_stream_error(s, tok, "unexpected extra token on line");
2056 isl_stream_push_token(s, tok);
2057 goto error;
2060 bmap = isl_basic_map_simplify(bmap);
2061 bmap = isl_basic_map_finalize(bmap);
2062 return bmap;
2063 error:
2064 isl_basic_map_free(bmap);
2065 return NULL;
2068 static struct isl_map *map_read_polylib(__isl_keep isl_stream *s)
2070 struct isl_token *tok;
2071 struct isl_token *tok2;
2072 int i, n;
2073 struct isl_map *map;
2075 tok = isl_stream_next_token(s);
2076 if (!tok) {
2077 isl_stream_error(s, NULL, "unexpected EOF");
2078 return NULL;
2080 tok2 = isl_stream_next_token_on_same_line(s);
2081 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2082 isl_stream_push_token(s, tok2);
2083 isl_stream_push_token(s, tok);
2084 return isl_map_from_basic_map(basic_map_read_polylib(s));
2086 if (tok2) {
2087 isl_stream_error(s, tok2, "unexpected token");
2088 isl_stream_push_token(s, tok2);
2089 isl_stream_push_token(s, tok);
2090 return NULL;
2092 n = isl_int_get_si(tok->u.v);
2093 isl_token_free(tok);
2095 isl_assert(s->ctx, n >= 1, return NULL);
2097 map = isl_map_from_basic_map(basic_map_read_polylib(s));
2099 for (i = 1; map && i < n; ++i)
2100 map = isl_map_union(map,
2101 isl_map_from_basic_map(basic_map_read_polylib(s)));
2103 return map;
2106 static int optional_power(__isl_keep isl_stream *s)
2108 int pow;
2109 struct isl_token *tok;
2111 tok = isl_stream_next_token(s);
2112 if (!tok)
2113 return 1;
2114 if (tok->type != '^') {
2115 isl_stream_push_token(s, tok);
2116 return 1;
2118 isl_token_free(tok);
2119 tok = isl_stream_next_token(s);
2120 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2121 isl_stream_error(s, tok, "expecting exponent");
2122 if (tok)
2123 isl_stream_push_token(s, tok);
2124 return 1;
2126 pow = isl_int_get_si(tok->u.v);
2127 isl_token_free(tok);
2128 return pow;
2131 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2132 __isl_keep isl_map *map, struct vars *v);
2134 static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2135 __isl_keep isl_map *map, struct vars *v)
2137 isl_pw_qpolynomial *pwqp;
2138 struct isl_token *tok;
2140 tok = next_token(s);
2141 if (!tok) {
2142 isl_stream_error(s, NULL, "unexpected EOF");
2143 return NULL;
2145 if (tok->type == '(') {
2146 int pow;
2148 isl_token_free(tok);
2149 pwqp = read_term(s, map, v);
2150 if (!pwqp)
2151 return NULL;
2152 if (isl_stream_eat(s, ')'))
2153 goto error;
2154 pow = optional_power(s);
2155 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2156 } else if (tok->type == ISL_TOKEN_VALUE) {
2157 struct isl_token *tok2;
2158 isl_qpolynomial *qp;
2160 tok2 = isl_stream_next_token(s);
2161 if (tok2 && tok2->type == '/') {
2162 isl_token_free(tok2);
2163 tok2 = next_token(s);
2164 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2165 isl_stream_error(s, tok2, "expected denominator");
2166 isl_token_free(tok);
2167 isl_token_free(tok2);
2168 return NULL;
2170 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2171 tok->u.v, tok2->u.v);
2172 isl_token_free(tok2);
2173 } else {
2174 isl_stream_push_token(s, tok2);
2175 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2176 tok->u.v);
2178 isl_token_free(tok);
2179 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2180 } else if (tok->type == ISL_TOKEN_INFTY) {
2181 isl_qpolynomial *qp;
2182 isl_token_free(tok);
2183 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2184 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2185 } else if (tok->type == ISL_TOKEN_NAN) {
2186 isl_qpolynomial *qp;
2187 isl_token_free(tok);
2188 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2189 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2190 } else if (tok->type == ISL_TOKEN_IDENT) {
2191 int n = v->n;
2192 int pos = vars_pos(v, tok->u.s, -1);
2193 int pow;
2194 isl_qpolynomial *qp;
2195 if (pos < 0) {
2196 isl_token_free(tok);
2197 return NULL;
2199 if (pos >= n) {
2200 vars_drop(v, v->n - n);
2201 isl_stream_error(s, tok, "unknown identifier");
2202 isl_token_free(tok);
2203 return NULL;
2205 isl_token_free(tok);
2206 pow = optional_power(s);
2207 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2208 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2209 } else if (is_start_of_div(tok)) {
2210 isl_pw_aff *pwaff;
2211 int pow;
2213 isl_stream_push_token(s, tok);
2214 pwaff = accept_div(s, isl_map_get_space(map), v);
2215 pow = optional_power(s);
2216 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2217 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2218 } else if (tok->type == '-') {
2219 isl_token_free(tok);
2220 pwqp = read_factor(s, map, v);
2221 pwqp = isl_pw_qpolynomial_neg(pwqp);
2222 } else {
2223 isl_stream_error(s, tok, "unexpected isl_token");
2224 isl_stream_push_token(s, tok);
2225 return NULL;
2228 if (isl_stream_eat_if_available(s, '*') ||
2229 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
2230 isl_pw_qpolynomial *pwqp2;
2232 pwqp2 = read_factor(s, map, v);
2233 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2236 return pwqp;
2237 error:
2238 isl_pw_qpolynomial_free(pwqp);
2239 return NULL;
2242 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2243 __isl_keep isl_map *map, struct vars *v)
2245 struct isl_token *tok;
2246 isl_pw_qpolynomial *pwqp;
2248 pwqp = read_factor(s, map, v);
2250 for (;;) {
2251 tok = next_token(s);
2252 if (!tok)
2253 return pwqp;
2255 if (tok->type == '+') {
2256 isl_pw_qpolynomial *pwqp2;
2258 isl_token_free(tok);
2259 pwqp2 = read_factor(s, map, v);
2260 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2261 } else if (tok->type == '-') {
2262 isl_pw_qpolynomial *pwqp2;
2264 isl_token_free(tok);
2265 pwqp2 = read_factor(s, map, v);
2266 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2267 } else if (tok->type == ISL_TOKEN_VALUE &&
2268 isl_int_is_neg(tok->u.v)) {
2269 isl_pw_qpolynomial *pwqp2;
2271 isl_stream_push_token(s, tok);
2272 pwqp2 = read_factor(s, map, v);
2273 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2274 } else {
2275 isl_stream_push_token(s, tok);
2276 break;
2280 return pwqp;
2283 static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2284 __isl_take isl_map *map, struct vars *v, int rational)
2286 struct isl_token *tok;
2288 tok = isl_stream_next_token(s);
2289 if (!tok) {
2290 isl_stream_error(s, NULL, "unexpected EOF");
2291 goto error;
2293 if (tok->type == ':' ||
2294 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
2295 isl_token_free(tok);
2296 map = read_formula(s, v, map, rational);
2297 } else
2298 isl_stream_push_token(s, tok);
2300 return map;
2301 error:
2302 isl_map_free(map);
2303 return NULL;
2306 static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2307 __isl_take isl_map *map, struct vars *v, int n)
2309 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2310 isl_pw_qpolynomial *pwqp;
2311 struct isl_set *set;
2313 pwqp = read_term(s, map, v);
2314 map = read_optional_formula(s, map, v, 0);
2315 set = isl_map_range(map);
2317 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2319 vars_drop(v, v->n - n);
2321 obj.v = pwqp;
2322 return obj;
2325 static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2326 __isl_take isl_set *set, struct vars *v, int n)
2328 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2329 isl_pw_qpolynomial *pwqp;
2330 isl_pw_qpolynomial_fold *pwf = NULL;
2332 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
2333 return obj_read_poly(s, set, v, n);
2335 if (isl_stream_eat(s, '('))
2336 goto error;
2338 pwqp = read_term(s, set, v);
2339 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
2341 while (isl_stream_eat_if_available(s, ',')) {
2342 isl_pw_qpolynomial_fold *pwf_i;
2343 pwqp = read_term(s, set, v);
2344 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
2345 pwqp);
2346 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2349 if (isl_stream_eat(s, ')'))
2350 goto error;
2352 set = read_optional_formula(s, set, v, 0);
2353 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2355 vars_drop(v, v->n - n);
2357 obj.v = pwf;
2358 return obj;
2359 error:
2360 isl_set_free(set);
2361 isl_pw_qpolynomial_fold_free(pwf);
2362 obj.type = isl_obj_none;
2363 return obj;
2366 static int is_rational(__isl_keep isl_stream *s)
2368 struct isl_token *tok;
2370 tok = isl_stream_next_token(s);
2371 if (!tok)
2372 return 0;
2373 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
2374 isl_token_free(tok);
2375 isl_stream_eat(s, ':');
2376 return 1;
2379 isl_stream_push_token(s, tok);
2381 return 0;
2384 static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2385 __isl_take isl_map *map, struct vars *v)
2387 struct isl_token *tok;
2388 struct isl_obj obj = { isl_obj_set, NULL };
2389 int n = v->n;
2390 int rational;
2392 rational = is_rational(s);
2393 if (rational)
2394 map = isl_map_set_rational(map);
2396 if (isl_stream_next_token_is(s, ':')) {
2397 obj.type = isl_obj_set;
2398 obj.v = read_optional_formula(s, map, v, rational);
2399 return obj;
2402 if (!next_is_tuple(s))
2403 return obj_read_poly_or_fold(s, map, v, n);
2405 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2406 if (!map)
2407 goto error;
2408 tok = isl_stream_next_token(s);
2409 if (!tok)
2410 goto error;
2411 if (tok->type == ISL_TOKEN_TO) {
2412 obj.type = isl_obj_map;
2413 isl_token_free(tok);
2414 if (!next_is_tuple(s)) {
2415 isl_set *set = isl_map_domain(map);
2416 return obj_read_poly_or_fold(s, set, v, n);
2418 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2419 if (!map)
2420 goto error;
2421 } else {
2422 map = isl_map_domain(map);
2423 isl_stream_push_token(s, tok);
2426 map = read_optional_formula(s, map, v, rational);
2428 vars_drop(v, v->n - n);
2430 obj.v = map;
2431 return obj;
2432 error:
2433 isl_map_free(map);
2434 obj.type = isl_obj_none;
2435 return obj;
2438 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2440 if (obj.type == isl_obj_map) {
2441 obj.v = isl_union_map_from_map(obj.v);
2442 obj.type = isl_obj_union_map;
2443 } else if (obj.type == isl_obj_set) {
2444 obj.v = isl_union_set_from_set(obj.v);
2445 obj.type = isl_obj_union_set;
2446 } else if (obj.type == isl_obj_pw_qpolynomial) {
2447 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2448 obj.type = isl_obj_union_pw_qpolynomial;
2449 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2450 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2451 obj.type = isl_obj_union_pw_qpolynomial_fold;
2452 } else
2453 isl_assert(ctx, 0, goto error);
2454 return obj;
2455 error:
2456 obj.type->free(obj.v);
2457 obj.type = isl_obj_none;
2458 return obj;
2461 static struct isl_obj obj_add(__isl_keep isl_stream *s,
2462 struct isl_obj obj1, struct isl_obj obj2)
2464 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2465 obj1 = to_union(s->ctx, obj1);
2466 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2467 obj2 = to_union(s->ctx, obj2);
2468 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2469 obj1 = to_union(s->ctx, obj1);
2470 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2471 obj2 = to_union(s->ctx, obj2);
2472 if (obj1.type == isl_obj_pw_qpolynomial &&
2473 obj2.type == isl_obj_union_pw_qpolynomial)
2474 obj1 = to_union(s->ctx, obj1);
2475 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2476 obj2.type == isl_obj_pw_qpolynomial)
2477 obj2 = to_union(s->ctx, obj2);
2478 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2479 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2480 obj1 = to_union(s->ctx, obj1);
2481 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2482 obj2.type == isl_obj_pw_qpolynomial_fold)
2483 obj2 = to_union(s->ctx, obj2);
2484 if (obj1.type != obj2.type) {
2485 isl_stream_error(s, NULL,
2486 "attempt to combine incompatible objects");
2487 goto error;
2489 if (!obj1.type->add)
2490 isl_die(s->ctx, isl_error_internal,
2491 "combination not supported on object type", goto error);
2492 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2493 obj1 = to_union(s->ctx, obj1);
2494 obj2 = to_union(s->ctx, obj2);
2496 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2497 obj1 = to_union(s->ctx, obj1);
2498 obj2 = to_union(s->ctx, obj2);
2500 if (obj1.type == isl_obj_pw_qpolynomial &&
2501 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2502 obj1 = to_union(s->ctx, obj1);
2503 obj2 = to_union(s->ctx, obj2);
2505 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2506 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2507 obj1 = to_union(s->ctx, obj1);
2508 obj2 = to_union(s->ctx, obj2);
2510 obj1.v = obj1.type->add(obj1.v, obj2.v);
2511 return obj1;
2512 error:
2513 obj1.type->free(obj1.v);
2514 obj2.type->free(obj2.v);
2515 obj1.type = isl_obj_none;
2516 obj1.v = NULL;
2517 return obj1;
2520 /* Are the first two tokens on "s", "domain" (either as a string
2521 * or as an identifier) followed by ":"?
2523 static int next_is_domain_colon(__isl_keep isl_stream *s)
2525 struct isl_token *tok;
2526 char *name;
2527 int res;
2529 tok = isl_stream_next_token(s);
2530 if (!tok)
2531 return 0;
2532 if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
2533 isl_stream_push_token(s, tok);
2534 return 0;
2537 name = isl_token_get_str(s->ctx, tok);
2538 res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2539 free(name);
2541 isl_stream_push_token(s, tok);
2543 return res;
2546 /* Do the first tokens on "s" look like a schedule?
2548 * The root of a schedule is always a domain node, so the first thing
2549 * we expect in the stream is a domain key, i.e., "domain" followed
2550 * by ":". If the schedule was printed in YAML flow style, then
2551 * we additionally expect a "{" to open the outer mapping.
2553 static int next_is_schedule(__isl_keep isl_stream *s)
2555 struct isl_token *tok;
2556 int is_schedule;
2558 tok = isl_stream_next_token(s);
2559 if (!tok)
2560 return 0;
2561 if (tok->type != '{') {
2562 isl_stream_push_token(s, tok);
2563 return next_is_domain_colon(s);
2566 is_schedule = next_is_domain_colon(s);
2567 isl_stream_push_token(s, tok);
2569 return is_schedule;
2572 /* Read an isl_schedule from "s" and store it in an isl_obj.
2574 static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2576 struct isl_obj obj;
2578 obj.type = isl_obj_schedule;
2579 obj.v = isl_stream_read_schedule(s);
2581 return obj;
2584 /* Read a disjunction of object bodies from "s".
2585 * That is, read the inside of the braces, but not the braces themselves.
2586 * "v" contains a description of the identifiers parsed so far.
2587 * "map" contains information about the parameters.
2589 static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
2590 struct vars *v, __isl_keep isl_map *map)
2592 struct isl_obj obj = { isl_obj_set, NULL };
2594 if (isl_stream_next_token_is(s, '}')) {
2595 obj.type = isl_obj_union_set;
2596 obj.v = isl_union_set_empty(isl_map_get_space(map));
2597 return obj;
2600 for (;;) {
2601 struct isl_obj o;
2602 o = obj_read_body(s, isl_map_copy(map), v);
2603 if (o.type == isl_obj_none || !o.v)
2604 return o;
2605 if (!obj.v)
2606 obj = o;
2607 else {
2608 obj = obj_add(s, obj, o);
2609 if (obj.type == isl_obj_none || !obj.v)
2610 return obj;
2612 if (!isl_stream_eat_if_available(s, ';'))
2613 break;
2614 if (isl_stream_next_token_is(s, '}'))
2615 break;
2618 return obj;
2621 static struct isl_obj obj_read(__isl_keep isl_stream *s)
2623 isl_map *map = NULL;
2624 struct isl_token *tok;
2625 struct vars *v = NULL;
2626 struct isl_obj obj = { isl_obj_set, NULL };
2628 if (next_is_schedule(s))
2629 return schedule_read(s);
2631 tok = next_token(s);
2632 if (!tok) {
2633 isl_stream_error(s, NULL, "unexpected EOF");
2634 goto error;
2636 if (tok->type == ISL_TOKEN_VALUE) {
2637 struct isl_token *tok2;
2638 struct isl_map *map;
2640 tok2 = isl_stream_next_token(s);
2641 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2642 isl_int_is_neg(tok2->u.v)) {
2643 if (tok2)
2644 isl_stream_push_token(s, tok2);
2645 obj.type = isl_obj_val;
2646 obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2647 isl_token_free(tok);
2648 return obj;
2650 isl_stream_push_token(s, tok2);
2651 isl_stream_push_token(s, tok);
2652 map = map_read_polylib(s);
2653 if (!map)
2654 goto error;
2655 if (isl_map_may_be_set(map))
2656 obj.v = isl_map_range(map);
2657 else {
2658 obj.type = isl_obj_map;
2659 obj.v = map;
2661 return obj;
2663 v = vars_new(s->ctx);
2664 if (!v) {
2665 isl_stream_push_token(s, tok);
2666 goto error;
2668 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2669 if (tok->type == '[') {
2670 isl_stream_push_token(s, tok);
2671 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2672 if (!map)
2673 goto error;
2674 tok = isl_stream_next_token(s);
2675 if (!tok || tok->type != ISL_TOKEN_TO) {
2676 isl_stream_error(s, tok, "expecting '->'");
2677 if (tok)
2678 isl_stream_push_token(s, tok);
2679 goto error;
2681 isl_token_free(tok);
2682 tok = isl_stream_next_token(s);
2684 if (!tok || tok->type != '{') {
2685 isl_stream_error(s, tok, "expecting '{'");
2686 if (tok)
2687 isl_stream_push_token(s, tok);
2688 goto error;
2690 isl_token_free(tok);
2692 tok = isl_stream_next_token(s);
2693 if (!tok)
2695 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2696 isl_token_free(tok);
2697 if (isl_stream_eat(s, '='))
2698 goto error;
2699 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2700 if (!map)
2701 goto error;
2702 } else
2703 isl_stream_push_token(s, tok);
2705 obj = obj_read_disjuncts(s, v, map);
2706 if (obj.type == isl_obj_none || !obj.v)
2707 goto error;
2709 tok = isl_stream_next_token(s);
2710 if (tok && tok->type == '}') {
2711 isl_token_free(tok);
2712 } else {
2713 isl_stream_error(s, tok, "unexpected isl_token");
2714 if (tok)
2715 isl_token_free(tok);
2716 goto error;
2719 vars_free(v);
2720 isl_map_free(map);
2722 return obj;
2723 error:
2724 isl_map_free(map);
2725 obj.type->free(obj.v);
2726 if (v)
2727 vars_free(v);
2728 obj.v = NULL;
2729 return obj;
2732 struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2734 return obj_read(s);
2737 __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2739 struct isl_obj obj;
2741 obj = obj_read(s);
2742 if (obj.v)
2743 isl_assert(s->ctx, obj.type == isl_obj_map ||
2744 obj.type == isl_obj_set, goto error);
2746 if (obj.type == isl_obj_set)
2747 obj.v = isl_map_from_range(obj.v);
2749 return obj.v;
2750 error:
2751 obj.type->free(obj.v);
2752 return NULL;
2755 __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2757 struct isl_obj obj;
2759 obj = obj_read(s);
2760 if (obj.v) {
2761 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2762 obj.v = isl_map_range(obj.v);
2763 obj.type = isl_obj_set;
2765 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2768 return obj.v;
2769 error:
2770 obj.type->free(obj.v);
2771 return NULL;
2774 __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2776 struct isl_obj obj;
2778 obj = obj_read(s);
2779 if (obj.type == isl_obj_map) {
2780 obj.type = isl_obj_union_map;
2781 obj.v = isl_union_map_from_map(obj.v);
2783 if (obj.type == isl_obj_set) {
2784 obj.type = isl_obj_union_set;
2785 obj.v = isl_union_set_from_set(obj.v);
2787 if (obj.v && obj.type == isl_obj_union_set &&
2788 isl_union_set_is_empty(obj.v))
2789 obj.type = isl_obj_union_map;
2790 if (obj.v && obj.type != isl_obj_union_map)
2791 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2793 return obj.v;
2794 error:
2795 obj.type->free(obj.v);
2796 return NULL;
2799 /* Extract an isl_union_set from "obj".
2800 * This only works if the object was detected as either a set
2801 * (in which case it is converted to a union set) or a union set.
2803 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
2804 struct isl_obj obj)
2806 if (obj.type == isl_obj_set) {
2807 obj.type = isl_obj_union_set;
2808 obj.v = isl_union_set_from_set(obj.v);
2810 if (obj.v)
2811 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
2813 return obj.v;
2814 error:
2815 obj.type->free(obj.v);
2816 return NULL;
2819 /* Read an isl_union_set from "s".
2820 * First read a generic object and then try and extract
2821 * an isl_union_set from that.
2823 __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2825 struct isl_obj obj;
2827 obj = obj_read(s);
2828 return extract_union_set(s->ctx, obj);
2831 static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2833 struct isl_obj obj;
2834 struct isl_map *map;
2835 struct isl_basic_map *bmap;
2837 obj = obj_read(s);
2838 if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
2839 isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
2840 goto error);
2841 map = obj.v;
2842 if (!map)
2843 return NULL;
2845 if (map->n > 1)
2846 isl_die(s->ctx, isl_error_invalid,
2847 "set or map description involves "
2848 "more than one disjunct", goto error);
2850 if (map->n == 0)
2851 bmap = isl_basic_map_empty(isl_map_get_space(map));
2852 else
2853 bmap = isl_basic_map_copy(map->p[0]);
2855 isl_map_free(map);
2857 return bmap;
2858 error:
2859 obj.type->free(obj.v);
2860 return NULL;
2863 static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2865 isl_basic_map *bmap;
2866 bmap = basic_map_read(s);
2867 if (!bmap)
2868 return NULL;
2869 if (!isl_basic_map_may_be_set(bmap))
2870 isl_die(s->ctx, isl_error_invalid,
2871 "input is not a set", goto error);
2872 return isl_basic_map_range(bmap);
2873 error:
2874 isl_basic_map_free(bmap);
2875 return NULL;
2878 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2879 FILE *input)
2881 struct isl_basic_map *bmap;
2882 isl_stream *s = isl_stream_new_file(ctx, input);
2883 if (!s)
2884 return NULL;
2885 bmap = basic_map_read(s);
2886 isl_stream_free(s);
2887 return bmap;
2890 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2891 FILE *input)
2893 isl_basic_set *bset;
2894 isl_stream *s = isl_stream_new_file(ctx, input);
2895 if (!s)
2896 return NULL;
2897 bset = basic_set_read(s);
2898 isl_stream_free(s);
2899 return bset;
2902 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2903 const char *str)
2905 struct isl_basic_map *bmap;
2906 isl_stream *s = isl_stream_new_str(ctx, str);
2907 if (!s)
2908 return NULL;
2909 bmap = basic_map_read(s);
2910 isl_stream_free(s);
2911 return bmap;
2914 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2915 const char *str)
2917 isl_basic_set *bset;
2918 isl_stream *s = isl_stream_new_str(ctx, str);
2919 if (!s)
2920 return NULL;
2921 bset = basic_set_read(s);
2922 isl_stream_free(s);
2923 return bset;
2926 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2927 FILE *input)
2929 struct isl_map *map;
2930 isl_stream *s = isl_stream_new_file(ctx, input);
2931 if (!s)
2932 return NULL;
2933 map = isl_stream_read_map(s);
2934 isl_stream_free(s);
2935 return map;
2938 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2939 const char *str)
2941 struct isl_map *map;
2942 isl_stream *s = isl_stream_new_str(ctx, str);
2943 if (!s)
2944 return NULL;
2945 map = isl_stream_read_map(s);
2946 isl_stream_free(s);
2947 return map;
2950 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2951 FILE *input)
2953 isl_set *set;
2954 isl_stream *s = isl_stream_new_file(ctx, input);
2955 if (!s)
2956 return NULL;
2957 set = isl_stream_read_set(s);
2958 isl_stream_free(s);
2959 return set;
2962 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2963 const char *str)
2965 isl_set *set;
2966 isl_stream *s = isl_stream_new_str(ctx, str);
2967 if (!s)
2968 return NULL;
2969 set = isl_stream_read_set(s);
2970 isl_stream_free(s);
2971 return set;
2974 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2975 FILE *input)
2977 isl_union_map *umap;
2978 isl_stream *s = isl_stream_new_file(ctx, input);
2979 if (!s)
2980 return NULL;
2981 umap = isl_stream_read_union_map(s);
2982 isl_stream_free(s);
2983 return umap;
2986 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2987 const char *str)
2989 isl_union_map *umap;
2990 isl_stream *s = isl_stream_new_str(ctx, str);
2991 if (!s)
2992 return NULL;
2993 umap = isl_stream_read_union_map(s);
2994 isl_stream_free(s);
2995 return umap;
2998 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2999 FILE *input)
3001 isl_union_set *uset;
3002 isl_stream *s = isl_stream_new_file(ctx, input);
3003 if (!s)
3004 return NULL;
3005 uset = isl_stream_read_union_set(s);
3006 isl_stream_free(s);
3007 return uset;
3010 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3011 const char *str)
3013 isl_union_set *uset;
3014 isl_stream *s = isl_stream_new_str(ctx, str);
3015 if (!s)
3016 return NULL;
3017 uset = isl_stream_read_union_set(s);
3018 isl_stream_free(s);
3019 return uset;
3022 static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3024 struct isl_vec *vec = NULL;
3025 struct isl_token *tok;
3026 unsigned size;
3027 int j;
3029 tok = isl_stream_next_token(s);
3030 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3031 isl_stream_error(s, tok, "expecting vector length");
3032 goto error;
3035 size = isl_int_get_si(tok->u.v);
3036 isl_token_free(tok);
3038 vec = isl_vec_alloc(s->ctx, size);
3040 for (j = 0; j < size; ++j) {
3041 tok = isl_stream_next_token(s);
3042 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3043 isl_stream_error(s, tok, "expecting constant value");
3044 goto error;
3046 isl_int_set(vec->el[j], tok->u.v);
3047 isl_token_free(tok);
3050 return vec;
3051 error:
3052 isl_token_free(tok);
3053 isl_vec_free(vec);
3054 return NULL;
3057 static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3059 return isl_vec_read_polylib(s);
3062 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3064 isl_vec *v;
3065 isl_stream *s = isl_stream_new_file(ctx, input);
3066 if (!s)
3067 return NULL;
3068 v = vec_read(s);
3069 isl_stream_free(s);
3070 return v;
3073 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3074 __isl_keep isl_stream *s)
3076 struct isl_obj obj;
3078 obj = obj_read(s);
3079 if (obj.v)
3080 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3081 goto error);
3083 return obj.v;
3084 error:
3085 obj.type->free(obj.v);
3086 return NULL;
3089 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3090 const char *str)
3092 isl_pw_qpolynomial *pwqp;
3093 isl_stream *s = isl_stream_new_str(ctx, str);
3094 if (!s)
3095 return NULL;
3096 pwqp = isl_stream_read_pw_qpolynomial(s);
3097 isl_stream_free(s);
3098 return pwqp;
3101 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3102 FILE *input)
3104 isl_pw_qpolynomial *pwqp;
3105 isl_stream *s = isl_stream_new_file(ctx, input);
3106 if (!s)
3107 return NULL;
3108 pwqp = isl_stream_read_pw_qpolynomial(s);
3109 isl_stream_free(s);
3110 return pwqp;
3113 /* Is the next token an identifer not in "v"?
3115 static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3117 int n = v->n;
3118 int fresh;
3119 struct isl_token *tok;
3121 tok = isl_stream_next_token(s);
3122 if (!tok)
3123 return 0;
3124 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
3125 isl_stream_push_token(s, tok);
3127 vars_drop(v, v->n - n);
3129 return fresh;
3132 /* First read the domain of the affine expression, which may be
3133 * a parameter space or a set.
3134 * The tricky part is that we don't know if the domain is a set or not,
3135 * so when we are trying to read the domain, we may actually be reading
3136 * the affine expression itself (defined on a parameter domains)
3137 * If the tuple we are reading is named, we assume it's the domain.
3138 * Also, if inside the tuple, the first thing we find is a nested tuple
3139 * or a new identifier, we again assume it's the domain.
3140 * Finally, if the tuple is empty, then it must be the domain
3141 * since it does not contain an affine expression.
3142 * Otherwise, we assume we are reading an affine expression.
3144 static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3145 __isl_take isl_set *dom, struct vars *v)
3147 struct isl_token *tok, *tok2;
3148 int is_empty;
3150 tok = isl_stream_next_token(s);
3151 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
3152 isl_stream_push_token(s, tok);
3153 return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3155 if (!tok || tok->type != '[') {
3156 isl_stream_error(s, tok, "expecting '['");
3157 goto error;
3159 tok2 = isl_stream_next_token(s);
3160 is_empty = tok2 && tok2->type == ']';
3161 if (tok2)
3162 isl_stream_push_token(s, tok2);
3163 if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
3164 isl_stream_push_token(s, tok);
3165 dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3166 } else
3167 isl_stream_push_token(s, tok);
3169 return dom;
3170 error:
3171 if (tok)
3172 isl_stream_push_token(s, tok);
3173 isl_set_free(dom);
3174 return NULL;
3177 /* Read an affine expression from "s".
3179 __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3181 isl_aff *aff;
3182 isl_multi_aff *ma;
3184 ma = isl_stream_read_multi_aff(s);
3185 if (!ma)
3186 return NULL;
3187 if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
3188 isl_die(s->ctx, isl_error_invalid,
3189 "expecting single affine expression",
3190 goto error);
3192 aff = isl_multi_aff_get_aff(ma, 0);
3193 isl_multi_aff_free(ma);
3194 return aff;
3195 error:
3196 isl_multi_aff_free(ma);
3197 return NULL;
3200 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3202 static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3203 __isl_take isl_set *dom, struct vars *v)
3205 isl_pw_aff *pwaff = NULL;
3207 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
3208 goto error;
3210 if (isl_stream_eat(s, '['))
3211 goto error;
3213 pwaff = accept_affine(s, isl_set_get_space(dom), v);
3215 if (isl_stream_eat(s, ']'))
3216 goto error;
3218 dom = read_optional_formula(s, dom, v, 0);
3219 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3221 return pwaff;
3222 error:
3223 isl_set_free(dom);
3224 isl_pw_aff_free(pwaff);
3225 return NULL;
3228 __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3230 struct vars *v;
3231 isl_set *dom = NULL;
3232 isl_set *aff_dom;
3233 isl_pw_aff *pa = NULL;
3234 int n;
3236 v = vars_new(s->ctx);
3237 if (!v)
3238 return NULL;
3240 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3241 if (next_is_tuple(s)) {
3242 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3243 if (isl_stream_eat(s, ISL_TOKEN_TO))
3244 goto error;
3246 if (isl_stream_eat(s, '{'))
3247 goto error;
3249 n = v->n;
3250 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3251 pa = read_pw_aff_with_dom(s, aff_dom, v);
3252 vars_drop(v, v->n - n);
3254 while (isl_stream_eat_if_available(s, ';')) {
3255 isl_pw_aff *pa_i;
3257 n = v->n;
3258 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3259 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3260 vars_drop(v, v->n - n);
3262 pa = isl_pw_aff_union_add(pa, pa_i);
3265 if (isl_stream_eat(s, '}'))
3266 goto error;
3268 vars_free(v);
3269 isl_set_free(dom);
3270 return pa;
3271 error:
3272 vars_free(v);
3273 isl_set_free(dom);
3274 isl_pw_aff_free(pa);
3275 return NULL;
3278 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3280 isl_aff *aff;
3281 isl_stream *s = isl_stream_new_str(ctx, str);
3282 if (!s)
3283 return NULL;
3284 aff = isl_stream_read_aff(s);
3285 isl_stream_free(s);
3286 return aff;
3289 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3291 isl_pw_aff *pa;
3292 isl_stream *s = isl_stream_new_str(ctx, str);
3293 if (!s)
3294 return NULL;
3295 pa = isl_stream_read_pw_aff(s);
3296 isl_stream_free(s);
3297 return pa;
3300 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3301 * from a tuple "tuple" read by read_tuple.
3303 * Note that the function read_tuple accepts tuples where some output or
3304 * set dimensions are defined in terms of other output or set dimensions
3305 * since this function is also used to read maps. As a special case,
3306 * read_tuple also accept dimensions that are defined in terms of themselves
3307 * (i.e., that are not defined).
3308 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3309 * that the definitions of the output/set dimensions do not involve any
3310 * output/set dimensions.
3311 * Finally, drop the output dimensions from the domain of the result
3312 * of read_tuple (which is of the form [input, output] -> [output],
3313 * with anonymous domain) and reset the space.
3315 static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3316 __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3318 int dim, i, n;
3319 isl_space *space;
3320 isl_multi_pw_aff *mpa;
3322 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3323 dim = isl_space_dim(dom_space, isl_dim_all);
3324 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3325 space = isl_space_align_params(space, isl_space_copy(dom_space));
3326 if (!isl_space_is_params(dom_space))
3327 space = isl_space_map_from_domain_and_range(
3328 isl_space_copy(dom_space), space);
3329 isl_space_free(dom_space);
3330 mpa = isl_multi_pw_aff_alloc(space);
3332 for (i = 0; i < n; ++i) {
3333 isl_pw_aff *pa;
3334 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3335 if (!pa)
3336 return isl_multi_pw_aff_free(mpa);
3337 if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3338 isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3339 isl_pw_aff_free(pa);
3340 isl_die(ctx, isl_error_invalid,
3341 "not an affine expression",
3342 return isl_multi_pw_aff_free(mpa));
3344 pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3345 space = isl_multi_pw_aff_get_domain_space(mpa);
3346 pa = isl_pw_aff_reset_domain_space(pa, space);
3347 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3350 return mpa;
3353 /* Read a tuple of affine expressions, together with optional constraints
3354 * on the domain from "s". "dom" represents the initial constraints
3355 * on the domain.
3357 * The isl_multi_aff may live in either a set or a map space.
3358 * First read the first tuple and check if it is followed by a "->".
3359 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3360 * read in the next tuple. This tuple (or the first tuple if it was
3361 * not followed by a "->") is then converted into an isl_multi_pw_aff
3362 * through a call to extract_mpa_from_tuple.
3363 * The result is converted to an isl_pw_multi_aff and
3364 * its domain is intersected with the domain.
3366 static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3367 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3369 isl_multi_pw_aff *tuple;
3370 isl_multi_pw_aff *mpa;
3371 isl_pw_multi_aff *pma;
3372 int n = v->n;
3374 tuple = read_tuple(s, v, 0, 0);
3375 if (!tuple)
3376 goto error;
3377 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3378 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3379 dom = isl_map_domain(map);
3380 tuple = read_tuple(s, v, 0, 0);
3381 if (!tuple)
3382 goto error;
3385 dom = read_optional_formula(s, dom, v, 0);
3387 vars_drop(v, v->n - n);
3389 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3390 isl_multi_pw_aff_free(tuple);
3391 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3392 pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3394 return pma;
3395 error:
3396 isl_set_free(dom);
3397 return NULL;
3400 /* Read an isl_pw_multi_aff from "s".
3402 * In particular, first read the parameters and then read a sequence
3403 * of one or more tuples of affine expressions with optional conditions and
3404 * add them up.
3406 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3407 __isl_keep isl_stream *s)
3409 struct vars *v;
3410 isl_set *dom;
3411 isl_pw_multi_aff *pma = NULL;
3413 v = vars_new(s->ctx);
3414 if (!v)
3415 return NULL;
3417 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3418 if (next_is_tuple(s)) {
3419 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3420 if (isl_stream_eat(s, ISL_TOKEN_TO))
3421 goto error;
3423 if (isl_stream_eat(s, '{'))
3424 goto error;
3426 pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3428 while (isl_stream_eat_if_available(s, ';')) {
3429 isl_pw_multi_aff *pma2;
3431 pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3432 pma = isl_pw_multi_aff_union_add(pma, pma2);
3433 if (!pma)
3434 goto error;
3437 if (isl_stream_eat(s, '}'))
3438 goto error;
3440 isl_set_free(dom);
3441 vars_free(v);
3442 return pma;
3443 error:
3444 isl_pw_multi_aff_free(pma);
3445 isl_set_free(dom);
3446 vars_free(v);
3447 return NULL;
3450 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3451 const char *str)
3453 isl_pw_multi_aff *pma;
3454 isl_stream *s = isl_stream_new_str(ctx, str);
3455 if (!s)
3456 return NULL;
3457 pma = isl_stream_read_pw_multi_aff(s);
3458 isl_stream_free(s);
3459 return pma;
3462 /* Read an isl_union_pw_multi_aff from "s".
3463 * We currently read a generic object and if it turns out to be a set or
3464 * a map, we convert that to an isl_union_pw_multi_aff.
3465 * It would be more efficient if we were to construct
3466 * the isl_union_pw_multi_aff directly.
3468 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3469 __isl_keep isl_stream *s)
3471 struct isl_obj obj;
3473 obj = obj_read(s);
3474 if (!obj.v)
3475 return NULL;
3477 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
3478 obj = to_union(s->ctx, obj);
3479 if (obj.type == isl_obj_union_map)
3480 return isl_union_pw_multi_aff_from_union_map(obj.v);
3481 if (obj.type == isl_obj_union_set)
3482 return isl_union_pw_multi_aff_from_union_set(obj.v);
3484 obj.type->free(obj.v);
3485 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3486 return NULL);
3489 /* Read an isl_union_pw_multi_aff from "str".
3491 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3492 isl_ctx *ctx, const char *str)
3494 isl_union_pw_multi_aff *upma;
3495 isl_stream *s = isl_stream_new_str(ctx, str);
3496 if (!s)
3497 return NULL;
3498 upma = isl_stream_read_union_pw_multi_aff(s);
3499 isl_stream_free(s);
3500 return upma;
3503 /* Assuming "pa" represents a single affine expression defined on a universe
3504 * domain, extract this affine expression.
3506 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3508 isl_aff *aff;
3510 if (!pa)
3511 return NULL;
3512 if (pa->n != 1)
3513 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3514 "expecting single affine expression",
3515 goto error);
3516 if (!isl_set_plain_is_universe(pa->p[0].set))
3517 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3518 "expecting universe domain",
3519 goto error);
3521 aff = isl_aff_copy(pa->p[0].aff);
3522 isl_pw_aff_free(pa);
3523 return aff;
3524 error:
3525 isl_pw_aff_free(pa);
3526 return NULL;
3529 /* This function is called for each element in a tuple inside
3530 * isl_stream_read_multi_val.
3531 * Read an isl_val from "s" and add it to *list.
3533 static __isl_give isl_space *read_val_el(__isl_keep isl_stream *s,
3534 struct vars *v, __isl_take isl_space *space, int rational, void *user)
3536 isl_val_list **list = (isl_val_list **) user;
3537 isl_val *val;
3539 val = isl_stream_read_val(s);
3540 *list = isl_val_list_add(*list, val);
3541 if (!*list)
3542 return isl_space_free(space);
3544 return space;
3547 /* Read an isl_multi_val from "s".
3549 * We first read a tuple space, collecting the element values in a list.
3550 * Then we create an isl_multi_val from the space and the isl_val_list.
3552 __isl_give isl_multi_val *isl_stream_read_multi_val(__isl_keep isl_stream *s)
3554 struct vars *v;
3555 isl_set *dom = NULL;
3556 isl_space *space;
3557 isl_multi_val *mv = NULL;
3558 isl_val_list *list;
3560 v = vars_new(s->ctx);
3561 if (!v)
3562 return NULL;
3564 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3565 if (next_is_tuple(s)) {
3566 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3567 if (isl_stream_eat(s, ISL_TOKEN_TO))
3568 goto error;
3570 if (!isl_set_plain_is_universe(dom))
3571 isl_die(s->ctx, isl_error_invalid,
3572 "expecting universe parameter domain", goto error);
3573 if (isl_stream_eat(s, '{'))
3574 goto error;
3576 space = isl_set_get_space(dom);
3578 list = isl_val_list_alloc(s->ctx, 0);
3579 space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list);
3580 mv = isl_multi_val_from_val_list(space, list);
3582 if (isl_stream_eat(s, '}'))
3583 goto error;
3585 vars_free(v);
3586 isl_set_free(dom);
3587 return mv;
3588 error:
3589 vars_free(v);
3590 isl_set_free(dom);
3591 isl_multi_val_free(mv);
3592 return NULL;
3595 /* Read an isl_multi_val from "str".
3597 __isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx,
3598 const char *str)
3600 isl_multi_val *mv;
3601 isl_stream *s = isl_stream_new_str(ctx, str);
3602 if (!s)
3603 return NULL;
3604 mv = isl_stream_read_multi_val(s);
3605 isl_stream_free(s);
3606 return mv;
3609 /* Read a multi-affine expression from "s".
3610 * If the multi-affine expression has a domain, then the tuple
3611 * representing this domain cannot involve any affine expressions.
3612 * The tuple representing the actual expressions needs to consist
3613 * of only affine expressions. Moreover, these expressions can
3614 * only depend on parameters and input dimensions and not on other
3615 * output dimensions.
3617 __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3619 struct vars *v;
3620 isl_set *dom = NULL;
3621 isl_multi_pw_aff *tuple = NULL;
3622 int dim, i, n;
3623 isl_space *space, *dom_space;
3624 isl_multi_aff *ma = NULL;
3626 v = vars_new(s->ctx);
3627 if (!v)
3628 return NULL;
3630 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3631 if (next_is_tuple(s)) {
3632 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3633 if (isl_stream_eat(s, ISL_TOKEN_TO))
3634 goto error;
3636 if (!isl_set_plain_is_universe(dom))
3637 isl_die(s->ctx, isl_error_invalid,
3638 "expecting universe parameter domain", goto error);
3639 if (isl_stream_eat(s, '{'))
3640 goto error;
3642 tuple = read_tuple(s, v, 0, 0);
3643 if (!tuple)
3644 goto error;
3645 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3646 isl_set *set;
3647 isl_space *space;
3648 int has_expr;
3650 has_expr = tuple_has_expr(tuple);
3651 if (has_expr < 0)
3652 goto error;
3653 if (has_expr)
3654 isl_die(s->ctx, isl_error_invalid,
3655 "expecting universe domain", goto error);
3656 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3657 set = isl_set_universe(space);
3658 dom = isl_set_intersect_params(set, dom);
3659 isl_multi_pw_aff_free(tuple);
3660 tuple = read_tuple(s, v, 0, 0);
3661 if (!tuple)
3662 goto error;
3665 if (isl_stream_eat(s, '}'))
3666 goto error;
3668 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3669 dim = isl_set_dim(dom, isl_dim_all);
3670 dom_space = isl_set_get_space(dom);
3671 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3672 space = isl_space_align_params(space, isl_space_copy(dom_space));
3673 if (!isl_space_is_params(dom_space))
3674 space = isl_space_map_from_domain_and_range(
3675 isl_space_copy(dom_space), space);
3676 isl_space_free(dom_space);
3677 ma = isl_multi_aff_alloc(space);
3679 for (i = 0; i < n; ++i) {
3680 isl_pw_aff *pa;
3681 isl_aff *aff;
3682 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3683 aff = aff_from_pw_aff(pa);
3684 if (!aff)
3685 goto error;
3686 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3687 isl_aff_free(aff);
3688 isl_die(s->ctx, isl_error_invalid,
3689 "not an affine expression", goto error);
3691 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3692 space = isl_multi_aff_get_domain_space(ma);
3693 aff = isl_aff_reset_domain_space(aff, space);
3694 ma = isl_multi_aff_set_aff(ma, i, aff);
3697 isl_multi_pw_aff_free(tuple);
3698 vars_free(v);
3699 isl_set_free(dom);
3700 return ma;
3701 error:
3702 isl_multi_pw_aff_free(tuple);
3703 vars_free(v);
3704 isl_set_free(dom);
3705 isl_multi_aff_free(ma);
3706 return NULL;
3709 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3710 const char *str)
3712 isl_multi_aff *maff;
3713 isl_stream *s = isl_stream_new_str(ctx, str);
3714 if (!s)
3715 return NULL;
3716 maff = isl_stream_read_multi_aff(s);
3717 isl_stream_free(s);
3718 return maff;
3721 /* Read an isl_multi_pw_aff from "s".
3723 * The input format is similar to that of map, except that any conditions
3724 * on the domains should be specified inside the tuple since each
3725 * piecewise affine expression may have a different domain.
3727 * Since we do not know in advance if the isl_multi_pw_aff lives
3728 * in a set or a map space, we first read the first tuple and check
3729 * if it is followed by a "->". If so, we convert the tuple into
3730 * the domain of the isl_multi_pw_aff and read in the next tuple.
3731 * This tuple (or the first tuple if it was not followed by a "->")
3732 * is then converted into the isl_multi_pw_aff through a call
3733 * to extract_mpa_from_tuple and the domain of the result
3734 * is intersected with the domain.
3736 __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3737 __isl_keep isl_stream *s)
3739 struct vars *v;
3740 isl_set *dom = NULL;
3741 isl_multi_pw_aff *tuple = NULL;
3742 isl_multi_pw_aff *mpa = NULL;
3744 v = vars_new(s->ctx);
3745 if (!v)
3746 return NULL;
3748 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3749 if (next_is_tuple(s)) {
3750 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3751 if (isl_stream_eat(s, ISL_TOKEN_TO))
3752 goto error;
3754 if (isl_stream_eat(s, '{'))
3755 goto error;
3757 tuple = read_tuple(s, v, 0, 0);
3758 if (!tuple)
3759 goto error;
3760 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3761 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3762 dom = isl_map_domain(map);
3763 tuple = read_tuple(s, v, 0, 0);
3764 if (!tuple)
3765 goto error;
3768 if (isl_stream_eat(s, '}'))
3769 goto error;
3771 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3773 isl_multi_pw_aff_free(tuple);
3774 vars_free(v);
3775 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3776 return mpa;
3777 error:
3778 isl_multi_pw_aff_free(tuple);
3779 vars_free(v);
3780 isl_set_free(dom);
3781 isl_multi_pw_aff_free(mpa);
3782 return NULL;
3785 /* Read an isl_multi_pw_aff from "str".
3787 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3788 const char *str)
3790 isl_multi_pw_aff *mpa;
3791 isl_stream *s = isl_stream_new_str(ctx, str);
3792 if (!s)
3793 return NULL;
3794 mpa = isl_stream_read_multi_pw_aff(s);
3795 isl_stream_free(s);
3796 return mpa;
3799 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3801 static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3802 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3804 isl_pw_aff *pa;
3805 isl_union_pw_aff *upa = NULL;
3806 isl_set *aff_dom;
3807 int n;
3809 n = v->n;
3810 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3811 pa = read_pw_aff_with_dom(s, aff_dom, v);
3812 vars_drop(v, v->n - n);
3814 upa = isl_union_pw_aff_from_pw_aff(pa);
3816 while (isl_stream_eat_if_available(s, ';')) {
3817 isl_pw_aff *pa_i;
3818 isl_union_pw_aff *upa_i;
3820 n = v->n;
3821 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3822 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3823 vars_drop(v, v->n - n);
3825 upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3826 upa = isl_union_pw_aff_union_add(upa, upa_i);
3829 isl_set_free(dom);
3830 return upa;
3833 /* Read an isl_union_pw_aff from "s".
3835 * First check if there are any paramters, then read in the opening brace
3836 * and use read_union_pw_aff_with_dom to read in the body of
3837 * the isl_union_pw_aff. Finally, read the closing brace.
3839 __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3840 __isl_keep isl_stream *s)
3842 struct vars *v;
3843 isl_set *dom;
3844 isl_union_pw_aff *upa = NULL;
3846 v = vars_new(s->ctx);
3847 if (!v)
3848 return NULL;
3850 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3851 if (next_is_tuple(s)) {
3852 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3853 if (isl_stream_eat(s, ISL_TOKEN_TO))
3854 goto error;
3856 if (isl_stream_eat(s, '{'))
3857 goto error;
3859 upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3861 if (isl_stream_eat(s, '}'))
3862 goto error;
3864 vars_free(v);
3865 isl_set_free(dom);
3866 return upa;
3867 error:
3868 vars_free(v);
3869 isl_set_free(dom);
3870 isl_union_pw_aff_free(upa);
3871 return NULL;
3874 /* Read an isl_union_pw_aff from "str".
3876 __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3877 const char *str)
3879 isl_union_pw_aff *upa;
3880 isl_stream *s = isl_stream_new_str(ctx, str);
3881 if (!s)
3882 return NULL;
3883 upa = isl_stream_read_union_pw_aff(s);
3884 isl_stream_free(s);
3885 return upa;
3888 /* This function is called for each element in a tuple inside
3889 * isl_stream_read_multi_union_pw_aff.
3891 * Read a '{', the union piecewise affine expression body and a '}' and
3892 * add the isl_union_pw_aff to *list.
3894 static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3895 struct vars *v, __isl_take isl_space *space, int rational, void *user)
3897 isl_set *dom;
3898 isl_union_pw_aff *upa;
3899 isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3901 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3902 if (isl_stream_eat(s, '{'))
3903 goto error;
3904 upa = read_union_pw_aff_with_dom(s, dom, v);
3905 *list = isl_union_pw_aff_list_add(*list, upa);
3906 if (isl_stream_eat(s, '}'))
3907 return isl_space_free(space);
3908 if (!*list)
3909 return isl_space_free(space);
3910 return space;
3911 error:
3912 isl_set_free(dom);
3913 return isl_space_free(space);
3916 /* Do the next tokens in "s" correspond to an empty tuple?
3917 * In particular, does the stream start with a '[', followed by a ']',
3918 * not followed by a "->"?
3920 static int next_is_empty_tuple(__isl_keep isl_stream *s)
3922 struct isl_token *tok, *tok2, *tok3;
3923 int is_empty_tuple = 0;
3925 tok = isl_stream_next_token(s);
3926 if (!tok)
3927 return 0;
3928 if (tok->type != '[') {
3929 isl_stream_push_token(s, tok);
3930 return 0;
3933 tok2 = isl_stream_next_token(s);
3934 if (tok2 && tok2->type == ']') {
3935 tok3 = isl_stream_next_token(s);
3936 is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3937 if (tok3)
3938 isl_stream_push_token(s, tok3);
3940 if (tok2)
3941 isl_stream_push_token(s, tok2);
3942 isl_stream_push_token(s, tok);
3944 return is_empty_tuple;
3947 /* Do the next tokens in "s" correspond to a tuple of parameters?
3948 * In particular, does the stream start with a '[' that is not
3949 * followed by a '{' or a nested tuple?
3951 static int next_is_param_tuple(__isl_keep isl_stream *s)
3953 struct isl_token *tok, *tok2;
3954 int is_tuple;
3956 tok = isl_stream_next_token(s);
3957 if (!tok)
3958 return 0;
3959 if (tok->type != '[' || next_is_tuple(s)) {
3960 isl_stream_push_token(s, tok);
3961 return 0;
3964 tok2 = isl_stream_next_token(s);
3965 is_tuple = tok2 && tok2->type != '{';
3966 if (tok2)
3967 isl_stream_push_token(s, tok2);
3968 isl_stream_push_token(s, tok);
3970 return is_tuple;
3973 /* Read the body of an isl_multi_union_pw_aff from "s",
3974 * i.e., everything except the parameter specification.
3975 * "v" contains a description of the identifiers parsed so far.
3976 * The parameters, if any, are specified by "space".
3978 * The body is of the form
3980 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3982 * Read the tuple, collecting the individual isl_union_pw_aff
3983 * elements in a list and construct the result from the tuple space and
3984 * the list.
3986 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
3987 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
3989 isl_union_pw_aff_list *list;
3990 isl_multi_union_pw_aff *mupa;
3992 list = isl_union_pw_aff_list_alloc(s->ctx, 0);
3993 space = read_tuple_space(s, v, space, 1, 0,
3994 &read_union_pw_aff_el, &list);
3995 mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
3997 return mupa;
4000 /* Read an isl_multi_union_pw_aff from "s".
4002 * The input has the form
4004 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4006 * or
4008 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4010 * We first check for the special case of an empty tuple "[]".
4011 * Then we check if there are any parameters.
4012 * Finally, read the tuple and construct the result.
4014 __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4015 __isl_keep isl_stream *s)
4017 struct vars *v;
4018 isl_set *dom = NULL;
4019 isl_space *space;
4020 isl_multi_union_pw_aff *mupa = NULL;
4022 if (next_is_empty_tuple(s)) {
4023 if (isl_stream_eat(s, '['))
4024 return NULL;
4025 if (isl_stream_eat(s, ']'))
4026 return NULL;
4027 space = isl_space_set_alloc(s->ctx, 0, 0);
4028 return isl_multi_union_pw_aff_zero(space);
4031 v = vars_new(s->ctx);
4032 if (!v)
4033 return NULL;
4035 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4036 if (next_is_param_tuple(s)) {
4037 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4038 if (isl_stream_eat(s, ISL_TOKEN_TO))
4039 goto error;
4041 space = isl_set_get_space(dom);
4042 isl_set_free(dom);
4043 mupa = read_multi_union_pw_aff_body(s, v, space);
4045 vars_free(v);
4047 return mupa;
4048 error:
4049 vars_free(v);
4050 isl_set_free(dom);
4051 isl_multi_union_pw_aff_free(mupa);
4052 return NULL;
4055 /* Read an isl_multi_union_pw_aff from "str".
4057 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4058 isl_ctx *ctx, const char *str)
4060 isl_multi_union_pw_aff *mupa;
4061 isl_stream *s = isl_stream_new_str(ctx, str);
4062 if (!s)
4063 return NULL;
4064 mupa = isl_stream_read_multi_union_pw_aff(s);
4065 isl_stream_free(s);
4066 return mupa;
4069 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4070 __isl_keep isl_stream *s)
4072 struct isl_obj obj;
4074 obj = obj_read(s);
4075 if (obj.type == isl_obj_pw_qpolynomial) {
4076 obj.type = isl_obj_union_pw_qpolynomial;
4077 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4079 if (obj.v)
4080 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4081 goto error);
4083 return obj.v;
4084 error:
4085 obj.type->free(obj.v);
4086 return NULL;
4089 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4090 isl_ctx *ctx, const char *str)
4092 isl_union_pw_qpolynomial *upwqp;
4093 isl_stream *s = isl_stream_new_str(ctx, str);
4094 if (!s)
4095 return NULL;
4096 upwqp = isl_stream_read_union_pw_qpolynomial(s);
4097 isl_stream_free(s);
4098 return upwqp;