isl_bool_not: avoid implicit enum conversion warning
[isl.git] / isl_input.c
blobff33e7abd5ecfdd3fb4fec2be771566b323fab9d
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 isl_size 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 isl_size n_out = isl_map_dim(map, isl_dim_out);
834 if (pos < 0 || n_out < 0)
835 return isl_map_free(map);
836 pos += n_out;
838 type = isl_dim_in;
840 if (pos < 0)
841 return isl_map_free(map);
842 --pos;
844 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
845 v, rational);
846 def_map = isl_map_from_pw_aff(def);
847 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
848 def_map = isl_set_unwrap(isl_map_domain(def_map));
850 map = isl_map_intersect(map, def_map);
852 return map;
855 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
856 __isl_take isl_space *dim, struct vars *v)
858 isl_pw_aff *pwaff;
859 isl_pw_aff_list *list;
860 struct isl_token *tok = NULL;
862 pwaff = accept_affine(s, isl_space_copy(dim), v);
863 list = isl_pw_aff_list_from_pw_aff(pwaff);
864 if (!list)
865 goto error;
867 for (;;) {
868 tok = isl_stream_next_token(s);
869 if (!tok) {
870 isl_stream_error(s, NULL, "unexpected EOF");
871 goto error;
873 if (tok->type != ',') {
874 isl_stream_push_token(s, tok);
875 break;
877 isl_token_free(tok);
879 pwaff = accept_affine(s, isl_space_copy(dim), v);
880 list = isl_pw_aff_list_concat(list,
881 isl_pw_aff_list_from_pw_aff(pwaff));
882 if (!list)
883 goto error;
886 isl_space_free(dim);
887 return list;
888 error:
889 isl_space_free(dim);
890 isl_pw_aff_list_free(list);
891 return NULL;
894 static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
895 struct vars *v, __isl_take isl_map *map, int rational)
897 struct isl_token *tok;
899 while ((tok = isl_stream_next_token(s)) != NULL) {
900 int p;
901 int n = v->n;
903 if (tok->type != ISL_TOKEN_IDENT)
904 break;
906 p = vars_pos(v, tok->u.s, -1);
907 if (p < 0)
908 goto error;
909 if (p < n) {
910 isl_stream_error(s, tok, "expecting unique identifier");
911 goto error;
914 map = isl_map_add_dims(map, isl_dim_out, 1);
916 isl_token_free(tok);
917 tok = isl_stream_next_token(s);
918 if (tok && tok->type == '=') {
919 isl_token_free(tok);
920 map = read_var_def(s, map, isl_dim_out, v, rational);
921 tok = isl_stream_next_token(s);
924 if (!tok || tok->type != ',')
925 break;
927 isl_token_free(tok);
929 if (tok)
930 isl_stream_push_token(s, tok);
932 return map;
933 error:
934 isl_token_free(tok);
935 isl_map_free(map);
936 return NULL;
939 static int next_is_tuple(__isl_keep isl_stream *s)
941 struct isl_token *tok;
942 int is_tuple;
944 tok = isl_stream_next_token(s);
945 if (!tok)
946 return 0;
947 if (tok->type == '[') {
948 isl_stream_push_token(s, tok);
949 return 1;
951 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
952 isl_stream_push_token(s, tok);
953 return 0;
956 is_tuple = isl_stream_next_token_is(s, '[');
958 isl_stream_push_token(s, tok);
960 return is_tuple;
963 /* Is "pa" an expression in term of earlier dimensions?
964 * The alternative is that the dimension is defined to be equal to itself,
965 * meaning that it has a universe domain and an expression that depends
966 * on itself. "i" is the position of the expression in a sequence
967 * of "n" expressions. The final dimensions of "pa" correspond to
968 * these "n" expressions.
970 static isl_bool pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
972 isl_aff *aff;
974 if (!pa)
975 return isl_bool_error;
976 if (pa->n != 1)
977 return isl_bool_true;
978 if (!isl_set_plain_is_universe(pa->p[0].set))
979 return isl_bool_true;
981 aff = pa->p[0].aff;
982 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
983 return isl_bool_true;
984 return isl_bool_false;
987 /* Does the tuple contain any dimensions that are defined
988 * in terms of earlier dimensions?
990 static isl_bool tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
992 int i;
993 isl_size n;
994 isl_bool has_expr = isl_bool_false;
995 isl_pw_aff *pa;
997 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
998 if (n < 0)
999 return isl_bool_error;
1000 for (i = 0; i < n; ++i) {
1001 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1002 has_expr = pw_aff_is_expr(pa, i, n);
1003 isl_pw_aff_free(pa);
1004 if (has_expr < 0 || has_expr)
1005 break;
1008 return has_expr;
1011 /* Set the name of dimension "pos" in "space" to "name".
1012 * During printing, we add primes if the same name appears more than once
1013 * to distinguish the occurrences. Here, we remove those primes from "name"
1014 * before setting the name of the dimension.
1016 static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
1017 int pos, char *name)
1019 char *prime;
1021 if (!name)
1022 return space;
1024 prime = strchr(name, '\'');
1025 if (prime)
1026 *prime = '\0';
1027 space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1028 if (prime)
1029 *prime = '\'';
1031 return space;
1034 /* Accept a piecewise affine expression.
1036 * At the outer level, the piecewise affine expression may be of the form
1038 * aff1 : condition1; aff2 : conditions2; ...
1040 * or simply
1042 * aff
1044 * each of the affine expressions may in turn include ternary operators.
1046 * There may be parentheses around some subexpression of "aff1"
1047 * around "aff1" itself, around "aff1 : condition1" and/or
1048 * around the entire piecewise affine expression.
1049 * We therefore remove the opening parenthesis (if any) from the stream
1050 * in case the closing parenthesis follows the colon, but if the closing
1051 * parenthesis is the first thing in the stream after the parsed affine
1052 * expression, we push the parsed expression onto the stream and parse
1053 * again in case the parentheses enclose some subexpression of "aff1".
1055 static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1056 __isl_take isl_space *space, struct vars *v, int rational)
1058 isl_pw_aff *res;
1059 isl_space *res_space;
1061 res_space = isl_space_from_domain(isl_space_copy(space));
1062 res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1063 res = isl_pw_aff_empty(res_space);
1064 do {
1065 isl_pw_aff *pa;
1066 int seen_paren;
1067 int line = -1, col = -1;
1069 set_current_line_col(s, &line, &col);
1070 seen_paren = isl_stream_eat_if_available(s, '(');
1071 if (seen_paren)
1072 pa = accept_piecewise_affine(s, isl_space_copy(space),
1073 v, rational);
1074 else
1075 pa = accept_extended_affine(s, isl_space_copy(space),
1076 v, rational);
1077 if (seen_paren && isl_stream_eat_if_available(s, ')')) {
1078 seen_paren = 0;
1079 if (push_aff(s, line, col, pa) < 0)
1080 goto error;
1081 pa = accept_extended_affine(s, isl_space_copy(space),
1082 v, rational);
1084 if (isl_stream_eat_if_available(s, ':')) {
1085 isl_space *dom_space;
1086 isl_set *dom;
1088 dom_space = isl_pw_aff_get_domain_space(pa);
1089 dom = isl_set_universe(dom_space);
1090 dom = read_formula(s, v, dom, rational);
1091 pa = isl_pw_aff_intersect_domain(pa, dom);
1094 res = isl_pw_aff_union_add(res, pa);
1096 if (seen_paren && isl_stream_eat(s, ')'))
1097 goto error;
1098 } while (isl_stream_eat_if_available(s, ';'));
1100 isl_space_free(space);
1102 return res;
1103 error:
1104 isl_space_free(space);
1105 return isl_pw_aff_free(res);
1108 /* Read an affine expression from "s" for use in read_tuple.
1110 * accept_extended_affine requires a wrapped space as input.
1111 * read_tuple on the other hand expects each isl_pw_aff
1112 * to have an anonymous space. We therefore adjust the space
1113 * of the isl_pw_aff before returning it.
1115 static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1116 struct vars *v, int rational)
1118 isl_space *space;
1119 isl_pw_aff *def;
1121 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1123 def = accept_piecewise_affine(s, space, v, rational);
1125 space = isl_space_set_alloc(s->ctx, 0, v->n);
1126 def = isl_pw_aff_reset_domain_space(def, space);
1128 return def;
1131 /* Read a list of tuple elements by calling "read_el" on each of them and
1132 * return a space with the same number of set dimensions derived from
1133 * the parameter space "space" and possibly updated by "read_el".
1134 * The elements in the list are separated by either "," or "][".
1135 * If "comma" is set then only "," is allowed.
1137 static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1138 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1139 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1140 struct vars *v, __isl_take isl_space *space, int rational,
1141 void *user),
1142 void *user)
1144 if (!space)
1145 return NULL;
1147 space = isl_space_set_from_params(space);
1149 if (isl_stream_next_token_is(s, ']'))
1150 return space;
1152 for (;;) {
1153 struct isl_token *tok;
1155 space = isl_space_add_dims(space, isl_dim_set, 1);
1157 space = read_el(s, v, space, rational, user);
1158 if (!space)
1159 return NULL;
1161 tok = isl_stream_next_token(s);
1162 if (!comma && tok && tok->type == ']' &&
1163 isl_stream_next_token_is(s, '[')) {
1164 isl_token_free(tok);
1165 tok = isl_stream_next_token(s);
1166 } else if (!tok || tok->type != ',') {
1167 if (tok)
1168 isl_stream_push_token(s, tok);
1169 break;
1172 isl_token_free(tok);
1175 return space;
1178 /* Read a tuple space from "s" derived from the parameter space "space".
1179 * Call "read_el" on each element in the tuples.
1181 static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1182 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1183 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1184 struct vars *v, __isl_take isl_space *space, int rational,
1185 void *user),
1186 void *user)
1188 struct isl_token *tok;
1189 char *name = NULL;
1190 isl_space *res = NULL;
1192 tok = isl_stream_next_token(s);
1193 if (!tok)
1194 goto error;
1195 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1196 name = strdup(tok->u.s);
1197 isl_token_free(tok);
1198 if (!name)
1199 goto error;
1200 } else
1201 isl_stream_push_token(s, tok);
1202 if (isl_stream_eat(s, '['))
1203 goto error;
1204 if (next_is_tuple(s)) {
1205 isl_space *out;
1206 res = read_tuple_space(s, v, isl_space_copy(space),
1207 rational, comma, read_el, user);
1208 if (isl_stream_eat(s, ISL_TOKEN_TO))
1209 goto error;
1210 out = read_tuple_space(s, v, isl_space_copy(space),
1211 rational, comma, read_el, user);
1212 res = isl_space_product(res, out);
1213 } else
1214 res = read_tuple_list(s, v, isl_space_copy(space),
1215 rational, comma, read_el, user);
1216 if (isl_stream_eat(s, ']'))
1217 goto error;
1219 if (name) {
1220 res = isl_space_set_tuple_name(res, isl_dim_set, name);
1221 free(name);
1224 isl_space_free(space);
1225 return res;
1226 error:
1227 free(name);
1228 isl_space_free(res);
1229 isl_space_free(space);
1230 return NULL;
1233 /* Construct an isl_pw_aff defined on a space with v->n variables
1234 * that is equal to the last of those variables.
1236 static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1238 isl_space *space;
1239 isl_aff *aff;
1241 space = isl_space_set_alloc(v->ctx, 0, v->n);
1242 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1243 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1244 return isl_pw_aff_from_aff(aff);
1247 /* This function is called for each element in a tuple inside read_tuple.
1248 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1249 * over a space containing all variables in "v" defined so far.
1250 * The isl_pw_aff expresses the new variable in terms of earlier variables
1251 * if a definition is provided. Otherwise, it is represented as being
1252 * equal to itself.
1253 * Add the isl_pw_aff to *list.
1254 * If the new variable was named, then adjust "space" accordingly and
1255 * return the updated space.
1257 static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1258 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1260 isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1261 isl_pw_aff *pa;
1262 struct isl_token *tok;
1263 int new_name = 0;
1265 tok = next_token(s);
1266 if (!tok) {
1267 isl_stream_error(s, NULL, "unexpected EOF");
1268 return isl_space_free(space);
1271 if (tok->type == ISL_TOKEN_IDENT) {
1272 int n = v->n;
1273 int p = vars_pos(v, tok->u.s, -1);
1274 if (p < 0)
1275 goto error;
1276 new_name = p >= n;
1279 if (tok->type == '*') {
1280 if (vars_add_anon(v) < 0)
1281 goto error;
1282 isl_token_free(tok);
1283 pa = identity_tuple_el(v);
1284 } else if (new_name) {
1285 isl_size pos = isl_space_dim(space, isl_dim_out);
1286 if (pos < 0)
1287 goto error;
1288 pos -= 1;
1289 space = space_set_dim_name(space, pos, v->v->name);
1290 isl_token_free(tok);
1291 if (isl_stream_eat_if_available(s, '='))
1292 pa = read_tuple_var_def(s, v, rational);
1293 else
1294 pa = identity_tuple_el(v);
1295 } else {
1296 isl_stream_push_token(s, tok);
1297 tok = NULL;
1298 if (vars_add_anon(v) < 0)
1299 goto error;
1300 pa = read_tuple_var_def(s, v, rational);
1303 *list = isl_pw_aff_list_add(*list, pa);
1304 if (!*list)
1305 return isl_space_free(space);
1307 return space;
1308 error:
1309 isl_token_free(tok);
1310 return isl_space_free(space);
1313 /* Read a tuple and represent it as an isl_multi_pw_aff.
1314 * The range space of the isl_multi_pw_aff is the space of the tuple.
1315 * The domain space is an anonymous space
1316 * with a dimension for each variable in the set of variables in "v",
1317 * including the variables in the range.
1318 * If a given dimension is not defined in terms of earlier dimensions in
1319 * the input, then the corresponding isl_pw_aff is set equal to one time
1320 * the variable corresponding to the dimension being defined.
1322 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1323 * Each element in this list is defined over a space representing
1324 * the variables defined so far. We need to adjust the earlier
1325 * elements to have as many variables in the domain as the final
1326 * element in the list.
1328 static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1329 struct vars *v, int rational, int comma)
1331 int i;
1332 isl_size n;
1333 isl_space *space;
1334 isl_pw_aff_list *list;
1336 space = isl_space_params_alloc(v->ctx, 0);
1337 list = isl_pw_aff_list_alloc(s->ctx, 0);
1338 space = read_tuple_space(s, v, space, rational, comma,
1339 &read_tuple_pw_aff_el, &list);
1340 n = isl_space_dim(space, isl_dim_set);
1341 if (n < 0)
1342 space = isl_space_free(space);
1343 for (i = 0; i + 1 < n; ++i) {
1344 isl_pw_aff *pa;
1346 pa = isl_pw_aff_list_get_pw_aff(list, i);
1347 pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1348 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1351 space = isl_space_from_range(space);
1352 space = isl_space_add_dims(space, isl_dim_in, v->n);
1353 return isl_multi_pw_aff_from_pw_aff_list(space, list);
1356 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1357 * We first create the appropriate space in "map" based on the range
1358 * space of this isl_multi_pw_aff. Then, we add equalities based
1359 * on the affine expressions. These live in an anonymous space,
1360 * however, so we first need to reset the space to that of "map".
1362 static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1363 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1364 int rational)
1366 int i;
1367 isl_size n;
1368 isl_ctx *ctx;
1369 isl_space *space = NULL;
1371 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1372 if (!map || n < 0)
1373 goto error;
1374 ctx = isl_multi_pw_aff_get_ctx(tuple);
1375 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1376 if (!space)
1377 goto error;
1379 if (type == isl_dim_param) {
1380 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1381 isl_space_is_wrapping(space)) {
1382 isl_die(ctx, isl_error_invalid,
1383 "parameter tuples cannot be named or nested",
1384 goto error);
1386 map = isl_map_add_dims(map, type, n);
1387 for (i = 0; i < n; ++i) {
1388 isl_id *id;
1389 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1390 isl_die(ctx, isl_error_invalid,
1391 "parameters must be named",
1392 goto error);
1393 id = isl_space_get_dim_id(space, isl_dim_set, i);
1394 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1396 } else if (type == isl_dim_in) {
1397 isl_set *set;
1399 set = isl_set_universe(isl_space_copy(space));
1400 if (rational)
1401 set = isl_set_set_rational(set);
1402 set = isl_set_intersect_params(set, isl_map_params(map));
1403 map = isl_map_from_domain(set);
1404 } else {
1405 isl_set *set;
1407 set = isl_set_universe(isl_space_copy(space));
1408 if (rational)
1409 set = isl_set_set_rational(set);
1410 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1413 for (i = 0; i < n; ++i) {
1414 isl_pw_aff *pa;
1415 isl_space *space;
1416 isl_aff *aff;
1417 isl_set *set;
1418 isl_map *map_i;
1420 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1421 space = isl_pw_aff_get_domain_space(pa);
1422 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1423 aff = isl_aff_add_coefficient_si(aff,
1424 isl_dim_in, v->n - n + i, -1);
1425 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1426 if (rational)
1427 pa = isl_pw_aff_set_rational(pa);
1428 set = isl_pw_aff_zero_set(pa);
1429 map_i = isl_map_from_range(set);
1430 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1431 map = isl_map_intersect(map, map_i);
1434 isl_space_free(space);
1435 isl_multi_pw_aff_free(tuple);
1436 return map;
1437 error:
1438 isl_space_free(space);
1439 isl_multi_pw_aff_free(tuple);
1440 isl_map_free(map);
1441 return NULL;
1444 /* Read a tuple from "s" and add it to "map".
1445 * The tuple is initially represented as an isl_multi_pw_aff and
1446 * then added to "map".
1448 static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1449 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1450 int rational, int comma)
1452 isl_multi_pw_aff *tuple;
1454 tuple = read_tuple(s, v, rational, comma);
1455 if (!tuple)
1456 return isl_map_free(map);
1458 return map_from_tuple(tuple, map, type, v, rational);
1461 /* Given two equal-length lists of piecewise affine expression with the space
1462 * of "set" as domain, construct a set in the same space that expresses
1463 * that "left" and "right" satisfy the comparison "type".
1465 * A space is constructed of the same dimension as the number of elements
1466 * in the two lists. The comparison is then expressed in a map from
1467 * this space to itself and wrapped into a set. Finally the two lists
1468 * of piecewise affine expressions are plugged into this set.
1470 * Let S be the space of "set" and T the constructed space.
1471 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1472 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1473 * while the comparison is first expressed in T -> T, then [T -> T]
1474 * and finally in S.
1476 static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1477 __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1479 isl_space *space;
1480 isl_size n;
1481 isl_multi_pw_aff *mpa1, *mpa2;
1483 n = isl_pw_aff_list_n_pw_aff(left);
1484 if (!set || n < 0 || !right)
1485 goto error;
1487 space = isl_set_get_space(set);
1488 space = isl_space_from_domain(space);
1489 space = isl_space_add_dims(space, isl_dim_out, n);
1490 mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1491 mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1492 mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1494 space = isl_space_range(space);
1495 switch (type) {
1496 case ISL_TOKEN_LEX_LT:
1497 set = isl_map_wrap(isl_map_lex_lt(space));
1498 break;
1499 case ISL_TOKEN_LEX_GT:
1500 set = isl_map_wrap(isl_map_lex_gt(space));
1501 break;
1502 case ISL_TOKEN_LEX_LE:
1503 set = isl_map_wrap(isl_map_lex_le(space));
1504 break;
1505 case ISL_TOKEN_LEX_GE:
1506 set = isl_map_wrap(isl_map_lex_ge(space));
1507 break;
1508 default:
1509 isl_multi_pw_aff_free(mpa1);
1510 isl_space_free(space);
1511 isl_die(isl_set_get_ctx(set), isl_error_internal,
1512 "unhandled list comparison type", return NULL);
1514 set = isl_set_preimage_multi_pw_aff(set, mpa1);
1515 return set;
1516 error:
1517 isl_pw_aff_list_free(left);
1518 isl_pw_aff_list_free(right);
1519 return NULL;
1522 /* Construct constraints of the form
1524 * a op b
1526 * where a is an element in "left", op is an operator of type "type" and
1527 * b is an element in "right", add the constraints to "set" and return
1528 * the result.
1529 * "rational" is set if the constraints should be treated as
1530 * a rational constraints.
1532 * If "type" is the type of a comparison operator between lists
1533 * of affine expressions, then a single (compound) constraint
1534 * is constructed by list_cmp instead.
1536 static __isl_give isl_set *construct_constraints(
1537 __isl_take isl_set *set, int type,
1538 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1539 int rational)
1541 isl_set *cond;
1543 left = isl_pw_aff_list_copy(left);
1544 right = isl_pw_aff_list_copy(right);
1545 if (rational) {
1546 left = isl_pw_aff_list_set_rational(left);
1547 right = isl_pw_aff_list_set_rational(right);
1549 if (is_list_comparator_type(type))
1550 cond = list_cmp(set, type, left, right);
1551 else if (type == ISL_TOKEN_LE)
1552 cond = isl_pw_aff_list_le_set(left, right);
1553 else if (type == ISL_TOKEN_GE)
1554 cond = isl_pw_aff_list_ge_set(left, right);
1555 else if (type == ISL_TOKEN_LT)
1556 cond = isl_pw_aff_list_lt_set(left, right);
1557 else if (type == ISL_TOKEN_GT)
1558 cond = isl_pw_aff_list_gt_set(left, right);
1559 else if (type == ISL_TOKEN_NE)
1560 cond = isl_pw_aff_list_ne_set(left, right);
1561 else
1562 cond = isl_pw_aff_list_eq_set(left, right);
1564 return isl_set_intersect(set, cond);
1567 /* Read a constraint from "s", add it to "map" and return the result.
1568 * "v" contains a description of the identifiers parsed so far.
1569 * "rational" is set if the constraint should be treated as
1570 * a rational constraint.
1571 * The constraint read from "s" may be applied to multiple pairs
1572 * of affine expressions and may be chained.
1573 * In particular, a list of affine expressions is read, followed
1574 * by a comparison operator and another list of affine expressions.
1575 * The comparison operator is then applied to each pair of elements
1576 * in the two lists and the results are added to "map".
1577 * However, if the operator expects two lists of affine expressions,
1578 * then it is applied directly to those lists and the two lists
1579 * are required to have the same length.
1580 * If the next token is another comparison operator, then another
1581 * list of affine expressions is read and the process repeats.
1583 * The processing is performed on a wrapped copy of "map" because
1584 * an affine expression cannot have a binary relation as domain.
1586 static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1587 struct vars *v, __isl_take isl_map *map, int rational)
1589 struct isl_token *tok;
1590 int type;
1591 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1592 isl_size n1, n2;
1593 isl_set *set;
1595 set = isl_map_wrap(map);
1596 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1597 if (!list1)
1598 goto error;
1599 tok = isl_stream_next_token(s);
1600 if (!is_comparator(tok)) {
1601 isl_stream_error(s, tok, "missing operator");
1602 if (tok)
1603 isl_stream_push_token(s, tok);
1604 goto error;
1606 type = tok->type;
1607 isl_token_free(tok);
1608 for (;;) {
1609 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1610 n1 = isl_pw_aff_list_n_pw_aff(list1);
1611 n2 = isl_pw_aff_list_n_pw_aff(list2);
1612 if (n1 < 0 || n2 < 0)
1613 goto error;
1614 if (is_list_comparator_type(type) && n1 != n2) {
1615 isl_stream_error(s, NULL,
1616 "list arguments not of same size");
1617 goto error;
1620 set = construct_constraints(set, type, list1, list2, rational);
1621 isl_pw_aff_list_free(list1);
1622 list1 = list2;
1624 tok = isl_stream_next_token(s);
1625 if (!is_comparator(tok)) {
1626 if (tok)
1627 isl_stream_push_token(s, tok);
1628 break;
1630 type = tok->type;
1631 isl_token_free(tok);
1633 isl_pw_aff_list_free(list1);
1635 return isl_set_unwrap(set);
1636 error:
1637 isl_pw_aff_list_free(list1);
1638 isl_pw_aff_list_free(list2);
1639 isl_set_free(set);
1640 return NULL;
1643 static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
1644 struct vars *v, __isl_take isl_map *map, int rational)
1646 int n = v->n;
1647 int seen_paren = isl_stream_eat_if_available(s, '(');
1649 map = isl_map_from_domain(isl_map_wrap(map));
1650 map = read_defined_var_list(s, v, map, rational);
1652 if (isl_stream_eat(s, ':'))
1653 goto error;
1655 map = read_formula(s, v, map, rational);
1656 map = isl_set_unwrap(isl_map_domain(map));
1658 vars_drop(v, v->n - n);
1659 if (seen_paren && isl_stream_eat(s, ')'))
1660 goto error;
1662 return map;
1663 error:
1664 isl_map_free(map);
1665 return NULL;
1668 /* Parse an expression between parentheses and push the result
1669 * back on the stream.
1671 * The parsed expression may be either an affine expression
1672 * or a condition. The first type is pushed onto the stream
1673 * as an isl_pw_aff, while the second is pushed as an isl_map.
1675 * If the initial token indicates the start of a condition,
1676 * we parse it as such.
1677 * Otherwise, we first parse an affine expression and push
1678 * that onto the stream. If the affine expression covers the
1679 * entire expression between parentheses, we return.
1680 * Otherwise, we assume that the affine expression is the
1681 * start of a condition and continue parsing.
1683 static int resolve_paren_expr(__isl_keep isl_stream *s,
1684 struct vars *v, __isl_take isl_map *map, int rational)
1686 struct isl_token *tok, *tok2;
1687 int line, col;
1688 isl_pw_aff *pwaff;
1690 tok = isl_stream_next_token(s);
1691 if (!tok || tok->type != '(')
1692 goto error;
1694 if (isl_stream_next_token_is(s, '('))
1695 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1696 goto error;
1698 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1699 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1700 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1701 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1702 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1703 map = read_formula(s, v, map, rational);
1704 if (isl_stream_eat(s, ')'))
1705 goto error;
1706 tok->type = ISL_TOKEN_MAP;
1707 tok->u.map = map;
1708 isl_stream_push_token(s, tok);
1709 return 0;
1712 tok2 = isl_stream_next_token(s);
1713 if (!tok2)
1714 goto error;
1715 line = tok2->line;
1716 col = tok2->col;
1717 isl_stream_push_token(s, tok2);
1719 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1720 if (!pwaff)
1721 goto error;
1723 tok2 = isl_token_new(s->ctx, line, col, 0);
1724 if (!tok2)
1725 goto error2;
1726 tok2->type = ISL_TOKEN_AFF;
1727 tok2->u.pwaff = pwaff;
1729 if (isl_stream_eat_if_available(s, ')')) {
1730 isl_stream_push_token(s, tok2);
1731 isl_token_free(tok);
1732 isl_map_free(map);
1733 return 0;
1736 isl_stream_push_token(s, tok2);
1738 map = read_formula(s, v, map, rational);
1739 if (isl_stream_eat(s, ')'))
1740 goto error;
1742 tok->type = ISL_TOKEN_MAP;
1743 tok->u.map = map;
1744 isl_stream_push_token(s, tok);
1746 return 0;
1747 error2:
1748 isl_pw_aff_free(pwaff);
1749 error:
1750 isl_token_free(tok);
1751 isl_map_free(map);
1752 return -1;
1755 static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
1756 struct vars *v, __isl_take isl_map *map, int rational)
1758 if (isl_stream_next_token_is(s, '('))
1759 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1760 goto error;
1762 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1763 struct isl_token *tok;
1764 tok = isl_stream_next_token(s);
1765 if (!tok)
1766 goto error;
1767 isl_map_free(map);
1768 map = isl_map_copy(tok->u.map);
1769 isl_token_free(tok);
1770 return map;
1773 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1774 return read_exists(s, v, map, rational);
1776 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1777 return map;
1779 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1780 isl_space *dim = isl_map_get_space(map);
1781 isl_map_free(map);
1782 return isl_map_empty(dim);
1785 return add_constraint(s, v, map, rational);
1786 error:
1787 isl_map_free(map);
1788 return NULL;
1791 static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
1792 struct vars *v, __isl_take isl_map *map, int rational)
1794 isl_map *res;
1795 int negate;
1797 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1798 res = read_conjunct(s, v, isl_map_copy(map), rational);
1799 if (negate)
1800 res = isl_map_subtract(isl_map_copy(map), res);
1802 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1803 isl_map *res_i;
1805 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1806 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1807 if (negate)
1808 res = isl_map_subtract(res, res_i);
1809 else
1810 res = isl_map_intersect(res, res_i);
1813 isl_map_free(map);
1814 return res;
1817 static struct isl_map *read_disjuncts(__isl_keep isl_stream *s,
1818 struct vars *v, __isl_take isl_map *map, int rational)
1820 isl_map *res;
1822 if (isl_stream_next_token_is(s, '}'))
1823 return map;
1825 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1826 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1827 isl_map *res_i;
1829 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1830 res = isl_map_union(res, res_i);
1833 isl_map_free(map);
1834 return res;
1837 /* Read a first order formula from "s", add the corresponding
1838 * constraints to "map" and return the result.
1840 * In particular, read a formula of the form
1844 * or
1846 * a implies b
1848 * where a and b are disjunctions.
1850 * In the first case, map is replaced by
1852 * map \cap { [..] : a }
1854 * In the second case, it is replaced by
1856 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1858 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
1859 struct vars *v, __isl_take isl_map *map, int rational)
1861 isl_map *res;
1863 res = read_disjuncts(s, v, isl_map_copy(map), rational);
1865 if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
1866 isl_map *res2;
1868 res = isl_map_subtract(isl_map_copy(map), res);
1869 res2 = read_disjuncts(s, v, map, rational);
1870 res = isl_map_union(res, res2);
1871 } else
1872 isl_map_free(map);
1874 return res;
1877 static isl_size polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1879 isl_size n_out, n_in, n_param, n_div;
1881 n_param = isl_basic_map_dim(bmap, isl_dim_param);
1882 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1883 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1884 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1885 if (n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1886 return isl_size_error;
1888 if (pos < n_out)
1889 return 1 + n_param + n_in + pos;
1890 pos -= n_out;
1892 if (pos < n_in)
1893 return 1 + n_param + pos;
1894 pos -= n_in;
1896 if (pos < n_div)
1897 return 1 + n_param + n_in + n_out + pos;
1898 pos -= n_div;
1900 if (pos < n_param)
1901 return 1 + pos;
1903 return 0;
1906 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1907 __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
1909 int j;
1910 struct isl_token *tok;
1911 int type;
1912 int k;
1913 isl_int *c;
1914 isl_size total;
1916 if (!bmap)
1917 return NULL;
1919 tok = isl_stream_next_token(s);
1920 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1921 isl_stream_error(s, tok, "expecting coefficient");
1922 if (tok)
1923 isl_stream_push_token(s, tok);
1924 goto error;
1926 if (!tok->on_new_line) {
1927 isl_stream_error(s, tok, "coefficient should appear on new line");
1928 isl_stream_push_token(s, tok);
1929 goto error;
1932 type = isl_int_get_si(tok->u.v);
1933 isl_token_free(tok);
1935 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1936 if (type == 0) {
1937 k = isl_basic_map_alloc_equality(bmap);
1938 c = bmap->eq[k];
1939 } else {
1940 k = isl_basic_map_alloc_inequality(bmap);
1941 c = bmap->ineq[k];
1943 if (k < 0)
1944 goto error;
1946 total = isl_basic_map_dim(bmap, isl_dim_all);
1947 if (total < 0)
1948 return isl_basic_map_free(bmap);
1949 for (j = 0; j < 1 + total; ++j) {
1950 isl_size pos;
1951 tok = isl_stream_next_token(s);
1952 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1953 isl_stream_error(s, tok, "expecting coefficient");
1954 if (tok)
1955 isl_stream_push_token(s, tok);
1956 goto error;
1958 if (tok->on_new_line) {
1959 isl_stream_error(s, tok,
1960 "coefficient should not appear on new line");
1961 isl_stream_push_token(s, tok);
1962 goto error;
1964 pos = polylib_pos_to_isl_pos(bmap, j);
1965 if (pos >= 0)
1966 isl_int_set(c[pos], tok->u.v);
1967 isl_token_free(tok);
1968 if (pos < 0)
1969 return isl_basic_map_free(bmap);
1972 return bmap;
1973 error:
1974 isl_basic_map_free(bmap);
1975 return NULL;
1978 static __isl_give isl_basic_map *basic_map_read_polylib(
1979 __isl_keep isl_stream *s)
1981 int i;
1982 struct isl_token *tok;
1983 struct isl_token *tok2;
1984 int n_row, n_col;
1985 int on_new_line;
1986 unsigned in = 0, out, local = 0;
1987 struct isl_basic_map *bmap = NULL;
1988 int nparam = 0;
1990 tok = isl_stream_next_token(s);
1991 if (!tok) {
1992 isl_stream_error(s, NULL, "unexpected EOF");
1993 return NULL;
1995 tok2 = isl_stream_next_token(s);
1996 if (!tok2) {
1997 isl_token_free(tok);
1998 isl_stream_error(s, NULL, "unexpected EOF");
1999 return NULL;
2001 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
2002 isl_stream_push_token(s, tok2);
2003 isl_stream_push_token(s, tok);
2004 isl_stream_error(s, NULL,
2005 "expecting constraint matrix dimensions");
2006 return NULL;
2008 n_row = isl_int_get_si(tok->u.v);
2009 n_col = isl_int_get_si(tok2->u.v);
2010 on_new_line = tok2->on_new_line;
2011 isl_token_free(tok2);
2012 isl_token_free(tok);
2013 isl_assert(s->ctx, !on_new_line, return NULL);
2014 isl_assert(s->ctx, n_row >= 0, return NULL);
2015 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
2016 tok = isl_stream_next_token_on_same_line(s);
2017 if (tok) {
2018 if (tok->type != ISL_TOKEN_VALUE) {
2019 isl_stream_error(s, tok,
2020 "expecting number of output dimensions");
2021 isl_stream_push_token(s, tok);
2022 goto error;
2024 out = isl_int_get_si(tok->u.v);
2025 isl_token_free(tok);
2027 tok = isl_stream_next_token_on_same_line(s);
2028 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2029 isl_stream_error(s, tok,
2030 "expecting number of input dimensions");
2031 if (tok)
2032 isl_stream_push_token(s, tok);
2033 goto error;
2035 in = isl_int_get_si(tok->u.v);
2036 isl_token_free(tok);
2038 tok = isl_stream_next_token_on_same_line(s);
2039 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2040 isl_stream_error(s, tok,
2041 "expecting number of existentials");
2042 if (tok)
2043 isl_stream_push_token(s, tok);
2044 goto error;
2046 local = isl_int_get_si(tok->u.v);
2047 isl_token_free(tok);
2049 tok = isl_stream_next_token_on_same_line(s);
2050 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2051 isl_stream_error(s, tok,
2052 "expecting number of parameters");
2053 if (tok)
2054 isl_stream_push_token(s, tok);
2055 goto error;
2057 nparam = isl_int_get_si(tok->u.v);
2058 isl_token_free(tok);
2059 if (n_col != 1 + out + in + local + nparam + 1) {
2060 isl_stream_error(s, NULL,
2061 "dimensions don't match");
2062 goto error;
2064 } else
2065 out = n_col - 2 - nparam;
2066 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2067 if (!bmap)
2068 return NULL;
2070 for (i = 0; i < local; ++i) {
2071 int k = isl_basic_map_alloc_div(bmap);
2072 if (k < 0)
2073 goto error;
2074 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2077 for (i = 0; i < n_row; ++i)
2078 bmap = basic_map_read_polylib_constraint(s, bmap);
2080 tok = isl_stream_next_token_on_same_line(s);
2081 if (tok) {
2082 isl_stream_error(s, tok, "unexpected extra token on line");
2083 isl_stream_push_token(s, tok);
2084 goto error;
2087 bmap = isl_basic_map_simplify(bmap);
2088 bmap = isl_basic_map_finalize(bmap);
2089 return bmap;
2090 error:
2091 isl_basic_map_free(bmap);
2092 return NULL;
2095 static struct isl_map *map_read_polylib(__isl_keep isl_stream *s)
2097 struct isl_token *tok;
2098 struct isl_token *tok2;
2099 int i, n;
2100 struct isl_map *map;
2102 tok = isl_stream_next_token(s);
2103 if (!tok) {
2104 isl_stream_error(s, NULL, "unexpected EOF");
2105 return NULL;
2107 tok2 = isl_stream_next_token_on_same_line(s);
2108 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2109 isl_stream_push_token(s, tok2);
2110 isl_stream_push_token(s, tok);
2111 return isl_map_from_basic_map(basic_map_read_polylib(s));
2113 if (tok2) {
2114 isl_stream_error(s, tok2, "unexpected token");
2115 isl_stream_push_token(s, tok2);
2116 isl_stream_push_token(s, tok);
2117 return NULL;
2119 n = isl_int_get_si(tok->u.v);
2120 isl_token_free(tok);
2122 isl_assert(s->ctx, n >= 1, return NULL);
2124 map = isl_map_from_basic_map(basic_map_read_polylib(s));
2126 for (i = 1; map && i < n; ++i)
2127 map = isl_map_union(map,
2128 isl_map_from_basic_map(basic_map_read_polylib(s)));
2130 return map;
2133 static int optional_power(__isl_keep isl_stream *s)
2135 int pow;
2136 struct isl_token *tok;
2138 tok = isl_stream_next_token(s);
2139 if (!tok)
2140 return 1;
2141 if (tok->type != '^') {
2142 isl_stream_push_token(s, tok);
2143 return 1;
2145 isl_token_free(tok);
2146 tok = isl_stream_next_token(s);
2147 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2148 isl_stream_error(s, tok, "expecting exponent");
2149 if (tok)
2150 isl_stream_push_token(s, tok);
2151 return 1;
2153 pow = isl_int_get_si(tok->u.v);
2154 isl_token_free(tok);
2155 return pow;
2158 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2159 __isl_keep isl_map *map, struct vars *v);
2161 static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2162 __isl_keep isl_map *map, struct vars *v)
2164 isl_pw_qpolynomial *pwqp;
2165 struct isl_token *tok;
2167 tok = next_token(s);
2168 if (!tok) {
2169 isl_stream_error(s, NULL, "unexpected EOF");
2170 return NULL;
2172 if (tok->type == '(') {
2173 int pow;
2175 isl_token_free(tok);
2176 pwqp = read_term(s, map, v);
2177 if (!pwqp)
2178 return NULL;
2179 if (isl_stream_eat(s, ')'))
2180 goto error;
2181 pow = optional_power(s);
2182 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2183 } else if (tok->type == ISL_TOKEN_VALUE) {
2184 struct isl_token *tok2;
2185 isl_qpolynomial *qp;
2187 tok2 = isl_stream_next_token(s);
2188 if (tok2 && tok2->type == '/') {
2189 isl_token_free(tok2);
2190 tok2 = next_token(s);
2191 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2192 isl_stream_error(s, tok2, "expected denominator");
2193 isl_token_free(tok);
2194 isl_token_free(tok2);
2195 return NULL;
2197 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2198 tok->u.v, tok2->u.v);
2199 isl_token_free(tok2);
2200 } else {
2201 isl_stream_push_token(s, tok2);
2202 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2203 tok->u.v);
2205 isl_token_free(tok);
2206 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2207 } else if (tok->type == ISL_TOKEN_INFTY) {
2208 isl_qpolynomial *qp;
2209 isl_token_free(tok);
2210 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2211 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2212 } else if (tok->type == ISL_TOKEN_NAN) {
2213 isl_qpolynomial *qp;
2214 isl_token_free(tok);
2215 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2216 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2217 } else if (tok->type == ISL_TOKEN_IDENT) {
2218 int n = v->n;
2219 int pos = vars_pos(v, tok->u.s, -1);
2220 int pow;
2221 isl_qpolynomial *qp;
2222 if (pos < 0) {
2223 isl_token_free(tok);
2224 return NULL;
2226 if (pos >= n) {
2227 vars_drop(v, v->n - n);
2228 isl_stream_error(s, tok, "unknown identifier");
2229 isl_token_free(tok);
2230 return NULL;
2232 isl_token_free(tok);
2233 pow = optional_power(s);
2234 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2235 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2236 } else if (is_start_of_div(tok)) {
2237 isl_pw_aff *pwaff;
2238 int pow;
2240 isl_stream_push_token(s, tok);
2241 pwaff = accept_div(s, isl_map_get_space(map), v);
2242 pow = optional_power(s);
2243 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2244 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2245 } else if (tok->type == '-') {
2246 isl_token_free(tok);
2247 pwqp = read_factor(s, map, v);
2248 pwqp = isl_pw_qpolynomial_neg(pwqp);
2249 } else {
2250 isl_stream_error(s, tok, "unexpected isl_token");
2251 isl_stream_push_token(s, tok);
2252 return NULL;
2255 if (isl_stream_eat_if_available(s, '*') ||
2256 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
2257 isl_pw_qpolynomial *pwqp2;
2259 pwqp2 = read_factor(s, map, v);
2260 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2263 return pwqp;
2264 error:
2265 isl_pw_qpolynomial_free(pwqp);
2266 return NULL;
2269 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2270 __isl_keep isl_map *map, struct vars *v)
2272 struct isl_token *tok;
2273 isl_pw_qpolynomial *pwqp;
2275 pwqp = read_factor(s, map, v);
2277 for (;;) {
2278 tok = next_token(s);
2279 if (!tok)
2280 return pwqp;
2282 if (tok->type == '+') {
2283 isl_pw_qpolynomial *pwqp2;
2285 isl_token_free(tok);
2286 pwqp2 = read_factor(s, map, v);
2287 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2288 } else if (tok->type == '-') {
2289 isl_pw_qpolynomial *pwqp2;
2291 isl_token_free(tok);
2292 pwqp2 = read_factor(s, map, v);
2293 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2294 } else if (tok->type == ISL_TOKEN_VALUE &&
2295 isl_int_is_neg(tok->u.v)) {
2296 isl_pw_qpolynomial *pwqp2;
2298 isl_stream_push_token(s, tok);
2299 pwqp2 = read_factor(s, map, v);
2300 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2301 } else {
2302 isl_stream_push_token(s, tok);
2303 break;
2307 return pwqp;
2310 static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2311 __isl_take isl_map *map, struct vars *v, int rational)
2313 struct isl_token *tok;
2315 tok = isl_stream_next_token(s);
2316 if (!tok) {
2317 isl_stream_error(s, NULL, "unexpected EOF");
2318 goto error;
2320 if (tok->type == ':' ||
2321 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
2322 isl_token_free(tok);
2323 map = read_formula(s, v, map, rational);
2324 } else
2325 isl_stream_push_token(s, tok);
2327 return map;
2328 error:
2329 isl_map_free(map);
2330 return NULL;
2333 static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2334 __isl_take isl_map *map, struct vars *v, int n)
2336 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2337 isl_pw_qpolynomial *pwqp;
2338 struct isl_set *set;
2340 pwqp = read_term(s, map, v);
2341 map = read_optional_formula(s, map, v, 0);
2342 set = isl_map_range(map);
2344 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2346 vars_drop(v, v->n - n);
2348 obj.v = pwqp;
2349 return obj;
2352 static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2353 __isl_take isl_set *set, struct vars *v, int n)
2355 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2356 isl_pw_qpolynomial *pwqp;
2357 isl_pw_qpolynomial_fold *pwf = NULL;
2359 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
2360 return obj_read_poly(s, set, v, n);
2362 if (isl_stream_eat(s, '('))
2363 goto error;
2365 pwqp = read_term(s, set, v);
2366 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
2368 while (isl_stream_eat_if_available(s, ',')) {
2369 isl_pw_qpolynomial_fold *pwf_i;
2370 pwqp = read_term(s, set, v);
2371 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
2372 pwqp);
2373 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2376 if (isl_stream_eat(s, ')'))
2377 goto error;
2379 set = read_optional_formula(s, set, v, 0);
2380 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2382 vars_drop(v, v->n - n);
2384 obj.v = pwf;
2385 return obj;
2386 error:
2387 isl_set_free(set);
2388 isl_pw_qpolynomial_fold_free(pwf);
2389 obj.type = isl_obj_none;
2390 return obj;
2393 static int is_rational(__isl_keep isl_stream *s)
2395 struct isl_token *tok;
2397 tok = isl_stream_next_token(s);
2398 if (!tok)
2399 return 0;
2400 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
2401 isl_token_free(tok);
2402 isl_stream_eat(s, ':');
2403 return 1;
2406 isl_stream_push_token(s, tok);
2408 return 0;
2411 static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2412 __isl_take isl_map *map, struct vars *v)
2414 struct isl_token *tok;
2415 struct isl_obj obj = { isl_obj_set, NULL };
2416 int n = v->n;
2417 int rational;
2419 rational = is_rational(s);
2420 if (rational)
2421 map = isl_map_set_rational(map);
2423 if (isl_stream_next_token_is(s, ':')) {
2424 obj.type = isl_obj_set;
2425 obj.v = read_optional_formula(s, map, v, rational);
2426 return obj;
2429 if (!next_is_tuple(s))
2430 return obj_read_poly_or_fold(s, map, v, n);
2432 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2433 if (!map)
2434 goto error;
2435 tok = isl_stream_next_token(s);
2436 if (!tok)
2437 goto error;
2438 if (tok->type == ISL_TOKEN_TO) {
2439 obj.type = isl_obj_map;
2440 isl_token_free(tok);
2441 if (!next_is_tuple(s)) {
2442 isl_set *set = isl_map_domain(map);
2443 return obj_read_poly_or_fold(s, set, v, n);
2445 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2446 if (!map)
2447 goto error;
2448 } else {
2449 map = isl_map_domain(map);
2450 isl_stream_push_token(s, tok);
2453 map = read_optional_formula(s, map, v, rational);
2455 vars_drop(v, v->n - n);
2457 obj.v = map;
2458 return obj;
2459 error:
2460 isl_map_free(map);
2461 obj.type = isl_obj_none;
2462 return obj;
2465 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2467 if (obj.type == isl_obj_map) {
2468 obj.v = isl_union_map_from_map(obj.v);
2469 obj.type = isl_obj_union_map;
2470 } else if (obj.type == isl_obj_set) {
2471 obj.v = isl_union_set_from_set(obj.v);
2472 obj.type = isl_obj_union_set;
2473 } else if (obj.type == isl_obj_pw_qpolynomial) {
2474 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2475 obj.type = isl_obj_union_pw_qpolynomial;
2476 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2477 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2478 obj.type = isl_obj_union_pw_qpolynomial_fold;
2479 } else
2480 isl_assert(ctx, 0, goto error);
2481 return obj;
2482 error:
2483 obj.type->free(obj.v);
2484 obj.type = isl_obj_none;
2485 return obj;
2488 static struct isl_obj obj_add(__isl_keep isl_stream *s,
2489 struct isl_obj obj1, struct isl_obj obj2)
2491 if (obj2.type == isl_obj_none || !obj2.v)
2492 goto error;
2493 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2494 obj1 = to_union(s->ctx, obj1);
2495 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2496 obj2 = to_union(s->ctx, obj2);
2497 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2498 obj1 = to_union(s->ctx, obj1);
2499 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2500 obj2 = to_union(s->ctx, obj2);
2501 if (obj1.type == isl_obj_pw_qpolynomial &&
2502 obj2.type == isl_obj_union_pw_qpolynomial)
2503 obj1 = to_union(s->ctx, obj1);
2504 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2505 obj2.type == isl_obj_pw_qpolynomial)
2506 obj2 = to_union(s->ctx, obj2);
2507 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2508 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2509 obj1 = to_union(s->ctx, obj1);
2510 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2511 obj2.type == isl_obj_pw_qpolynomial_fold)
2512 obj2 = to_union(s->ctx, obj2);
2513 if (obj1.type != obj2.type) {
2514 isl_stream_error(s, NULL,
2515 "attempt to combine incompatible objects");
2516 goto error;
2518 if (!obj1.type->add)
2519 isl_die(s->ctx, isl_error_internal,
2520 "combination not supported on object type", goto error);
2521 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2522 obj1 = to_union(s->ctx, obj1);
2523 obj2 = to_union(s->ctx, obj2);
2525 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2526 obj1 = to_union(s->ctx, obj1);
2527 obj2 = to_union(s->ctx, obj2);
2529 if (obj1.type == isl_obj_pw_qpolynomial &&
2530 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2531 obj1 = to_union(s->ctx, obj1);
2532 obj2 = to_union(s->ctx, obj2);
2534 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2535 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2536 obj1 = to_union(s->ctx, obj1);
2537 obj2 = to_union(s->ctx, obj2);
2539 obj1.v = obj1.type->add(obj1.v, obj2.v);
2540 return obj1;
2541 error:
2542 obj1.type->free(obj1.v);
2543 obj2.type->free(obj2.v);
2544 obj1.type = isl_obj_none;
2545 obj1.v = NULL;
2546 return obj1;
2549 /* Are the first two tokens on "s", "domain" (either as a string
2550 * or as an identifier) followed by ":"?
2552 static int next_is_domain_colon(__isl_keep isl_stream *s)
2554 struct isl_token *tok;
2555 char *name;
2556 int res;
2558 tok = isl_stream_next_token(s);
2559 if (!tok)
2560 return 0;
2561 if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
2562 isl_stream_push_token(s, tok);
2563 return 0;
2566 name = isl_token_get_str(s->ctx, tok);
2567 res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2568 free(name);
2570 isl_stream_push_token(s, tok);
2572 return res;
2575 /* Do the first tokens on "s" look like a schedule?
2577 * The root of a schedule is always a domain node, so the first thing
2578 * we expect in the stream is a domain key, i.e., "domain" followed
2579 * by ":". If the schedule was printed in YAML flow style, then
2580 * we additionally expect a "{" to open the outer mapping.
2582 static int next_is_schedule(__isl_keep isl_stream *s)
2584 struct isl_token *tok;
2585 int is_schedule;
2587 tok = isl_stream_next_token(s);
2588 if (!tok)
2589 return 0;
2590 if (tok->type != '{') {
2591 isl_stream_push_token(s, tok);
2592 return next_is_domain_colon(s);
2595 is_schedule = next_is_domain_colon(s);
2596 isl_stream_push_token(s, tok);
2598 return is_schedule;
2601 /* Read an isl_schedule from "s" and store it in an isl_obj.
2603 static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2605 struct isl_obj obj;
2607 obj.type = isl_obj_schedule;
2608 obj.v = isl_stream_read_schedule(s);
2610 return obj;
2613 /* Read a disjunction of object bodies from "s".
2614 * That is, read the inside of the braces, but not the braces themselves.
2615 * "v" contains a description of the identifiers parsed so far.
2616 * "map" contains information about the parameters.
2618 static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
2619 struct vars *v, __isl_keep isl_map *map)
2621 struct isl_obj obj = { isl_obj_set, NULL };
2623 if (isl_stream_next_token_is(s, '}')) {
2624 obj.type = isl_obj_union_set;
2625 obj.v = isl_union_set_empty(isl_map_get_space(map));
2626 return obj;
2629 for (;;) {
2630 struct isl_obj o;
2631 o = obj_read_body(s, isl_map_copy(map), v);
2632 if (!obj.v)
2633 obj = o;
2634 else
2635 obj = obj_add(s, obj, o);
2636 if (obj.type == isl_obj_none || !obj.v)
2637 return obj;
2638 if (!isl_stream_eat_if_available(s, ';'))
2639 break;
2640 if (isl_stream_next_token_is(s, '}'))
2641 break;
2644 return obj;
2647 static struct isl_obj obj_read(__isl_keep isl_stream *s)
2649 isl_map *map = NULL;
2650 struct isl_token *tok;
2651 struct vars *v = NULL;
2652 struct isl_obj obj = { isl_obj_set, NULL };
2654 if (next_is_schedule(s))
2655 return schedule_read(s);
2657 tok = next_token(s);
2658 if (!tok) {
2659 isl_stream_error(s, NULL, "unexpected EOF");
2660 goto error;
2662 if (tok->type == ISL_TOKEN_VALUE) {
2663 struct isl_token *tok2;
2664 struct isl_map *map;
2666 tok2 = isl_stream_next_token(s);
2667 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2668 isl_int_is_neg(tok2->u.v)) {
2669 if (tok2)
2670 isl_stream_push_token(s, tok2);
2671 obj.type = isl_obj_val;
2672 obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2673 isl_token_free(tok);
2674 return obj;
2676 isl_stream_push_token(s, tok2);
2677 isl_stream_push_token(s, tok);
2678 map = map_read_polylib(s);
2679 if (!map)
2680 goto error;
2681 if (isl_map_may_be_set(map))
2682 obj.v = isl_map_range(map);
2683 else {
2684 obj.type = isl_obj_map;
2685 obj.v = map;
2687 return obj;
2689 v = vars_new(s->ctx);
2690 if (!v) {
2691 isl_stream_push_token(s, tok);
2692 goto error;
2694 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2695 if (tok->type == '[') {
2696 isl_stream_push_token(s, tok);
2697 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2698 if (!map)
2699 goto error;
2700 tok = isl_stream_next_token(s);
2701 if (!tok || tok->type != ISL_TOKEN_TO) {
2702 isl_stream_error(s, tok, "expecting '->'");
2703 if (tok)
2704 isl_stream_push_token(s, tok);
2705 goto error;
2707 isl_token_free(tok);
2708 tok = isl_stream_next_token(s);
2710 if (!tok || tok->type != '{') {
2711 isl_stream_error(s, tok, "expecting '{'");
2712 if (tok)
2713 isl_stream_push_token(s, tok);
2714 goto error;
2716 isl_token_free(tok);
2718 tok = isl_stream_next_token(s);
2719 if (!tok)
2721 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2722 isl_token_free(tok);
2723 if (isl_stream_eat(s, '='))
2724 goto error;
2725 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2726 if (!map)
2727 goto error;
2728 } else
2729 isl_stream_push_token(s, tok);
2731 obj = obj_read_disjuncts(s, v, map);
2732 if (obj.type == isl_obj_none || !obj.v)
2733 goto error;
2735 tok = isl_stream_next_token(s);
2736 if (tok && tok->type == '}') {
2737 isl_token_free(tok);
2738 } else {
2739 isl_stream_error(s, tok, "unexpected isl_token");
2740 if (tok)
2741 isl_token_free(tok);
2742 goto error;
2745 vars_free(v);
2746 isl_map_free(map);
2748 return obj;
2749 error:
2750 isl_map_free(map);
2751 obj.type->free(obj.v);
2752 if (v)
2753 vars_free(v);
2754 obj.v = NULL;
2755 return obj;
2758 struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2760 return obj_read(s);
2763 __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2765 struct isl_obj obj;
2767 obj = obj_read(s);
2768 if (obj.v)
2769 isl_assert(s->ctx, obj.type == isl_obj_map ||
2770 obj.type == isl_obj_set, goto error);
2772 if (obj.type == isl_obj_set)
2773 obj.v = isl_map_from_range(obj.v);
2775 return obj.v;
2776 error:
2777 obj.type->free(obj.v);
2778 return NULL;
2781 __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2783 struct isl_obj obj;
2785 obj = obj_read(s);
2786 if (obj.v) {
2787 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2788 obj.v = isl_map_range(obj.v);
2789 obj.type = isl_obj_set;
2791 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2794 return obj.v;
2795 error:
2796 obj.type->free(obj.v);
2797 return NULL;
2800 __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2802 struct isl_obj obj;
2804 obj = obj_read(s);
2805 if (obj.type == isl_obj_map) {
2806 obj.type = isl_obj_union_map;
2807 obj.v = isl_union_map_from_map(obj.v);
2809 if (obj.type == isl_obj_set) {
2810 obj.type = isl_obj_union_set;
2811 obj.v = isl_union_set_from_set(obj.v);
2813 if (obj.v && obj.type == isl_obj_union_set &&
2814 isl_union_set_is_empty(obj.v))
2815 obj.type = isl_obj_union_map;
2816 if (obj.v && obj.type != isl_obj_union_map)
2817 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2819 return obj.v;
2820 error:
2821 obj.type->free(obj.v);
2822 return NULL;
2825 /* Extract an isl_union_set from "obj".
2826 * This only works if the object was detected as either a set
2827 * (in which case it is converted to a union set) or a union set.
2829 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
2830 struct isl_obj obj)
2832 if (obj.type == isl_obj_set) {
2833 obj.type = isl_obj_union_set;
2834 obj.v = isl_union_set_from_set(obj.v);
2836 if (obj.v)
2837 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
2839 return obj.v;
2840 error:
2841 obj.type->free(obj.v);
2842 return NULL;
2845 /* Read an isl_union_set from "s".
2846 * First read a generic object and then try and extract
2847 * an isl_union_set from that.
2849 __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2851 struct isl_obj obj;
2853 obj = obj_read(s);
2854 return extract_union_set(s->ctx, obj);
2857 static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2859 struct isl_obj obj;
2860 struct isl_map *map;
2861 struct isl_basic_map *bmap;
2863 obj = obj_read(s);
2864 if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
2865 isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
2866 goto error);
2867 map = obj.v;
2868 if (!map)
2869 return NULL;
2871 if (map->n > 1)
2872 isl_die(s->ctx, isl_error_invalid,
2873 "set or map description involves "
2874 "more than one disjunct", goto error);
2876 if (map->n == 0)
2877 bmap = isl_basic_map_empty(isl_map_get_space(map));
2878 else
2879 bmap = isl_basic_map_copy(map->p[0]);
2881 isl_map_free(map);
2883 return bmap;
2884 error:
2885 obj.type->free(obj.v);
2886 return NULL;
2889 static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2891 isl_basic_map *bmap;
2892 bmap = basic_map_read(s);
2893 if (!bmap)
2894 return NULL;
2895 if (!isl_basic_map_may_be_set(bmap))
2896 isl_die(s->ctx, isl_error_invalid,
2897 "input is not a set", goto error);
2898 return isl_basic_map_range(bmap);
2899 error:
2900 isl_basic_map_free(bmap);
2901 return NULL;
2904 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2905 FILE *input)
2907 struct isl_basic_map *bmap;
2908 isl_stream *s = isl_stream_new_file(ctx, input);
2909 if (!s)
2910 return NULL;
2911 bmap = basic_map_read(s);
2912 isl_stream_free(s);
2913 return bmap;
2916 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2917 FILE *input)
2919 isl_basic_set *bset;
2920 isl_stream *s = isl_stream_new_file(ctx, input);
2921 if (!s)
2922 return NULL;
2923 bset = basic_set_read(s);
2924 isl_stream_free(s);
2925 return bset;
2928 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2929 const char *str)
2931 struct isl_basic_map *bmap;
2932 isl_stream *s = isl_stream_new_str(ctx, str);
2933 if (!s)
2934 return NULL;
2935 bmap = basic_map_read(s);
2936 isl_stream_free(s);
2937 return bmap;
2940 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2941 const char *str)
2943 isl_basic_set *bset;
2944 isl_stream *s = isl_stream_new_str(ctx, str);
2945 if (!s)
2946 return NULL;
2947 bset = basic_set_read(s);
2948 isl_stream_free(s);
2949 return bset;
2952 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2953 FILE *input)
2955 struct isl_map *map;
2956 isl_stream *s = isl_stream_new_file(ctx, input);
2957 if (!s)
2958 return NULL;
2959 map = isl_stream_read_map(s);
2960 isl_stream_free(s);
2961 return map;
2964 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2965 const char *str)
2967 struct isl_map *map;
2968 isl_stream *s = isl_stream_new_str(ctx, str);
2969 if (!s)
2970 return NULL;
2971 map = isl_stream_read_map(s);
2972 isl_stream_free(s);
2973 return map;
2976 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2977 FILE *input)
2979 isl_set *set;
2980 isl_stream *s = isl_stream_new_file(ctx, input);
2981 if (!s)
2982 return NULL;
2983 set = isl_stream_read_set(s);
2984 isl_stream_free(s);
2985 return set;
2988 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2989 const char *str)
2991 isl_set *set;
2992 isl_stream *s = isl_stream_new_str(ctx, str);
2993 if (!s)
2994 return NULL;
2995 set = isl_stream_read_set(s);
2996 isl_stream_free(s);
2997 return set;
3000 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
3001 FILE *input)
3003 isl_union_map *umap;
3004 isl_stream *s = isl_stream_new_file(ctx, input);
3005 if (!s)
3006 return NULL;
3007 umap = isl_stream_read_union_map(s);
3008 isl_stream_free(s);
3009 return umap;
3012 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
3013 const char *str)
3015 isl_union_map *umap;
3016 isl_stream *s = isl_stream_new_str(ctx, str);
3017 if (!s)
3018 return NULL;
3019 umap = isl_stream_read_union_map(s);
3020 isl_stream_free(s);
3021 return umap;
3024 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
3025 FILE *input)
3027 isl_union_set *uset;
3028 isl_stream *s = isl_stream_new_file(ctx, input);
3029 if (!s)
3030 return NULL;
3031 uset = isl_stream_read_union_set(s);
3032 isl_stream_free(s);
3033 return uset;
3036 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3037 const char *str)
3039 isl_union_set *uset;
3040 isl_stream *s = isl_stream_new_str(ctx, str);
3041 if (!s)
3042 return NULL;
3043 uset = isl_stream_read_union_set(s);
3044 isl_stream_free(s);
3045 return uset;
3048 static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3050 struct isl_vec *vec = NULL;
3051 struct isl_token *tok;
3052 unsigned size;
3053 int j;
3055 tok = isl_stream_next_token(s);
3056 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3057 isl_stream_error(s, tok, "expecting vector length");
3058 goto error;
3061 size = isl_int_get_si(tok->u.v);
3062 isl_token_free(tok);
3064 vec = isl_vec_alloc(s->ctx, size);
3066 for (j = 0; j < size; ++j) {
3067 tok = isl_stream_next_token(s);
3068 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3069 isl_stream_error(s, tok, "expecting constant value");
3070 goto error;
3072 isl_int_set(vec->el[j], tok->u.v);
3073 isl_token_free(tok);
3076 return vec;
3077 error:
3078 isl_token_free(tok);
3079 isl_vec_free(vec);
3080 return NULL;
3083 static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3085 return isl_vec_read_polylib(s);
3088 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3090 isl_vec *v;
3091 isl_stream *s = isl_stream_new_file(ctx, input);
3092 if (!s)
3093 return NULL;
3094 v = vec_read(s);
3095 isl_stream_free(s);
3096 return v;
3099 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3100 __isl_keep isl_stream *s)
3102 struct isl_obj obj;
3104 obj = obj_read(s);
3105 if (obj.v)
3106 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3107 goto error);
3109 return obj.v;
3110 error:
3111 obj.type->free(obj.v);
3112 return NULL;
3115 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3116 const char *str)
3118 isl_pw_qpolynomial *pwqp;
3119 isl_stream *s = isl_stream_new_str(ctx, str);
3120 if (!s)
3121 return NULL;
3122 pwqp = isl_stream_read_pw_qpolynomial(s);
3123 isl_stream_free(s);
3124 return pwqp;
3127 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3128 FILE *input)
3130 isl_pw_qpolynomial *pwqp;
3131 isl_stream *s = isl_stream_new_file(ctx, input);
3132 if (!s)
3133 return NULL;
3134 pwqp = isl_stream_read_pw_qpolynomial(s);
3135 isl_stream_free(s);
3136 return pwqp;
3139 /* Is the next token an identifer not in "v"?
3141 static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3143 int n = v->n;
3144 int fresh;
3145 struct isl_token *tok;
3147 tok = isl_stream_next_token(s);
3148 if (!tok)
3149 return 0;
3150 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
3151 isl_stream_push_token(s, tok);
3153 vars_drop(v, v->n - n);
3155 return fresh;
3158 /* First read the domain of the affine expression, which may be
3159 * a parameter space or a set.
3160 * The tricky part is that we don't know if the domain is a set or not,
3161 * so when we are trying to read the domain, we may actually be reading
3162 * the affine expression itself (defined on a parameter domains)
3163 * If the tuple we are reading is named, we assume it's the domain.
3164 * Also, if inside the tuple, the first thing we find is a nested tuple
3165 * or a new identifier, we again assume it's the domain.
3166 * Finally, if the tuple is empty, then it must be the domain
3167 * since it does not contain an affine expression.
3168 * Otherwise, we assume we are reading an affine expression.
3170 static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3171 __isl_take isl_set *dom, struct vars *v)
3173 struct isl_token *tok, *tok2;
3174 int is_empty;
3176 tok = isl_stream_next_token(s);
3177 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
3178 isl_stream_push_token(s, tok);
3179 return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3181 if (!tok || tok->type != '[') {
3182 isl_stream_error(s, tok, "expecting '['");
3183 goto error;
3185 tok2 = isl_stream_next_token(s);
3186 is_empty = tok2 && tok2->type == ']';
3187 if (tok2)
3188 isl_stream_push_token(s, tok2);
3189 if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
3190 isl_stream_push_token(s, tok);
3191 dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3192 } else
3193 isl_stream_push_token(s, tok);
3195 return dom;
3196 error:
3197 if (tok)
3198 isl_stream_push_token(s, tok);
3199 isl_set_free(dom);
3200 return NULL;
3203 /* Read an affine expression from "s".
3205 __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3207 isl_aff *aff;
3208 isl_multi_aff *ma;
3209 isl_size dim;
3211 ma = isl_stream_read_multi_aff(s);
3212 dim = isl_multi_aff_dim(ma, isl_dim_out);
3213 if (dim < 0)
3214 goto error;
3215 if (dim != 1)
3216 isl_die(s->ctx, isl_error_invalid,
3217 "expecting single affine expression",
3218 goto error);
3220 aff = isl_multi_aff_get_aff(ma, 0);
3221 isl_multi_aff_free(ma);
3222 return aff;
3223 error:
3224 isl_multi_aff_free(ma);
3225 return NULL;
3228 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3230 static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3231 __isl_take isl_set *dom, struct vars *v)
3233 isl_pw_aff *pwaff = NULL;
3235 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
3236 goto error;
3238 if (isl_stream_eat(s, '['))
3239 goto error;
3241 pwaff = accept_affine(s, isl_set_get_space(dom), v);
3243 if (isl_stream_eat(s, ']'))
3244 goto error;
3246 dom = read_optional_formula(s, dom, v, 0);
3247 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3249 return pwaff;
3250 error:
3251 isl_set_free(dom);
3252 isl_pw_aff_free(pwaff);
3253 return NULL;
3256 __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3258 struct vars *v;
3259 isl_set *dom = NULL;
3260 isl_set *aff_dom;
3261 isl_pw_aff *pa = NULL;
3262 int n;
3264 v = vars_new(s->ctx);
3265 if (!v)
3266 return NULL;
3268 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3269 if (next_is_tuple(s)) {
3270 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3271 if (isl_stream_eat(s, ISL_TOKEN_TO))
3272 goto error;
3274 if (isl_stream_eat(s, '{'))
3275 goto error;
3277 n = v->n;
3278 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3279 pa = read_pw_aff_with_dom(s, aff_dom, v);
3280 vars_drop(v, v->n - n);
3282 while (isl_stream_eat_if_available(s, ';')) {
3283 isl_pw_aff *pa_i;
3285 n = v->n;
3286 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3287 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3288 vars_drop(v, v->n - n);
3290 pa = isl_pw_aff_union_add(pa, pa_i);
3293 if (isl_stream_eat(s, '}'))
3294 goto error;
3296 vars_free(v);
3297 isl_set_free(dom);
3298 return pa;
3299 error:
3300 vars_free(v);
3301 isl_set_free(dom);
3302 isl_pw_aff_free(pa);
3303 return NULL;
3306 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3308 isl_aff *aff;
3309 isl_stream *s = isl_stream_new_str(ctx, str);
3310 if (!s)
3311 return NULL;
3312 aff = isl_stream_read_aff(s);
3313 isl_stream_free(s);
3314 return aff;
3317 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3319 isl_pw_aff *pa;
3320 isl_stream *s = isl_stream_new_str(ctx, str);
3321 if (!s)
3322 return NULL;
3323 pa = isl_stream_read_pw_aff(s);
3324 isl_stream_free(s);
3325 return pa;
3328 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3329 * from a tuple "tuple" read by read_tuple.
3331 * Note that the function read_tuple accepts tuples where some output or
3332 * set dimensions are defined in terms of other output or set dimensions
3333 * since this function is also used to read maps. As a special case,
3334 * read_tuple also accept dimensions that are defined in terms of themselves
3335 * (i.e., that are not defined).
3336 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3337 * that the definitions of the output/set dimensions do not involve any
3338 * output/set dimensions.
3339 * Finally, drop the output dimensions from the domain of the result
3340 * of read_tuple (which is of the form [input, output] -> [output],
3341 * with anonymous domain) and reset the space.
3343 static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3344 __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3346 int i;
3347 isl_size dim, n;
3348 isl_space *space;
3349 isl_multi_pw_aff *mpa;
3351 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3352 dim = isl_space_dim(dom_space, isl_dim_all);
3353 if (n < 0 || dim < 0)
3354 dom_space = isl_space_free(dom_space);
3355 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3356 space = isl_space_align_params(space, isl_space_copy(dom_space));
3357 if (!isl_space_is_params(dom_space))
3358 space = isl_space_map_from_domain_and_range(
3359 isl_space_copy(dom_space), space);
3360 isl_space_free(dom_space);
3361 mpa = isl_multi_pw_aff_alloc(space);
3363 for (i = 0; i < n; ++i) {
3364 isl_pw_aff *pa;
3365 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3366 if (!pa)
3367 return isl_multi_pw_aff_free(mpa);
3368 if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3369 isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3370 isl_pw_aff_free(pa);
3371 isl_die(ctx, isl_error_invalid,
3372 "not an affine expression",
3373 return isl_multi_pw_aff_free(mpa));
3375 pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3376 space = isl_multi_pw_aff_get_domain_space(mpa);
3377 pa = isl_pw_aff_reset_domain_space(pa, space);
3378 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3381 return mpa;
3384 /* Read a tuple of affine expressions, together with optional constraints
3385 * on the domain from "s". "dom" represents the initial constraints
3386 * on the domain.
3388 * The isl_multi_aff may live in either a set or a map space.
3389 * First read the first tuple and check if it is followed by a "->".
3390 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3391 * read in the next tuple. This tuple (or the first tuple if it was
3392 * not followed by a "->") is then converted into an isl_multi_pw_aff
3393 * through a call to extract_mpa_from_tuple.
3394 * The result is converted to an isl_pw_multi_aff and
3395 * its domain is intersected with the domain.
3397 static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3398 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3400 isl_multi_pw_aff *tuple;
3401 isl_multi_pw_aff *mpa;
3402 isl_pw_multi_aff *pma;
3403 int n = v->n;
3405 tuple = read_tuple(s, v, 0, 0);
3406 if (!tuple)
3407 goto error;
3408 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3409 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3410 dom = isl_map_domain(map);
3411 tuple = read_tuple(s, v, 0, 0);
3412 if (!tuple)
3413 goto error;
3416 dom = read_optional_formula(s, dom, v, 0);
3418 vars_drop(v, v->n - n);
3420 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3421 isl_multi_pw_aff_free(tuple);
3422 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3423 pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3425 return pma;
3426 error:
3427 isl_set_free(dom);
3428 return NULL;
3431 /* Read an isl_pw_multi_aff from "s".
3433 * In particular, first read the parameters and then read a sequence
3434 * of one or more tuples of affine expressions with optional conditions and
3435 * add them up.
3437 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3438 __isl_keep isl_stream *s)
3440 struct vars *v;
3441 isl_set *dom;
3442 isl_pw_multi_aff *pma = NULL;
3444 v = vars_new(s->ctx);
3445 if (!v)
3446 return NULL;
3448 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3449 if (next_is_tuple(s)) {
3450 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3451 if (isl_stream_eat(s, ISL_TOKEN_TO))
3452 goto error;
3454 if (isl_stream_eat(s, '{'))
3455 goto error;
3457 pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3459 while (isl_stream_eat_if_available(s, ';')) {
3460 isl_pw_multi_aff *pma2;
3462 pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3463 pma = isl_pw_multi_aff_union_add(pma, pma2);
3464 if (!pma)
3465 goto error;
3468 if (isl_stream_eat(s, '}'))
3469 goto error;
3471 isl_set_free(dom);
3472 vars_free(v);
3473 return pma;
3474 error:
3475 isl_pw_multi_aff_free(pma);
3476 isl_set_free(dom);
3477 vars_free(v);
3478 return NULL;
3481 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3482 const char *str)
3484 isl_pw_multi_aff *pma;
3485 isl_stream *s = isl_stream_new_str(ctx, str);
3486 if (!s)
3487 return NULL;
3488 pma = isl_stream_read_pw_multi_aff(s);
3489 isl_stream_free(s);
3490 return pma;
3493 /* Read an isl_union_pw_multi_aff from "s".
3494 * We currently read a generic object and if it turns out to be a set or
3495 * a map, we convert that to an isl_union_pw_multi_aff.
3496 * It would be more efficient if we were to construct
3497 * the isl_union_pw_multi_aff directly.
3499 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3500 __isl_keep isl_stream *s)
3502 struct isl_obj obj;
3504 obj = obj_read(s);
3505 if (!obj.v)
3506 return NULL;
3508 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
3509 obj = to_union(s->ctx, obj);
3510 if (obj.type == isl_obj_union_map)
3511 return isl_union_pw_multi_aff_from_union_map(obj.v);
3512 if (obj.type == isl_obj_union_set)
3513 return isl_union_pw_multi_aff_from_union_set(obj.v);
3515 obj.type->free(obj.v);
3516 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3517 return NULL);
3520 /* Read an isl_union_pw_multi_aff from "str".
3522 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3523 isl_ctx *ctx, const char *str)
3525 isl_union_pw_multi_aff *upma;
3526 isl_stream *s = isl_stream_new_str(ctx, str);
3527 if (!s)
3528 return NULL;
3529 upma = isl_stream_read_union_pw_multi_aff(s);
3530 isl_stream_free(s);
3531 return upma;
3534 /* Assuming "pa" represents a single affine expression defined on a universe
3535 * domain, extract this affine expression.
3537 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3539 isl_aff *aff;
3541 if (!pa)
3542 return NULL;
3543 if (pa->n != 1)
3544 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3545 "expecting single affine expression",
3546 goto error);
3547 if (!isl_set_plain_is_universe(pa->p[0].set))
3548 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3549 "expecting universe domain",
3550 goto error);
3552 aff = isl_aff_copy(pa->p[0].aff);
3553 isl_pw_aff_free(pa);
3554 return aff;
3555 error:
3556 isl_pw_aff_free(pa);
3557 return NULL;
3560 /* This function is called for each element in a tuple inside
3561 * isl_stream_read_multi_val.
3562 * Read an isl_val from "s" and add it to *list.
3564 static __isl_give isl_space *read_val_el(__isl_keep isl_stream *s,
3565 struct vars *v, __isl_take isl_space *space, int rational, void *user)
3567 isl_val_list **list = (isl_val_list **) user;
3568 isl_val *val;
3570 val = isl_stream_read_val(s);
3571 *list = isl_val_list_add(*list, val);
3572 if (!*list)
3573 return isl_space_free(space);
3575 return space;
3578 /* Read an isl_multi_val from "s".
3580 * We first read a tuple space, collecting the element values in a list.
3581 * Then we create an isl_multi_val from the space and the isl_val_list.
3583 __isl_give isl_multi_val *isl_stream_read_multi_val(__isl_keep isl_stream *s)
3585 struct vars *v;
3586 isl_set *dom = NULL;
3587 isl_space *space;
3588 isl_multi_val *mv = NULL;
3589 isl_val_list *list;
3591 v = vars_new(s->ctx);
3592 if (!v)
3593 return NULL;
3595 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3596 if (next_is_tuple(s)) {
3597 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3598 if (isl_stream_eat(s, ISL_TOKEN_TO))
3599 goto error;
3601 if (!isl_set_plain_is_universe(dom))
3602 isl_die(s->ctx, isl_error_invalid,
3603 "expecting universe parameter domain", goto error);
3604 if (isl_stream_eat(s, '{'))
3605 goto error;
3607 space = isl_set_get_space(dom);
3609 list = isl_val_list_alloc(s->ctx, 0);
3610 space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list);
3611 mv = isl_multi_val_from_val_list(space, list);
3613 if (isl_stream_eat(s, '}'))
3614 goto error;
3616 vars_free(v);
3617 isl_set_free(dom);
3618 return mv;
3619 error:
3620 vars_free(v);
3621 isl_set_free(dom);
3622 isl_multi_val_free(mv);
3623 return NULL;
3626 /* Read an isl_multi_val from "str".
3628 __isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx,
3629 const char *str)
3631 isl_multi_val *mv;
3632 isl_stream *s = isl_stream_new_str(ctx, str);
3633 if (!s)
3634 return NULL;
3635 mv = isl_stream_read_multi_val(s);
3636 isl_stream_free(s);
3637 return mv;
3640 /* Read a multi-affine expression from "s".
3641 * If the multi-affine expression has a domain, then the tuple
3642 * representing this domain cannot involve any affine expressions.
3643 * The tuple representing the actual expressions needs to consist
3644 * of only affine expressions. Moreover, these expressions can
3645 * only depend on parameters and input dimensions and not on other
3646 * output dimensions.
3648 __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3650 struct vars *v;
3651 isl_set *dom = NULL;
3652 isl_multi_pw_aff *tuple = NULL;
3653 int i;
3654 isl_size dim, n;
3655 isl_space *space, *dom_space;
3656 isl_multi_aff *ma = NULL;
3658 v = vars_new(s->ctx);
3659 if (!v)
3660 return NULL;
3662 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3663 if (next_is_tuple(s)) {
3664 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3665 if (isl_stream_eat(s, ISL_TOKEN_TO))
3666 goto error;
3668 if (!isl_set_plain_is_universe(dom))
3669 isl_die(s->ctx, isl_error_invalid,
3670 "expecting universe parameter domain", goto error);
3671 if (isl_stream_eat(s, '{'))
3672 goto error;
3674 tuple = read_tuple(s, v, 0, 0);
3675 if (!tuple)
3676 goto error;
3677 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3678 isl_set *set;
3679 isl_space *space;
3680 isl_bool has_expr;
3682 has_expr = tuple_has_expr(tuple);
3683 if (has_expr < 0)
3684 goto error;
3685 if (has_expr)
3686 isl_die(s->ctx, isl_error_invalid,
3687 "expecting universe domain", goto error);
3688 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3689 set = isl_set_universe(space);
3690 dom = isl_set_intersect_params(set, dom);
3691 isl_multi_pw_aff_free(tuple);
3692 tuple = read_tuple(s, v, 0, 0);
3693 if (!tuple)
3694 goto error;
3697 if (isl_stream_eat(s, '}'))
3698 goto error;
3700 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3701 dim = isl_set_dim(dom, isl_dim_all);
3702 if (n < 0 || dim < 0)
3703 goto error;
3704 dom_space = isl_set_get_space(dom);
3705 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3706 space = isl_space_align_params(space, isl_space_copy(dom_space));
3707 if (!isl_space_is_params(dom_space))
3708 space = isl_space_map_from_domain_and_range(
3709 isl_space_copy(dom_space), space);
3710 isl_space_free(dom_space);
3711 ma = isl_multi_aff_alloc(space);
3713 for (i = 0; i < n; ++i) {
3714 isl_pw_aff *pa;
3715 isl_aff *aff;
3716 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3717 aff = aff_from_pw_aff(pa);
3718 if (!aff)
3719 goto error;
3720 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3721 isl_aff_free(aff);
3722 isl_die(s->ctx, isl_error_invalid,
3723 "not an affine expression", goto error);
3725 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3726 space = isl_multi_aff_get_domain_space(ma);
3727 aff = isl_aff_reset_domain_space(aff, space);
3728 ma = isl_multi_aff_set_aff(ma, i, aff);
3731 isl_multi_pw_aff_free(tuple);
3732 vars_free(v);
3733 isl_set_free(dom);
3734 return ma;
3735 error:
3736 isl_multi_pw_aff_free(tuple);
3737 vars_free(v);
3738 isl_set_free(dom);
3739 isl_multi_aff_free(ma);
3740 return NULL;
3743 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3744 const char *str)
3746 isl_multi_aff *maff;
3747 isl_stream *s = isl_stream_new_str(ctx, str);
3748 if (!s)
3749 return NULL;
3750 maff = isl_stream_read_multi_aff(s);
3751 isl_stream_free(s);
3752 return maff;
3755 /* Read an isl_multi_pw_aff from "s".
3757 * The input format is similar to that of map, except that any conditions
3758 * on the domains should be specified inside the tuple since each
3759 * piecewise affine expression may have a different domain.
3760 * However, additional, shared conditions can also be specified.
3761 * This is especially useful for setting the explicit domain
3762 * of a zero-dimensional isl_multi_pw_aff.
3764 * Since we do not know in advance if the isl_multi_pw_aff lives
3765 * in a set or a map space, we first read the first tuple and check
3766 * if it is followed by a "->". If so, we convert the tuple into
3767 * the domain of the isl_multi_pw_aff and read in the next tuple.
3768 * This tuple (or the first tuple if it was not followed by a "->")
3769 * is then converted into the isl_multi_pw_aff through a call
3770 * to extract_mpa_from_tuple and the domain of the result
3771 * is intersected with the domain.
3773 __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3774 __isl_keep isl_stream *s)
3776 struct vars *v;
3777 isl_set *dom = NULL;
3778 isl_multi_pw_aff *tuple = NULL;
3779 isl_multi_pw_aff *mpa = NULL;
3781 v = vars_new(s->ctx);
3782 if (!v)
3783 return NULL;
3785 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3786 if (next_is_tuple(s)) {
3787 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3788 if (isl_stream_eat(s, ISL_TOKEN_TO))
3789 goto error;
3791 if (isl_stream_eat(s, '{'))
3792 goto error;
3794 tuple = read_tuple(s, v, 0, 0);
3795 if (!tuple)
3796 goto error;
3797 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3798 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3799 dom = isl_map_domain(map);
3800 tuple = read_tuple(s, v, 0, 0);
3801 if (!tuple)
3802 goto error;
3805 if (isl_stream_eat_if_available(s, ':'))
3806 dom = read_formula(s, v, dom, 0);
3808 if (isl_stream_eat(s, '}'))
3809 goto error;
3811 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3813 isl_multi_pw_aff_free(tuple);
3814 vars_free(v);
3815 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3816 return mpa;
3817 error:
3818 isl_multi_pw_aff_free(tuple);
3819 vars_free(v);
3820 isl_set_free(dom);
3821 isl_multi_pw_aff_free(mpa);
3822 return NULL;
3825 /* Read an isl_multi_pw_aff from "str".
3827 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3828 const char *str)
3830 isl_multi_pw_aff *mpa;
3831 isl_stream *s = isl_stream_new_str(ctx, str);
3832 if (!s)
3833 return NULL;
3834 mpa = isl_stream_read_multi_pw_aff(s);
3835 isl_stream_free(s);
3836 return mpa;
3839 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3841 static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3842 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3844 isl_pw_aff *pa;
3845 isl_union_pw_aff *upa = NULL;
3846 isl_set *aff_dom;
3847 int n;
3849 n = v->n;
3850 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3851 pa = read_pw_aff_with_dom(s, aff_dom, v);
3852 vars_drop(v, v->n - n);
3854 upa = isl_union_pw_aff_from_pw_aff(pa);
3856 while (isl_stream_eat_if_available(s, ';')) {
3857 isl_pw_aff *pa_i;
3858 isl_union_pw_aff *upa_i;
3860 n = v->n;
3861 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3862 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3863 vars_drop(v, v->n - n);
3865 upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3866 upa = isl_union_pw_aff_union_add(upa, upa_i);
3869 isl_set_free(dom);
3870 return upa;
3873 /* Read an isl_union_pw_aff from "s".
3875 * First check if there are any paramters, then read in the opening brace
3876 * and use read_union_pw_aff_with_dom to read in the body of
3877 * the isl_union_pw_aff. Finally, read the closing brace.
3879 __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3880 __isl_keep isl_stream *s)
3882 struct vars *v;
3883 isl_set *dom;
3884 isl_union_pw_aff *upa = NULL;
3886 v = vars_new(s->ctx);
3887 if (!v)
3888 return NULL;
3890 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3891 if (next_is_tuple(s)) {
3892 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3893 if (isl_stream_eat(s, ISL_TOKEN_TO))
3894 goto error;
3896 if (isl_stream_eat(s, '{'))
3897 goto error;
3899 upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3901 if (isl_stream_eat(s, '}'))
3902 goto error;
3904 vars_free(v);
3905 isl_set_free(dom);
3906 return upa;
3907 error:
3908 vars_free(v);
3909 isl_set_free(dom);
3910 isl_union_pw_aff_free(upa);
3911 return NULL;
3914 /* Read an isl_union_pw_aff from "str".
3916 __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3917 const char *str)
3919 isl_union_pw_aff *upa;
3920 isl_stream *s = isl_stream_new_str(ctx, str);
3921 if (!s)
3922 return NULL;
3923 upa = isl_stream_read_union_pw_aff(s);
3924 isl_stream_free(s);
3925 return upa;
3928 /* This function is called for each element in a tuple inside
3929 * isl_stream_read_multi_union_pw_aff.
3931 * Read a '{', the union piecewise affine expression body and a '}' and
3932 * add the isl_union_pw_aff to *list.
3934 static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3935 struct vars *v, __isl_take isl_space *space, int rational, void *user)
3937 isl_set *dom;
3938 isl_union_pw_aff *upa;
3939 isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3941 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3942 if (isl_stream_eat(s, '{'))
3943 goto error;
3944 upa = read_union_pw_aff_with_dom(s, dom, v);
3945 *list = isl_union_pw_aff_list_add(*list, upa);
3946 if (isl_stream_eat(s, '}'))
3947 return isl_space_free(space);
3948 if (!*list)
3949 return isl_space_free(space);
3950 return space;
3951 error:
3952 isl_set_free(dom);
3953 return isl_space_free(space);
3956 /* Do the next tokens in "s" correspond to an empty tuple?
3957 * In particular, does the stream start with a '[', followed by a ']',
3958 * not followed by a "->"?
3960 static int next_is_empty_tuple(__isl_keep isl_stream *s)
3962 struct isl_token *tok, *tok2, *tok3;
3963 int is_empty_tuple = 0;
3965 tok = isl_stream_next_token(s);
3966 if (!tok)
3967 return 0;
3968 if (tok->type != '[') {
3969 isl_stream_push_token(s, tok);
3970 return 0;
3973 tok2 = isl_stream_next_token(s);
3974 if (tok2 && tok2->type == ']') {
3975 tok3 = isl_stream_next_token(s);
3976 is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3977 if (tok3)
3978 isl_stream_push_token(s, tok3);
3980 if (tok2)
3981 isl_stream_push_token(s, tok2);
3982 isl_stream_push_token(s, tok);
3984 return is_empty_tuple;
3987 /* Do the next tokens in "s" correspond to a tuple of parameters?
3988 * In particular, does the stream start with a '[' that is not
3989 * followed by a '{' or a nested tuple?
3991 static int next_is_param_tuple(__isl_keep isl_stream *s)
3993 struct isl_token *tok, *tok2;
3994 int is_tuple;
3996 tok = isl_stream_next_token(s);
3997 if (!tok)
3998 return 0;
3999 if (tok->type != '[' || next_is_tuple(s)) {
4000 isl_stream_push_token(s, tok);
4001 return 0;
4004 tok2 = isl_stream_next_token(s);
4005 is_tuple = tok2 && tok2->type != '{';
4006 if (tok2)
4007 isl_stream_push_token(s, tok2);
4008 isl_stream_push_token(s, tok);
4010 return is_tuple;
4013 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4014 * i.e., everything except the parameter specification and
4015 * without shared domain constraints.
4016 * "v" contains a description of the identifiers parsed so far.
4017 * The parameters, if any, are specified by "space".
4019 * The body is of the form
4021 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4023 * Read the tuple, collecting the individual isl_union_pw_aff
4024 * elements in a list and construct the result from the tuple space and
4025 * the list.
4027 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
4028 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4030 isl_union_pw_aff_list *list;
4031 isl_multi_union_pw_aff *mupa;
4033 list = isl_union_pw_aff_list_alloc(s->ctx, 0);
4034 space = read_tuple_space(s, v, space, 1, 0,
4035 &read_union_pw_aff_el, &list);
4036 mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
4038 return mupa;
4041 /* Read the body of an isl_union_set from "s",
4042 * i.e., everything except the parameter specification.
4043 * "v" contains a description of the identifiers parsed so far.
4044 * The parameters, if any, are specified by "space".
4046 * First read a generic disjunction of object bodies and then try and extract
4047 * an isl_union_set from that.
4049 static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
4050 struct vars *v, __isl_take isl_space *space)
4052 struct isl_obj obj = { isl_obj_set, NULL };
4053 isl_map *map;
4055 map = isl_set_universe(space);
4056 if (isl_stream_eat(s, '{') < 0)
4057 goto error;
4058 obj = obj_read_disjuncts(s, v, map);
4059 if (isl_stream_eat(s, '}') < 0)
4060 goto error;
4061 isl_map_free(map);
4063 return extract_union_set(s->ctx, obj);
4064 error:
4065 obj.type->free(obj.v);
4066 isl_map_free(map);
4067 return NULL;
4070 /* Read the body of an isl_multi_union_pw_aff from "s",
4071 * i.e., everything except the parameter specification.
4072 * "v" contains a description of the identifiers parsed so far.
4073 * The parameters, if any, are specified by "space".
4075 * In particular, handle the special case with shared domain constraints.
4076 * These are specified as
4078 * ([...] : ...)
4080 * and are especially useful for setting the explicit domain
4081 * of a zero-dimensional isl_multi_union_pw_aff.
4082 * The core isl_multi_union_pw_aff body ([...]) is read by
4083 * read_multi_union_pw_aff_body_core.
4085 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
4086 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4088 isl_multi_union_pw_aff *mupa;
4090 if (!isl_stream_next_token_is(s, '('))
4091 return read_multi_union_pw_aff_body_core(s, v, space);
4093 if (isl_stream_eat(s, '(') < 0)
4094 goto error;
4095 mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
4096 if (isl_stream_eat_if_available(s, ':')) {
4097 isl_union_set *dom;
4099 dom = read_union_set_body(s, v, space);
4100 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4101 } else {
4102 isl_space_free(space);
4104 if (isl_stream_eat(s, ')') < 0)
4105 return isl_multi_union_pw_aff_free(mupa);
4107 return mupa;
4108 error:
4109 isl_space_free(space);
4110 return NULL;
4113 /* Read an isl_multi_union_pw_aff from "s".
4115 * The input has the form
4117 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4119 * or
4121 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4123 * Additionally, a shared domain may be specified as
4125 * ([..] : ...)
4127 * or
4129 * [..] -> ([..] : ...)
4131 * The first case is handled by the caller, the second case
4132 * is handled by read_multi_union_pw_aff_body.
4134 * We first check for the special case of an empty tuple "[]".
4135 * Then we check if there are any parameters.
4136 * Finally, read the tuple and construct the result.
4138 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
4139 __isl_keep isl_stream *s)
4141 struct vars *v;
4142 isl_set *dom = NULL;
4143 isl_space *space;
4144 isl_multi_union_pw_aff *mupa = NULL;
4146 if (next_is_empty_tuple(s)) {
4147 if (isl_stream_eat(s, '['))
4148 return NULL;
4149 if (isl_stream_eat(s, ']'))
4150 return NULL;
4151 space = isl_space_set_alloc(s->ctx, 0, 0);
4152 return isl_multi_union_pw_aff_zero(space);
4155 v = vars_new(s->ctx);
4156 if (!v)
4157 return NULL;
4159 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4160 if (next_is_param_tuple(s)) {
4161 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4162 if (isl_stream_eat(s, ISL_TOKEN_TO))
4163 goto error;
4165 space = isl_set_get_space(dom);
4166 isl_set_free(dom);
4167 mupa = read_multi_union_pw_aff_body(s, v, space);
4169 vars_free(v);
4171 return mupa;
4172 error:
4173 vars_free(v);
4174 isl_set_free(dom);
4175 isl_multi_union_pw_aff_free(mupa);
4176 return NULL;
4179 /* Read an isl_multi_union_pw_aff from "s".
4181 * In particular, handle the special case with shared domain constraints.
4182 * These are specified as
4184 * ([...] : ...)
4186 * and are especially useful for setting the explicit domain
4187 * of a zero-dimensional isl_multi_union_pw_aff.
4188 * The core isl_multi_union_pw_aff ([...]) is read by
4189 * read_multi_union_pw_aff_core.
4191 __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4192 __isl_keep isl_stream *s)
4194 isl_multi_union_pw_aff *mupa;
4196 if (!isl_stream_next_token_is(s, '('))
4197 return read_multi_union_pw_aff_core(s);
4199 if (isl_stream_eat(s, '(') < 0)
4200 return NULL;
4201 mupa = read_multi_union_pw_aff_core(s);
4202 if (isl_stream_eat_if_available(s, ':')) {
4203 isl_union_set *dom;
4205 dom = isl_stream_read_union_set(s);
4206 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4208 if (isl_stream_eat(s, ')') < 0)
4209 return isl_multi_union_pw_aff_free(mupa);
4210 return mupa;
4213 /* Read an isl_multi_union_pw_aff from "str".
4215 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4216 isl_ctx *ctx, const char *str)
4218 isl_multi_union_pw_aff *mupa;
4219 isl_stream *s = isl_stream_new_str(ctx, str);
4220 if (!s)
4221 return NULL;
4222 mupa = isl_stream_read_multi_union_pw_aff(s);
4223 isl_stream_free(s);
4224 return mupa;
4227 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4228 __isl_keep isl_stream *s)
4230 struct isl_obj obj;
4232 obj = obj_read(s);
4233 if (obj.type == isl_obj_pw_qpolynomial) {
4234 obj.type = isl_obj_union_pw_qpolynomial;
4235 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4237 if (obj.v)
4238 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4239 goto error);
4241 return obj.v;
4242 error:
4243 obj.type->free(obj.v);
4244 return NULL;
4247 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4248 isl_ctx *ctx, const char *str)
4250 isl_union_pw_qpolynomial *upwqp;
4251 isl_stream *s = isl_stream_new_str(ctx, str);
4252 if (!s)
4253 return NULL;
4254 upwqp = isl_stream_read_union_pw_qpolynomial(s);
4255 isl_stream_free(s);
4256 return upwqp;