add exported isl_aff_bind_id
[isl.git] / isl_input.c
blob7dc229d8b247d1950d6678aec86ba1434c4ac739
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_id_private.h>
21 #include <isl/set.h>
22 #include <isl_seq.h>
23 #include <isl_stream_private.h>
24 #include <isl/obj.h>
25 #include "isl_polynomial_private.h"
26 #include <isl/union_set.h>
27 #include <isl/union_map.h>
28 #include <isl_mat_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_vec_private.h>
31 #include <isl/list.h>
32 #include <isl_val_private.h>
34 struct variable {
35 char *name;
36 int pos;
37 struct variable *next;
40 struct vars {
41 struct isl_ctx *ctx;
42 int n;
43 struct variable *v;
46 static struct vars *vars_new(struct isl_ctx *ctx)
48 struct vars *v;
49 v = isl_alloc_type(ctx, struct vars);
50 if (!v)
51 return NULL;
52 v->ctx = ctx;
53 v->n = 0;
54 v->v = NULL;
55 return v;
58 static void variable_free(struct variable *var)
60 while (var) {
61 struct variable *next = var->next;
62 free(var->name);
63 free(var);
64 var = next;
68 static void vars_free(struct vars *v)
70 if (!v)
71 return;
72 variable_free(v->v);
73 free(v);
76 static void vars_drop(struct vars *v, int n)
78 struct variable *var;
80 if (!v || !v->v)
81 return;
83 v->n -= n;
85 var = v->v;
86 while (--n >= 0) {
87 struct variable *next = var->next;
88 free(var->name);
89 free(var);
90 var = next;
92 v->v = var;
95 static struct variable *variable_new(struct vars *v, const char *name, int len,
96 int pos)
98 struct variable *var;
99 var = isl_calloc_type(v->ctx, struct variable);
100 if (!var)
101 goto error;
102 var->name = strdup(name);
103 var->name[len] = '\0';
104 var->pos = pos;
105 var->next = v->v;
106 return var;
107 error:
108 variable_free(v->v);
109 return NULL;
112 static int vars_pos(struct vars *v, const char *s, int len)
114 int pos;
115 struct variable *q;
117 if (len == -1)
118 len = strlen(s);
119 for (q = v->v; q; q = q->next) {
120 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
121 break;
123 if (q)
124 pos = q->pos;
125 else {
126 pos = v->n;
127 v->v = variable_new(v, s, len, v->n);
128 if (!v->v)
129 return -1;
130 v->n++;
132 return pos;
135 static int vars_add_anon(struct vars *v)
137 v->v = variable_new(v, "", 0, v->n);
139 if (!v->v)
140 return -1;
141 v->n++;
143 return 0;
146 /* Obtain next token, with some preprocessing.
147 * In particular, evaluate expressions of the form x^y,
148 * with x and y values.
150 static struct isl_token *next_token(__isl_keep isl_stream *s)
152 struct isl_token *tok, *tok2;
154 tok = isl_stream_next_token(s);
155 if (!tok || tok->type != ISL_TOKEN_VALUE)
156 return tok;
157 if (!isl_stream_eat_if_available(s, '^'))
158 return tok;
159 tok2 = isl_stream_next_token(s);
160 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
161 isl_stream_error(s, tok2, "expecting constant value");
162 goto error;
165 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
167 isl_token_free(tok2);
168 return tok;
169 error:
170 isl_token_free(tok);
171 isl_token_free(tok2);
172 return NULL;
175 /* Read an isl_val from "s".
177 * The following token sequences are recognized
179 * "infty" -> infty
180 * "-" "infty" -> -infty
181 * "NaN" -> NaN
182 * n "/" d -> n/d
183 * v -> v
185 * where n, d and v are integer constants.
187 __isl_give isl_val *isl_stream_read_val(__isl_keep isl_stream *s)
189 struct isl_token *tok = NULL;
190 struct isl_token *tok2 = NULL;
191 isl_val *val;
193 tok = next_token(s);
194 if (!tok) {
195 isl_stream_error(s, NULL, "unexpected EOF");
196 goto error;
198 if (tok->type == ISL_TOKEN_INFTY) {
199 isl_token_free(tok);
200 return isl_val_infty(s->ctx);
202 if (tok->type == '-' &&
203 isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
204 isl_token_free(tok);
205 return isl_val_neginfty(s->ctx);
207 if (tok->type == ISL_TOKEN_NAN) {
208 isl_token_free(tok);
209 return isl_val_nan(s->ctx);
211 if (tok->type != ISL_TOKEN_VALUE) {
212 isl_stream_error(s, tok, "expecting value");
213 goto error;
216 if (isl_stream_eat_if_available(s, '/')) {
217 tok2 = next_token(s);
218 if (!tok2) {
219 isl_stream_error(s, NULL, "unexpected EOF");
220 goto error;
222 if (tok2->type != ISL_TOKEN_VALUE) {
223 isl_stream_error(s, tok2, "expecting value");
224 goto error;
226 val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
227 val = isl_val_normalize(val);
228 } else {
229 val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
232 isl_token_free(tok);
233 isl_token_free(tok2);
234 return val;
235 error:
236 isl_token_free(tok);
237 isl_token_free(tok2);
238 return NULL;
241 /* Read an isl_val from "str".
243 struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
244 const char *str)
246 isl_val *val;
247 isl_stream *s = isl_stream_new_str(ctx, str);
248 if (!s)
249 return NULL;
250 val = isl_stream_read_val(s);
251 isl_stream_free(s);
252 return val;
255 static int accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
257 struct isl_token *tok;
259 tok = next_token(s);
260 if (!tok || tok->type != ISL_TOKEN_VALUE) {
261 isl_stream_error(s, tok, "expecting constant value");
262 goto error;
265 isl_int_mul(*f, *f, tok->u.v);
267 isl_token_free(tok);
269 if (isl_stream_eat_if_available(s, '*'))
270 return accept_cst_factor(s, f);
272 return 0;
273 error:
274 isl_token_free(tok);
275 return -1;
278 /* Given an affine expression aff, return an affine expression
279 * for aff % d, with d the next token on the stream, which is
280 * assumed to be a constant.
282 * We introduce an integer division q = [aff/d] and the result
283 * is set to aff - d q.
285 static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
286 struct vars *v, __isl_take isl_pw_aff *aff)
288 struct isl_token *tok;
289 isl_pw_aff *q;
291 tok = next_token(s);
292 if (!tok || tok->type != ISL_TOKEN_VALUE) {
293 isl_stream_error(s, tok, "expecting constant value");
294 goto error;
297 q = isl_pw_aff_copy(aff);
298 q = isl_pw_aff_scale_down(q, tok->u.v);
299 q = isl_pw_aff_floor(q);
300 q = isl_pw_aff_scale(q, tok->u.v);
302 aff = isl_pw_aff_sub(aff, q);
304 isl_token_free(tok);
305 return aff;
306 error:
307 isl_pw_aff_free(aff);
308 isl_token_free(tok);
309 return NULL;
312 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
313 __isl_take isl_space *space, struct vars *v);
314 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
315 __isl_take isl_space *dim, struct vars *v);
317 static __isl_give isl_pw_aff *accept_minmax(__isl_keep isl_stream *s,
318 __isl_take isl_space *dim, struct vars *v)
320 struct isl_token *tok;
321 isl_pw_aff_list *list = NULL;
322 int min;
324 tok = isl_stream_next_token(s);
325 if (!tok)
326 goto error;
327 min = tok->type == ISL_TOKEN_MIN;
328 isl_token_free(tok);
330 if (isl_stream_eat(s, '('))
331 goto error;
333 list = accept_affine_list(s, isl_space_copy(dim), v);
334 if (!list)
335 goto error;
337 if (isl_stream_eat(s, ')'))
338 goto error;
340 isl_space_free(dim);
341 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
342 error:
343 isl_space_free(dim);
344 isl_pw_aff_list_free(list);
345 return NULL;
348 /* Is "tok" the start of an integer division?
350 static int is_start_of_div(struct isl_token *tok)
352 if (!tok)
353 return 0;
354 if (tok->type == '[')
355 return 1;
356 if (tok->type == ISL_TOKEN_FLOOR)
357 return 1;
358 if (tok->type == ISL_TOKEN_CEIL)
359 return 1;
360 if (tok->type == ISL_TOKEN_FLOORD)
361 return 1;
362 if (tok->type == ISL_TOKEN_CEILD)
363 return 1;
364 return 0;
367 /* Read an integer division from "s" and return it as an isl_pw_aff.
369 * The integer division can be of the form
371 * [<affine expression>]
372 * floor(<affine expression>)
373 * ceil(<affine expression>)
374 * floord(<affine expression>,<denominator>)
375 * ceild(<affine expression>,<denominator>)
377 static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
378 __isl_take isl_space *dim, struct vars *v)
380 struct isl_token *tok;
381 int f = 0;
382 int c = 0;
383 int extra = 0;
384 isl_pw_aff *pwaff = NULL;
386 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
387 extra = f = 1;
388 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
389 extra = c = 1;
390 else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
391 f = 1;
392 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
393 c = 1;
394 if (f || c) {
395 if (isl_stream_eat(s, '('))
396 goto error;
397 } else {
398 if (isl_stream_eat(s, '['))
399 goto error;
402 pwaff = accept_affine(s, isl_space_copy(dim), v);
404 if (extra) {
405 if (isl_stream_eat(s, ','))
406 goto error;
408 tok = next_token(s);
409 if (!tok)
410 goto error;
411 if (tok->type != ISL_TOKEN_VALUE) {
412 isl_stream_error(s, tok, "expected denominator");
413 isl_stream_push_token(s, tok);
414 goto error;
416 pwaff = isl_pw_aff_scale_down(pwaff, tok->u.v);
417 isl_token_free(tok);
420 if (c)
421 pwaff = isl_pw_aff_ceil(pwaff);
422 else
423 pwaff = isl_pw_aff_floor(pwaff);
425 if (f || c) {
426 if (isl_stream_eat(s, ')'))
427 goto error;
428 } else {
429 if (isl_stream_eat(s, ']'))
430 goto error;
433 isl_space_free(dim);
434 return pwaff;
435 error:
436 isl_space_free(dim);
437 isl_pw_aff_free(pwaff);
438 return NULL;
441 static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
442 __isl_take isl_space *dim, struct vars *v)
444 struct isl_token *tok = NULL;
445 isl_pw_aff *res = NULL;
447 tok = next_token(s);
448 if (!tok) {
449 isl_stream_error(s, NULL, "unexpected EOF");
450 goto error;
453 if (tok->type == ISL_TOKEN_AFF) {
454 res = isl_pw_aff_copy(tok->u.pwaff);
455 isl_token_free(tok);
456 } else if (tok->type == ISL_TOKEN_IDENT) {
457 int n = v->n;
458 int pos = vars_pos(v, tok->u.s, -1);
459 isl_aff *aff;
461 if (pos < 0)
462 goto error;
463 if (pos >= n) {
464 vars_drop(v, v->n - n);
465 isl_stream_error(s, tok, "unknown identifier");
466 goto error;
469 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
470 if (!aff)
471 goto error;
472 isl_int_set_si(aff->v->el[2 + pos], 1);
473 res = isl_pw_aff_from_aff(aff);
474 isl_token_free(tok);
475 } else if (tok->type == ISL_TOKEN_VALUE) {
476 if (isl_stream_eat_if_available(s, '*')) {
477 res = accept_affine_factor(s, isl_space_copy(dim), v);
478 res = isl_pw_aff_scale(res, tok->u.v);
479 } else {
480 isl_local_space *ls;
481 isl_aff *aff;
482 ls = isl_local_space_from_space(isl_space_copy(dim));
483 aff = isl_aff_zero_on_domain(ls);
484 aff = isl_aff_add_constant(aff, tok->u.v);
485 res = isl_pw_aff_from_aff(aff);
487 isl_token_free(tok);
488 } else if (tok->type == '(') {
489 isl_token_free(tok);
490 tok = NULL;
491 res = accept_affine(s, isl_space_copy(dim), v);
492 if (!res)
493 goto error;
494 if (isl_stream_eat(s, ')'))
495 goto error;
496 } else if (is_start_of_div(tok)) {
497 isl_stream_push_token(s, tok);
498 tok = NULL;
499 res = accept_div(s, isl_space_copy(dim), v);
500 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
501 isl_stream_push_token(s, tok);
502 tok = NULL;
503 res = accept_minmax(s, isl_space_copy(dim), v);
504 } else {
505 isl_stream_error(s, tok, "expecting factor");
506 goto error;
508 if (isl_stream_eat_if_available(s, '%') ||
509 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
510 isl_space_free(dim);
511 return affine_mod(s, v, res);
513 if (isl_stream_eat_if_available(s, '*')) {
514 isl_int f;
515 isl_int_init(f);
516 isl_int_set_si(f, 1);
517 if (accept_cst_factor(s, &f) < 0) {
518 isl_int_clear(f);
519 goto error2;
521 res = isl_pw_aff_scale(res, f);
522 isl_int_clear(f);
524 if (isl_stream_eat_if_available(s, '/')) {
525 isl_int f;
526 isl_int_init(f);
527 isl_int_set_si(f, 1);
528 if (accept_cst_factor(s, &f) < 0) {
529 isl_int_clear(f);
530 goto error2;
532 res = isl_pw_aff_scale_down(res, f);
533 isl_int_clear(f);
536 isl_space_free(dim);
537 return res;
538 error:
539 isl_token_free(tok);
540 error2:
541 isl_pw_aff_free(res);
542 isl_space_free(dim);
543 return NULL;
546 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
548 isl_aff *aff;
549 isl_space *space;
551 space = isl_pw_aff_get_domain_space(pwaff);
552 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
553 aff = isl_aff_add_constant(aff, v);
555 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
558 /* Return a piecewise affine expression defined on the specified domain
559 * that represents NaN.
561 static __isl_give isl_pw_aff *nan_on_domain(__isl_keep isl_space *space)
563 isl_local_space *ls;
565 ls = isl_local_space_from_space(isl_space_copy(space));
566 return isl_pw_aff_nan_on_domain(ls);
569 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
570 __isl_take isl_space *space, struct vars *v)
572 struct isl_token *tok = NULL;
573 isl_local_space *ls;
574 isl_pw_aff *res;
575 int sign = 1;
577 ls = isl_local_space_from_space(isl_space_copy(space));
578 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
579 if (!res)
580 goto error;
582 for (;;) {
583 tok = next_token(s);
584 if (!tok) {
585 isl_stream_error(s, NULL, "unexpected EOF");
586 goto error;
588 if (tok->type == '-') {
589 sign = -sign;
590 isl_token_free(tok);
591 continue;
593 if (tok->type == '(' || is_start_of_div(tok) ||
594 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
595 tok->type == ISL_TOKEN_IDENT ||
596 tok->type == ISL_TOKEN_AFF) {
597 isl_pw_aff *term;
598 isl_stream_push_token(s, tok);
599 tok = NULL;
600 term = accept_affine_factor(s,
601 isl_space_copy(space), v);
602 if (sign < 0)
603 res = isl_pw_aff_sub(res, term);
604 else
605 res = isl_pw_aff_add(res, term);
606 if (!res)
607 goto error;
608 sign = 1;
609 } else if (tok->type == ISL_TOKEN_VALUE) {
610 if (sign < 0)
611 isl_int_neg(tok->u.v, tok->u.v);
612 if (isl_stream_eat_if_available(s, '*') ||
613 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
614 isl_pw_aff *term;
615 term = accept_affine_factor(s,
616 isl_space_copy(space), v);
617 term = isl_pw_aff_scale(term, tok->u.v);
618 res = isl_pw_aff_add(res, term);
619 if (!res)
620 goto error;
621 } else {
622 res = add_cst(res, tok->u.v);
624 sign = 1;
625 } else if (tok->type == ISL_TOKEN_NAN) {
626 res = isl_pw_aff_add(res, nan_on_domain(space));
627 } else {
628 isl_stream_error(s, tok, "unexpected isl_token");
629 isl_stream_push_token(s, tok);
630 isl_pw_aff_free(res);
631 isl_space_free(space);
632 return NULL;
634 isl_token_free(tok);
636 tok = next_token(s);
637 if (tok && tok->type == '-') {
638 sign = -sign;
639 isl_token_free(tok);
640 } else if (tok && tok->type == '+') {
641 /* nothing */
642 isl_token_free(tok);
643 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
644 isl_int_is_neg(tok->u.v)) {
645 isl_stream_push_token(s, tok);
646 } else {
647 if (tok)
648 isl_stream_push_token(s, tok);
649 break;
653 isl_space_free(space);
654 return res;
655 error:
656 isl_space_free(space);
657 isl_token_free(tok);
658 isl_pw_aff_free(res);
659 return NULL;
662 /* Is "type" the type of a comparison operator between lists
663 * of affine expressions?
665 static int is_list_comparator_type(int type)
667 switch (type) {
668 case ISL_TOKEN_LEX_LT:
669 case ISL_TOKEN_LEX_GT:
670 case ISL_TOKEN_LEX_LE:
671 case ISL_TOKEN_LEX_GE:
672 return 1;
673 default:
674 return 0;
678 static int is_comparator(struct isl_token *tok)
680 if (!tok)
681 return 0;
682 if (is_list_comparator_type(tok->type))
683 return 1;
685 switch (tok->type) {
686 case ISL_TOKEN_LT:
687 case ISL_TOKEN_GT:
688 case ISL_TOKEN_LE:
689 case ISL_TOKEN_GE:
690 case ISL_TOKEN_NE:
691 case '=':
692 return 1;
693 default:
694 return 0;
698 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
699 struct vars *v, __isl_take isl_map *map, int rational);
700 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
701 __isl_take isl_space *dim, struct vars *v, int rational);
703 /* Accept a ternary operator, given the first argument.
705 static __isl_give isl_pw_aff *accept_ternary(__isl_keep isl_stream *s,
706 __isl_take isl_map *cond, struct vars *v, int rational)
708 isl_space *dim;
709 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
711 if (!cond)
712 return NULL;
714 if (isl_stream_eat(s, '?'))
715 goto error;
717 dim = isl_space_wrap(isl_map_get_space(cond));
718 pwaff1 = accept_extended_affine(s, dim, v, rational);
719 if (!pwaff1)
720 goto error;
722 if (isl_stream_eat(s, ':'))
723 goto error;
725 dim = isl_pw_aff_get_domain_space(pwaff1);
726 pwaff2 = accept_extended_affine(s, dim, v, rational);
727 if (!pwaff1)
728 goto error;
730 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
731 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
732 error:
733 isl_map_free(cond);
734 isl_pw_aff_free(pwaff1);
735 isl_pw_aff_free(pwaff2);
736 return NULL;
739 /* Set *line and *col to those of the next token, if any.
741 static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
743 struct isl_token *tok;
745 tok = isl_stream_next_token(s);
746 if (!tok)
747 return;
749 *line = tok->line;
750 *col = tok->col;
751 isl_stream_push_token(s, tok);
754 /* Push a token encapsulating "pa" onto "s", with the given
755 * line and column.
757 static int push_aff(__isl_keep isl_stream *s, int line, int col,
758 __isl_take isl_pw_aff *pa)
760 struct isl_token *tok;
762 tok = isl_token_new(s->ctx, line, col, 0);
763 if (!tok)
764 goto error;
765 tok->type = ISL_TOKEN_AFF;
766 tok->u.pwaff = pa;
767 isl_stream_push_token(s, tok);
769 return 0;
770 error:
771 isl_pw_aff_free(pa);
772 return -1;
775 /* Accept an affine expression that may involve ternary operators.
776 * We first read an affine expression.
777 * If it is not followed by a comparison operator, we simply return it.
778 * Otherwise, we assume the affine expression is part of the first
779 * argument of a ternary operator and try to parse that.
781 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
782 __isl_take isl_space *dim, struct vars *v, int rational)
784 isl_space *space;
785 isl_map *cond;
786 isl_pw_aff *pwaff;
787 struct isl_token *tok;
788 int line = -1, col = -1;
789 int is_comp;
791 set_current_line_col(s, &line, &col);
793 pwaff = accept_affine(s, dim, v);
794 if (rational)
795 pwaff = isl_pw_aff_set_rational(pwaff);
796 if (!pwaff)
797 return NULL;
799 tok = isl_stream_next_token(s);
800 if (!tok)
801 return isl_pw_aff_free(pwaff);
803 is_comp = is_comparator(tok);
804 isl_stream_push_token(s, tok);
805 if (!is_comp)
806 return pwaff;
808 space = isl_pw_aff_get_domain_space(pwaff);
809 cond = isl_map_universe(isl_space_unwrap(space));
811 if (push_aff(s, line, col, pwaff) < 0)
812 cond = isl_map_free(cond);
813 if (!cond)
814 return NULL;
816 cond = read_formula(s, v, cond, rational);
818 return accept_ternary(s, cond, v, rational);
821 static __isl_give isl_map *read_var_def(__isl_keep isl_stream *s,
822 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
823 int rational)
825 isl_pw_aff *def;
826 isl_size pos;
827 isl_map *def_map;
829 if (type == isl_dim_param)
830 pos = isl_map_dim(map, isl_dim_param);
831 else {
832 pos = isl_map_dim(map, isl_dim_in);
833 if (type == isl_dim_out) {
834 isl_size n_out = isl_map_dim(map, isl_dim_out);
835 if (pos < 0 || n_out < 0)
836 return isl_map_free(map);
837 pos += n_out;
839 type = isl_dim_in;
841 if (pos < 0)
842 return isl_map_free(map);
843 --pos;
845 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
846 v, rational);
847 def_map = isl_map_from_pw_aff(def);
848 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
849 def_map = isl_set_unwrap(isl_map_domain(def_map));
851 map = isl_map_intersect(map, def_map);
853 return map;
856 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
857 __isl_take isl_space *dim, struct vars *v)
859 isl_pw_aff *pwaff;
860 isl_pw_aff_list *list;
861 struct isl_token *tok = NULL;
863 pwaff = accept_affine(s, isl_space_copy(dim), v);
864 list = isl_pw_aff_list_from_pw_aff(pwaff);
865 if (!list)
866 goto error;
868 for (;;) {
869 tok = isl_stream_next_token(s);
870 if (!tok) {
871 isl_stream_error(s, NULL, "unexpected EOF");
872 goto error;
874 if (tok->type != ',') {
875 isl_stream_push_token(s, tok);
876 break;
878 isl_token_free(tok);
880 pwaff = accept_affine(s, isl_space_copy(dim), v);
881 list = isl_pw_aff_list_concat(list,
882 isl_pw_aff_list_from_pw_aff(pwaff));
883 if (!list)
884 goto error;
887 isl_space_free(dim);
888 return list;
889 error:
890 isl_space_free(dim);
891 isl_pw_aff_list_free(list);
892 return NULL;
895 static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
896 struct vars *v, __isl_take isl_map *map, int rational)
898 struct isl_token *tok;
900 while ((tok = isl_stream_next_token(s)) != NULL) {
901 int p;
902 int n = v->n;
904 if (tok->type != ISL_TOKEN_IDENT)
905 break;
907 p = vars_pos(v, tok->u.s, -1);
908 if (p < 0)
909 goto error;
910 if (p < n) {
911 isl_stream_error(s, tok, "expecting unique identifier");
912 goto error;
915 map = isl_map_add_dims(map, isl_dim_out, 1);
917 isl_token_free(tok);
918 tok = isl_stream_next_token(s);
919 if (tok && tok->type == '=') {
920 isl_token_free(tok);
921 map = read_var_def(s, map, isl_dim_out, v, rational);
922 tok = isl_stream_next_token(s);
925 if (!tok || tok->type != ',')
926 break;
928 isl_token_free(tok);
930 if (tok)
931 isl_stream_push_token(s, tok);
933 return map;
934 error:
935 isl_token_free(tok);
936 isl_map_free(map);
937 return NULL;
940 static int next_is_tuple(__isl_keep isl_stream *s)
942 struct isl_token *tok;
943 int is_tuple;
945 tok = isl_stream_next_token(s);
946 if (!tok)
947 return 0;
948 if (tok->type == '[') {
949 isl_stream_push_token(s, tok);
950 return 1;
952 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
953 isl_stream_push_token(s, tok);
954 return 0;
957 is_tuple = isl_stream_next_token_is(s, '[');
959 isl_stream_push_token(s, tok);
961 return is_tuple;
964 /* Is "pa" an expression in term of earlier dimensions?
965 * The alternative is that the dimension is defined to be equal to itself,
966 * meaning that it has a universe domain and an expression that depends
967 * on itself. "i" is the position of the expression in a sequence
968 * of "n" expressions. The final dimensions of "pa" correspond to
969 * these "n" expressions.
971 static isl_bool pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
973 isl_aff *aff;
975 if (!pa)
976 return isl_bool_error;
977 if (pa->n != 1)
978 return isl_bool_true;
979 if (!isl_set_plain_is_universe(pa->p[0].set))
980 return isl_bool_true;
982 aff = pa->p[0].aff;
983 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
984 return isl_bool_true;
985 return isl_bool_false;
988 /* Does the tuple contain any dimensions that are defined
989 * in terms of earlier dimensions?
991 static isl_bool tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
993 int i;
994 isl_size n;
995 isl_bool has_expr = isl_bool_false;
996 isl_pw_aff *pa;
998 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
999 if (n < 0)
1000 return isl_bool_error;
1001 for (i = 0; i < n; ++i) {
1002 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1003 has_expr = pw_aff_is_expr(pa, i, n);
1004 isl_pw_aff_free(pa);
1005 if (has_expr < 0 || has_expr)
1006 break;
1009 return has_expr;
1012 /* Set the name of dimension "pos" in "space" to "name".
1013 * During printing, we add primes if the same name appears more than once
1014 * to distinguish the occurrences. Here, we remove those primes from "name"
1015 * before setting the name of the dimension.
1017 static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
1018 int pos, char *name)
1020 char *prime;
1022 if (!name)
1023 return space;
1025 prime = strchr(name, '\'');
1026 if (prime)
1027 *prime = '\0';
1028 space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1029 if (prime)
1030 *prime = '\'';
1032 return space;
1035 /* Accept a piecewise affine expression.
1037 * At the outer level, the piecewise affine expression may be of the form
1039 * aff1 : condition1; aff2 : conditions2; ...
1041 * or simply
1043 * aff
1045 * each of the affine expressions may in turn include ternary operators.
1047 * There may be parentheses around some subexpression of "aff1"
1048 * around "aff1" itself, around "aff1 : condition1" and/or
1049 * around the entire piecewise affine expression.
1050 * We therefore remove the opening parenthesis (if any) from the stream
1051 * in case the closing parenthesis follows the colon, but if the closing
1052 * parenthesis is the first thing in the stream after the parsed affine
1053 * expression, we push the parsed expression onto the stream and parse
1054 * again in case the parentheses enclose some subexpression of "aff1".
1056 static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1057 __isl_take isl_space *space, struct vars *v, int rational)
1059 isl_pw_aff *res;
1060 isl_space *res_space;
1062 res_space = isl_space_from_domain(isl_space_copy(space));
1063 res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1064 res = isl_pw_aff_empty(res_space);
1065 do {
1066 isl_pw_aff *pa;
1067 int seen_paren;
1068 int line = -1, col = -1;
1070 set_current_line_col(s, &line, &col);
1071 seen_paren = isl_stream_eat_if_available(s, '(');
1072 if (seen_paren)
1073 pa = accept_piecewise_affine(s, isl_space_copy(space),
1074 v, rational);
1075 else
1076 pa = accept_extended_affine(s, isl_space_copy(space),
1077 v, rational);
1078 if (seen_paren && isl_stream_eat_if_available(s, ')')) {
1079 seen_paren = 0;
1080 if (push_aff(s, line, col, pa) < 0)
1081 goto error;
1082 pa = accept_extended_affine(s, isl_space_copy(space),
1083 v, rational);
1085 if (isl_stream_eat_if_available(s, ':')) {
1086 isl_space *dom_space;
1087 isl_set *dom;
1089 dom_space = isl_pw_aff_get_domain_space(pa);
1090 dom = isl_set_universe(dom_space);
1091 dom = read_formula(s, v, dom, rational);
1092 pa = isl_pw_aff_intersect_domain(pa, dom);
1095 res = isl_pw_aff_union_add(res, pa);
1097 if (seen_paren && isl_stream_eat(s, ')'))
1098 goto error;
1099 } while (isl_stream_eat_if_available(s, ';'));
1101 isl_space_free(space);
1103 return res;
1104 error:
1105 isl_space_free(space);
1106 return isl_pw_aff_free(res);
1109 /* Read an affine expression from "s" for use in read_tuple.
1111 * accept_extended_affine requires a wrapped space as input.
1112 * read_tuple on the other hand expects each isl_pw_aff
1113 * to have an anonymous space. We therefore adjust the space
1114 * of the isl_pw_aff before returning it.
1116 static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1117 struct vars *v, int rational)
1119 isl_space *space;
1120 isl_pw_aff *def;
1122 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1124 def = accept_piecewise_affine(s, space, v, rational);
1126 space = isl_space_set_alloc(s->ctx, 0, v->n);
1127 def = isl_pw_aff_reset_domain_space(def, space);
1129 return def;
1132 /* Read a list of tuple elements by calling "read_el" on each of them and
1133 * return a space with the same number of set dimensions derived from
1134 * the parameter space "space" and possibly updated by "read_el".
1135 * The elements in the list are separated by either "," or "][".
1136 * If "comma" is set then only "," is allowed.
1138 static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1139 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1140 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1141 struct vars *v, __isl_take isl_space *space, int rational,
1142 void *user),
1143 void *user)
1145 if (!space)
1146 return NULL;
1148 space = isl_space_set_from_params(space);
1150 if (isl_stream_next_token_is(s, ']'))
1151 return space;
1153 for (;;) {
1154 struct isl_token *tok;
1156 space = isl_space_add_dims(space, isl_dim_set, 1);
1158 space = read_el(s, v, space, rational, user);
1159 if (!space)
1160 return NULL;
1162 tok = isl_stream_next_token(s);
1163 if (!comma && tok && tok->type == ']' &&
1164 isl_stream_next_token_is(s, '[')) {
1165 isl_token_free(tok);
1166 tok = isl_stream_next_token(s);
1167 } else if (!tok || tok->type != ',') {
1168 if (tok)
1169 isl_stream_push_token(s, tok);
1170 break;
1173 isl_token_free(tok);
1176 return space;
1179 /* Read a tuple space from "s" derived from the parameter space "space".
1180 * Call "read_el" on each element in the tuples.
1182 static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1183 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1184 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1185 struct vars *v, __isl_take isl_space *space, int rational,
1186 void *user),
1187 void *user)
1189 struct isl_token *tok;
1190 char *name = NULL;
1191 isl_space *res = NULL;
1193 tok = isl_stream_next_token(s);
1194 if (!tok)
1195 goto error;
1196 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1197 name = strdup(tok->u.s);
1198 isl_token_free(tok);
1199 if (!name)
1200 goto error;
1201 } else
1202 isl_stream_push_token(s, tok);
1203 if (isl_stream_eat(s, '['))
1204 goto error;
1205 if (next_is_tuple(s)) {
1206 isl_space *out;
1207 res = read_tuple_space(s, v, isl_space_copy(space),
1208 rational, comma, read_el, user);
1209 if (isl_stream_eat(s, ISL_TOKEN_TO))
1210 goto error;
1211 out = read_tuple_space(s, v, isl_space_copy(space),
1212 rational, comma, read_el, user);
1213 res = isl_space_product(res, out);
1214 } else
1215 res = read_tuple_list(s, v, isl_space_copy(space),
1216 rational, comma, read_el, user);
1217 if (isl_stream_eat(s, ']'))
1218 goto error;
1220 if (name) {
1221 res = isl_space_set_tuple_name(res, isl_dim_set, name);
1222 free(name);
1225 isl_space_free(space);
1226 return res;
1227 error:
1228 free(name);
1229 isl_space_free(res);
1230 isl_space_free(space);
1231 return NULL;
1234 /* Construct an isl_pw_aff defined on a space with v->n variables
1235 * that is equal to the last of those variables.
1237 static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1239 isl_space *space;
1240 isl_aff *aff;
1242 space = isl_space_set_alloc(v->ctx, 0, v->n);
1243 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1244 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1245 return isl_pw_aff_from_aff(aff);
1248 /* This function is called for each element in a tuple inside read_tuple.
1249 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1250 * over a space containing all variables in "v" defined so far.
1251 * The isl_pw_aff expresses the new variable in terms of earlier variables
1252 * if a definition is provided. Otherwise, it is represented as being
1253 * equal to itself.
1254 * Add the isl_pw_aff to *list.
1255 * If the new variable was named, then adjust "space" accordingly and
1256 * return the updated space.
1258 static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1259 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1261 isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1262 isl_pw_aff *pa;
1263 struct isl_token *tok;
1264 int new_name = 0;
1266 tok = next_token(s);
1267 if (!tok) {
1268 isl_stream_error(s, NULL, "unexpected EOF");
1269 return isl_space_free(space);
1272 if (tok->type == ISL_TOKEN_IDENT) {
1273 int n = v->n;
1274 int p = vars_pos(v, tok->u.s, -1);
1275 if (p < 0)
1276 goto error;
1277 new_name = p >= n;
1280 if (tok->type == '*') {
1281 if (vars_add_anon(v) < 0)
1282 goto error;
1283 isl_token_free(tok);
1284 pa = identity_tuple_el(v);
1285 } else if (new_name) {
1286 isl_size pos = isl_space_dim(space, isl_dim_out);
1287 if (pos < 0)
1288 goto error;
1289 pos -= 1;
1290 space = space_set_dim_name(space, pos, v->v->name);
1291 isl_token_free(tok);
1292 if (isl_stream_eat_if_available(s, '='))
1293 pa = read_tuple_var_def(s, v, rational);
1294 else
1295 pa = identity_tuple_el(v);
1296 } else {
1297 isl_stream_push_token(s, tok);
1298 tok = NULL;
1299 if (vars_add_anon(v) < 0)
1300 goto error;
1301 pa = read_tuple_var_def(s, v, rational);
1304 *list = isl_pw_aff_list_add(*list, pa);
1305 if (!*list)
1306 return isl_space_free(space);
1308 return space;
1309 error:
1310 isl_token_free(tok);
1311 return isl_space_free(space);
1314 /* Read a tuple and represent it as an isl_multi_pw_aff.
1315 * The range space of the isl_multi_pw_aff is the space of the tuple.
1316 * The domain space is an anonymous space
1317 * with a dimension for each variable in the set of variables in "v",
1318 * including the variables in the range.
1319 * If a given dimension is not defined in terms of earlier dimensions in
1320 * the input, then the corresponding isl_pw_aff is set equal to one time
1321 * the variable corresponding to the dimension being defined.
1323 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1324 * Each element in this list is defined over a space representing
1325 * the variables defined so far. We need to adjust the earlier
1326 * elements to have as many variables in the domain as the final
1327 * element in the list.
1329 static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1330 struct vars *v, int rational, int comma)
1332 int i;
1333 isl_size n;
1334 isl_space *space;
1335 isl_pw_aff_list *list;
1337 space = isl_space_params_alloc(v->ctx, 0);
1338 list = isl_pw_aff_list_alloc(s->ctx, 0);
1339 space = read_tuple_space(s, v, space, rational, comma,
1340 &read_tuple_pw_aff_el, &list);
1341 n = isl_space_dim(space, isl_dim_set);
1342 if (n < 0)
1343 space = isl_space_free(space);
1344 for (i = 0; i + 1 < n; ++i) {
1345 isl_pw_aff *pa;
1347 pa = isl_pw_aff_list_get_pw_aff(list, i);
1348 pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1349 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1352 space = isl_space_from_range(space);
1353 space = isl_space_add_dims(space, isl_dim_in, v->n);
1354 return isl_multi_pw_aff_from_pw_aff_list(space, list);
1357 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1358 * We first create the appropriate space in "map" based on the range
1359 * space of this isl_multi_pw_aff. Then, we add equalities based
1360 * on the affine expressions. These live in an anonymous space,
1361 * however, so we first need to reset the space to that of "map".
1363 static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1364 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1365 int rational)
1367 int i;
1368 isl_size n;
1369 isl_ctx *ctx;
1370 isl_space *space = NULL;
1372 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1373 if (!map || n < 0)
1374 goto error;
1375 ctx = isl_multi_pw_aff_get_ctx(tuple);
1376 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1377 if (!space)
1378 goto error;
1380 if (type == isl_dim_param) {
1381 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1382 isl_space_is_wrapping(space)) {
1383 isl_die(ctx, isl_error_invalid,
1384 "parameter tuples cannot be named or nested",
1385 goto error);
1387 map = isl_map_add_dims(map, type, n);
1388 for (i = 0; i < n; ++i) {
1389 isl_id *id;
1390 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1391 isl_die(ctx, isl_error_invalid,
1392 "parameters must be named",
1393 goto error);
1394 id = isl_space_get_dim_id(space, isl_dim_set, i);
1395 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1397 } else if (type == isl_dim_in) {
1398 isl_set *set;
1400 set = isl_set_universe(isl_space_copy(space));
1401 if (rational)
1402 set = isl_set_set_rational(set);
1403 set = isl_set_intersect_params(set, isl_map_params(map));
1404 map = isl_map_from_domain(set);
1405 } else {
1406 isl_set *set;
1408 set = isl_set_universe(isl_space_copy(space));
1409 if (rational)
1410 set = isl_set_set_rational(set);
1411 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1414 for (i = 0; i < n; ++i) {
1415 isl_pw_aff *pa;
1416 isl_space *space;
1417 isl_aff *aff;
1418 isl_set *set;
1419 isl_map *map_i;
1421 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1422 space = isl_pw_aff_get_domain_space(pa);
1423 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1424 aff = isl_aff_add_coefficient_si(aff,
1425 isl_dim_in, v->n - n + i, -1);
1426 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1427 if (rational)
1428 pa = isl_pw_aff_set_rational(pa);
1429 set = isl_pw_aff_zero_set(pa);
1430 map_i = isl_map_from_range(set);
1431 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1432 map = isl_map_intersect(map, map_i);
1435 isl_space_free(space);
1436 isl_multi_pw_aff_free(tuple);
1437 return map;
1438 error:
1439 isl_space_free(space);
1440 isl_multi_pw_aff_free(tuple);
1441 isl_map_free(map);
1442 return NULL;
1445 /* Read a tuple from "s" and add it to "map".
1446 * The tuple is initially represented as an isl_multi_pw_aff and
1447 * then added to "map".
1449 static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1450 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1451 int rational, int comma)
1453 isl_multi_pw_aff *tuple;
1455 tuple = read_tuple(s, v, rational, comma);
1456 if (!tuple)
1457 return isl_map_free(map);
1459 return map_from_tuple(tuple, map, type, v, rational);
1462 /* Given two equal-length lists of piecewise affine expression with the space
1463 * of "set" as domain, construct a set in the same space that expresses
1464 * that "left" and "right" satisfy the comparison "type".
1466 * A space is constructed of the same dimension as the number of elements
1467 * in the two lists. The comparison is then expressed in a map from
1468 * this space to itself and wrapped into a set. Finally the two lists
1469 * of piecewise affine expressions are plugged into this set.
1471 * Let S be the space of "set" and T the constructed space.
1472 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1473 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1474 * while the comparison is first expressed in T -> T, then [T -> T]
1475 * and finally in S.
1477 static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1478 __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1480 isl_space *space;
1481 isl_size n;
1482 isl_multi_pw_aff *mpa1, *mpa2;
1484 n = isl_pw_aff_list_n_pw_aff(left);
1485 if (!set || n < 0 || !right)
1486 goto error;
1488 space = isl_set_get_space(set);
1489 space = isl_space_from_domain(space);
1490 space = isl_space_add_dims(space, isl_dim_out, n);
1491 mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1492 mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1493 mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1495 space = isl_space_range(space);
1496 switch (type) {
1497 case ISL_TOKEN_LEX_LT:
1498 set = isl_map_wrap(isl_map_lex_lt(space));
1499 break;
1500 case ISL_TOKEN_LEX_GT:
1501 set = isl_map_wrap(isl_map_lex_gt(space));
1502 break;
1503 case ISL_TOKEN_LEX_LE:
1504 set = isl_map_wrap(isl_map_lex_le(space));
1505 break;
1506 case ISL_TOKEN_LEX_GE:
1507 set = isl_map_wrap(isl_map_lex_ge(space));
1508 break;
1509 default:
1510 isl_multi_pw_aff_free(mpa1);
1511 isl_space_free(space);
1512 isl_die(isl_set_get_ctx(set), isl_error_internal,
1513 "unhandled list comparison type", return NULL);
1515 set = isl_set_preimage_multi_pw_aff(set, mpa1);
1516 return set;
1517 error:
1518 isl_pw_aff_list_free(left);
1519 isl_pw_aff_list_free(right);
1520 return NULL;
1523 /* Construct constraints of the form
1525 * a op b
1527 * where a is an element in "left", op is an operator of type "type" and
1528 * b is an element in "right", add the constraints to "set" and return
1529 * the result.
1530 * "rational" is set if the constraints should be treated as
1531 * a rational constraints.
1533 * If "type" is the type of a comparison operator between lists
1534 * of affine expressions, then a single (compound) constraint
1535 * is constructed by list_cmp instead.
1537 static __isl_give isl_set *construct_constraints(
1538 __isl_take isl_set *set, int type,
1539 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1540 int rational)
1542 isl_set *cond;
1544 left = isl_pw_aff_list_copy(left);
1545 right = isl_pw_aff_list_copy(right);
1546 if (rational) {
1547 left = isl_pw_aff_list_set_rational(left);
1548 right = isl_pw_aff_list_set_rational(right);
1550 if (is_list_comparator_type(type))
1551 cond = list_cmp(set, type, left, right);
1552 else if (type == ISL_TOKEN_LE)
1553 cond = isl_pw_aff_list_le_set(left, right);
1554 else if (type == ISL_TOKEN_GE)
1555 cond = isl_pw_aff_list_ge_set(left, right);
1556 else if (type == ISL_TOKEN_LT)
1557 cond = isl_pw_aff_list_lt_set(left, right);
1558 else if (type == ISL_TOKEN_GT)
1559 cond = isl_pw_aff_list_gt_set(left, right);
1560 else if (type == ISL_TOKEN_NE)
1561 cond = isl_pw_aff_list_ne_set(left, right);
1562 else
1563 cond = isl_pw_aff_list_eq_set(left, right);
1565 return isl_set_intersect(set, cond);
1568 /* Read a constraint from "s", add it to "map" and return the result.
1569 * "v" contains a description of the identifiers parsed so far.
1570 * "rational" is set if the constraint should be treated as
1571 * a rational constraint.
1572 * The constraint read from "s" may be applied to multiple pairs
1573 * of affine expressions and may be chained.
1574 * In particular, a list of affine expressions is read, followed
1575 * by a comparison operator and another list of affine expressions.
1576 * The comparison operator is then applied to each pair of elements
1577 * in the two lists and the results are added to "map".
1578 * However, if the operator expects two lists of affine expressions,
1579 * then it is applied directly to those lists and the two lists
1580 * are required to have the same length.
1581 * If the next token is another comparison operator, then another
1582 * list of affine expressions is read and the process repeats.
1584 * The processing is performed on a wrapped copy of "map" because
1585 * an affine expression cannot have a binary relation as domain.
1587 static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1588 struct vars *v, __isl_take isl_map *map, int rational)
1590 struct isl_token *tok;
1591 int type;
1592 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1593 isl_size n1, n2;
1594 isl_set *set;
1596 set = isl_map_wrap(map);
1597 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1598 if (!list1)
1599 goto error;
1600 tok = isl_stream_next_token(s);
1601 if (!is_comparator(tok)) {
1602 isl_stream_error(s, tok, "missing operator");
1603 if (tok)
1604 isl_stream_push_token(s, tok);
1605 goto error;
1607 type = tok->type;
1608 isl_token_free(tok);
1609 for (;;) {
1610 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1611 n1 = isl_pw_aff_list_n_pw_aff(list1);
1612 n2 = isl_pw_aff_list_n_pw_aff(list2);
1613 if (n1 < 0 || n2 < 0)
1614 goto error;
1615 if (is_list_comparator_type(type) && n1 != n2) {
1616 isl_stream_error(s, NULL,
1617 "list arguments not of same size");
1618 goto error;
1621 set = construct_constraints(set, type, list1, list2, rational);
1622 isl_pw_aff_list_free(list1);
1623 list1 = list2;
1625 tok = isl_stream_next_token(s);
1626 if (!is_comparator(tok)) {
1627 if (tok)
1628 isl_stream_push_token(s, tok);
1629 break;
1631 type = tok->type;
1632 isl_token_free(tok);
1634 isl_pw_aff_list_free(list1);
1636 return isl_set_unwrap(set);
1637 error:
1638 isl_pw_aff_list_free(list1);
1639 isl_pw_aff_list_free(list2);
1640 isl_set_free(set);
1641 return NULL;
1644 static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
1645 struct vars *v, __isl_take isl_map *map, int rational)
1647 int n = v->n;
1648 int seen_paren = isl_stream_eat_if_available(s, '(');
1650 map = isl_map_from_domain(isl_map_wrap(map));
1651 map = read_defined_var_list(s, v, map, rational);
1653 if (isl_stream_eat(s, ':'))
1654 goto error;
1656 map = read_formula(s, v, map, rational);
1657 map = isl_set_unwrap(isl_map_domain(map));
1659 vars_drop(v, v->n - n);
1660 if (seen_paren && isl_stream_eat(s, ')'))
1661 goto error;
1663 return map;
1664 error:
1665 isl_map_free(map);
1666 return NULL;
1669 /* Parse an expression between parentheses and push the result
1670 * back on the stream.
1672 * The parsed expression may be either an affine expression
1673 * or a condition. The first type is pushed onto the stream
1674 * as an isl_pw_aff, while the second is pushed as an isl_map.
1676 * If the initial token indicates the start of a condition,
1677 * we parse it as such.
1678 * Otherwise, we first parse an affine expression and push
1679 * that onto the stream. If the affine expression covers the
1680 * entire expression between parentheses, we return.
1681 * Otherwise, we assume that the affine expression is the
1682 * start of a condition and continue parsing.
1684 static int resolve_paren_expr(__isl_keep isl_stream *s,
1685 struct vars *v, __isl_take isl_map *map, int rational)
1687 struct isl_token *tok, *tok2;
1688 int line, col;
1689 isl_pw_aff *pwaff;
1691 tok = isl_stream_next_token(s);
1692 if (!tok || tok->type != '(')
1693 goto error;
1695 if (isl_stream_next_token_is(s, '('))
1696 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1697 goto error;
1699 if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1700 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1701 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1702 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1703 isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1704 map = read_formula(s, v, map, rational);
1705 if (isl_stream_eat(s, ')'))
1706 goto error;
1707 tok->type = ISL_TOKEN_MAP;
1708 tok->u.map = map;
1709 isl_stream_push_token(s, tok);
1710 return 0;
1713 tok2 = isl_stream_next_token(s);
1714 if (!tok2)
1715 goto error;
1716 line = tok2->line;
1717 col = tok2->col;
1718 isl_stream_push_token(s, tok2);
1720 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1721 if (!pwaff)
1722 goto error;
1724 tok2 = isl_token_new(s->ctx, line, col, 0);
1725 if (!tok2)
1726 goto error2;
1727 tok2->type = ISL_TOKEN_AFF;
1728 tok2->u.pwaff = pwaff;
1730 if (isl_stream_eat_if_available(s, ')')) {
1731 isl_stream_push_token(s, tok2);
1732 isl_token_free(tok);
1733 isl_map_free(map);
1734 return 0;
1737 isl_stream_push_token(s, tok2);
1739 map = read_formula(s, v, map, rational);
1740 if (isl_stream_eat(s, ')'))
1741 goto error;
1743 tok->type = ISL_TOKEN_MAP;
1744 tok->u.map = map;
1745 isl_stream_push_token(s, tok);
1747 return 0;
1748 error2:
1749 isl_pw_aff_free(pwaff);
1750 error:
1751 isl_token_free(tok);
1752 isl_map_free(map);
1753 return -1;
1756 static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
1757 struct vars *v, __isl_take isl_map *map, int rational)
1759 if (isl_stream_next_token_is(s, '('))
1760 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1761 goto error;
1763 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1764 struct isl_token *tok;
1765 tok = isl_stream_next_token(s);
1766 if (!tok)
1767 goto error;
1768 isl_map_free(map);
1769 map = isl_map_copy(tok->u.map);
1770 isl_token_free(tok);
1771 return map;
1774 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1775 return read_exists(s, v, map, rational);
1777 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1778 return map;
1780 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1781 isl_space *dim = isl_map_get_space(map);
1782 isl_map_free(map);
1783 return isl_map_empty(dim);
1786 return add_constraint(s, v, map, rational);
1787 error:
1788 isl_map_free(map);
1789 return NULL;
1792 static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
1793 struct vars *v, __isl_take isl_map *map, int rational)
1795 isl_map *res;
1796 int negate;
1798 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1799 res = read_conjunct(s, v, isl_map_copy(map), rational);
1800 if (negate)
1801 res = isl_map_subtract(isl_map_copy(map), res);
1803 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
1804 isl_map *res_i;
1806 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1807 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1808 if (negate)
1809 res = isl_map_subtract(res, res_i);
1810 else
1811 res = isl_map_intersect(res, res_i);
1814 isl_map_free(map);
1815 return res;
1818 static struct isl_map *read_disjuncts(__isl_keep isl_stream *s,
1819 struct vars *v, __isl_take isl_map *map, int rational)
1821 isl_map *res;
1823 if (isl_stream_next_token_is(s, '}'))
1824 return map;
1826 res = read_conjuncts(s, v, isl_map_copy(map), rational);
1827 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1828 isl_map *res_i;
1830 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1831 res = isl_map_union(res, res_i);
1834 isl_map_free(map);
1835 return res;
1838 /* Read a first order formula from "s", add the corresponding
1839 * constraints to "map" and return the result.
1841 * In particular, read a formula of the form
1845 * or
1847 * a implies b
1849 * where a and b are disjunctions.
1851 * In the first case, map is replaced by
1853 * map \cap { [..] : a }
1855 * In the second case, it is replaced by
1857 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1859 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
1860 struct vars *v, __isl_take isl_map *map, int rational)
1862 isl_map *res;
1864 res = read_disjuncts(s, v, isl_map_copy(map), rational);
1866 if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
1867 isl_map *res2;
1869 res = isl_map_subtract(isl_map_copy(map), res);
1870 res2 = read_disjuncts(s, v, map, rational);
1871 res = isl_map_union(res, res2);
1872 } else
1873 isl_map_free(map);
1875 return res;
1878 static isl_size polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1880 isl_size n_out, n_in, n_param, n_div;
1882 n_param = isl_basic_map_dim(bmap, isl_dim_param);
1883 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1884 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1885 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1886 if (n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1887 return isl_size_error;
1889 if (pos < n_out)
1890 return 1 + n_param + n_in + pos;
1891 pos -= n_out;
1893 if (pos < n_in)
1894 return 1 + n_param + pos;
1895 pos -= n_in;
1897 if (pos < n_div)
1898 return 1 + n_param + n_in + n_out + pos;
1899 pos -= n_div;
1901 if (pos < n_param)
1902 return 1 + pos;
1904 return 0;
1907 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1908 __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
1910 int j;
1911 struct isl_token *tok;
1912 int type;
1913 int k;
1914 isl_int *c;
1915 isl_size total;
1917 if (!bmap)
1918 return NULL;
1920 tok = isl_stream_next_token(s);
1921 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1922 isl_stream_error(s, tok, "expecting coefficient");
1923 if (tok)
1924 isl_stream_push_token(s, tok);
1925 goto error;
1927 if (!tok->on_new_line) {
1928 isl_stream_error(s, tok, "coefficient should appear on new line");
1929 isl_stream_push_token(s, tok);
1930 goto error;
1933 type = isl_int_get_si(tok->u.v);
1934 isl_token_free(tok);
1936 isl_assert(s->ctx, type == 0 || type == 1, goto error);
1937 if (type == 0) {
1938 k = isl_basic_map_alloc_equality(bmap);
1939 c = bmap->eq[k];
1940 } else {
1941 k = isl_basic_map_alloc_inequality(bmap);
1942 c = bmap->ineq[k];
1944 if (k < 0)
1945 goto error;
1947 total = isl_basic_map_dim(bmap, isl_dim_all);
1948 if (total < 0)
1949 return isl_basic_map_free(bmap);
1950 for (j = 0; j < 1 + total; ++j) {
1951 isl_size pos;
1952 tok = isl_stream_next_token(s);
1953 if (!tok || tok->type != ISL_TOKEN_VALUE) {
1954 isl_stream_error(s, tok, "expecting coefficient");
1955 if (tok)
1956 isl_stream_push_token(s, tok);
1957 goto error;
1959 if (tok->on_new_line) {
1960 isl_stream_error(s, tok,
1961 "coefficient should not appear on new line");
1962 isl_stream_push_token(s, tok);
1963 goto error;
1965 pos = polylib_pos_to_isl_pos(bmap, j);
1966 if (pos >= 0)
1967 isl_int_set(c[pos], tok->u.v);
1968 isl_token_free(tok);
1969 if (pos < 0)
1970 return isl_basic_map_free(bmap);
1973 return bmap;
1974 error:
1975 isl_basic_map_free(bmap);
1976 return NULL;
1979 static __isl_give isl_basic_map *basic_map_read_polylib(
1980 __isl_keep isl_stream *s)
1982 int i;
1983 struct isl_token *tok;
1984 struct isl_token *tok2;
1985 int n_row, n_col;
1986 int on_new_line;
1987 unsigned in = 0, out, local = 0;
1988 struct isl_basic_map *bmap = NULL;
1989 int nparam = 0;
1991 tok = isl_stream_next_token(s);
1992 if (!tok) {
1993 isl_stream_error(s, NULL, "unexpected EOF");
1994 return NULL;
1996 tok2 = isl_stream_next_token(s);
1997 if (!tok2) {
1998 isl_token_free(tok);
1999 isl_stream_error(s, NULL, "unexpected EOF");
2000 return NULL;
2002 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
2003 isl_stream_push_token(s, tok2);
2004 isl_stream_push_token(s, tok);
2005 isl_stream_error(s, NULL,
2006 "expecting constraint matrix dimensions");
2007 return NULL;
2009 n_row = isl_int_get_si(tok->u.v);
2010 n_col = isl_int_get_si(tok2->u.v);
2011 on_new_line = tok2->on_new_line;
2012 isl_token_free(tok2);
2013 isl_token_free(tok);
2014 isl_assert(s->ctx, !on_new_line, return NULL);
2015 isl_assert(s->ctx, n_row >= 0, return NULL);
2016 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
2017 tok = isl_stream_next_token_on_same_line(s);
2018 if (tok) {
2019 if (tok->type != ISL_TOKEN_VALUE) {
2020 isl_stream_error(s, tok,
2021 "expecting number of output dimensions");
2022 isl_stream_push_token(s, tok);
2023 goto error;
2025 out = isl_int_get_si(tok->u.v);
2026 isl_token_free(tok);
2028 tok = isl_stream_next_token_on_same_line(s);
2029 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2030 isl_stream_error(s, tok,
2031 "expecting number of input dimensions");
2032 if (tok)
2033 isl_stream_push_token(s, tok);
2034 goto error;
2036 in = isl_int_get_si(tok->u.v);
2037 isl_token_free(tok);
2039 tok = isl_stream_next_token_on_same_line(s);
2040 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2041 isl_stream_error(s, tok,
2042 "expecting number of existentials");
2043 if (tok)
2044 isl_stream_push_token(s, tok);
2045 goto error;
2047 local = isl_int_get_si(tok->u.v);
2048 isl_token_free(tok);
2050 tok = isl_stream_next_token_on_same_line(s);
2051 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2052 isl_stream_error(s, tok,
2053 "expecting number of parameters");
2054 if (tok)
2055 isl_stream_push_token(s, tok);
2056 goto error;
2058 nparam = isl_int_get_si(tok->u.v);
2059 isl_token_free(tok);
2060 if (n_col != 1 + out + in + local + nparam + 1) {
2061 isl_stream_error(s, NULL,
2062 "dimensions don't match");
2063 goto error;
2065 } else
2066 out = n_col - 2 - nparam;
2067 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2068 if (!bmap)
2069 return NULL;
2071 for (i = 0; i < local; ++i) {
2072 int k = isl_basic_map_alloc_div(bmap);
2073 if (k < 0)
2074 goto error;
2075 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2078 for (i = 0; i < n_row; ++i)
2079 bmap = basic_map_read_polylib_constraint(s, bmap);
2081 tok = isl_stream_next_token_on_same_line(s);
2082 if (tok) {
2083 isl_stream_error(s, tok, "unexpected extra token on line");
2084 isl_stream_push_token(s, tok);
2085 goto error;
2088 bmap = isl_basic_map_simplify(bmap);
2089 bmap = isl_basic_map_finalize(bmap);
2090 return bmap;
2091 error:
2092 isl_basic_map_free(bmap);
2093 return NULL;
2096 static struct isl_map *map_read_polylib(__isl_keep isl_stream *s)
2098 struct isl_token *tok;
2099 struct isl_token *tok2;
2100 int i, n;
2101 struct isl_map *map;
2103 tok = isl_stream_next_token(s);
2104 if (!tok) {
2105 isl_stream_error(s, NULL, "unexpected EOF");
2106 return NULL;
2108 tok2 = isl_stream_next_token_on_same_line(s);
2109 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2110 isl_stream_push_token(s, tok2);
2111 isl_stream_push_token(s, tok);
2112 return isl_map_from_basic_map(basic_map_read_polylib(s));
2114 if (tok2) {
2115 isl_stream_error(s, tok2, "unexpected token");
2116 isl_stream_push_token(s, tok2);
2117 isl_stream_push_token(s, tok);
2118 return NULL;
2120 n = isl_int_get_si(tok->u.v);
2121 isl_token_free(tok);
2123 isl_assert(s->ctx, n >= 1, return NULL);
2125 map = isl_map_from_basic_map(basic_map_read_polylib(s));
2127 for (i = 1; map && i < n; ++i)
2128 map = isl_map_union(map,
2129 isl_map_from_basic_map(basic_map_read_polylib(s)));
2131 return map;
2134 static int optional_power(__isl_keep isl_stream *s)
2136 int pow;
2137 struct isl_token *tok;
2139 tok = isl_stream_next_token(s);
2140 if (!tok)
2141 return 1;
2142 if (tok->type != '^') {
2143 isl_stream_push_token(s, tok);
2144 return 1;
2146 isl_token_free(tok);
2147 tok = isl_stream_next_token(s);
2148 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2149 isl_stream_error(s, tok, "expecting exponent");
2150 if (tok)
2151 isl_stream_push_token(s, tok);
2152 return 1;
2154 pow = isl_int_get_si(tok->u.v);
2155 isl_token_free(tok);
2156 return pow;
2159 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2160 __isl_keep isl_map *map, struct vars *v);
2162 static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2163 __isl_keep isl_map *map, struct vars *v)
2165 isl_pw_qpolynomial *pwqp;
2166 struct isl_token *tok;
2168 tok = next_token(s);
2169 if (!tok) {
2170 isl_stream_error(s, NULL, "unexpected EOF");
2171 return NULL;
2173 if (tok->type == '(') {
2174 int pow;
2176 isl_token_free(tok);
2177 pwqp = read_term(s, map, v);
2178 if (!pwqp)
2179 return NULL;
2180 if (isl_stream_eat(s, ')'))
2181 goto error;
2182 pow = optional_power(s);
2183 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2184 } else if (tok->type == ISL_TOKEN_VALUE) {
2185 struct isl_token *tok2;
2186 isl_qpolynomial *qp;
2188 tok2 = isl_stream_next_token(s);
2189 if (tok2 && tok2->type == '/') {
2190 isl_token_free(tok2);
2191 tok2 = next_token(s);
2192 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2193 isl_stream_error(s, tok2, "expected denominator");
2194 isl_token_free(tok);
2195 isl_token_free(tok2);
2196 return NULL;
2198 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2199 tok->u.v, tok2->u.v);
2200 isl_token_free(tok2);
2201 } else {
2202 isl_stream_push_token(s, tok2);
2203 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2204 tok->u.v);
2206 isl_token_free(tok);
2207 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2208 } else if (tok->type == ISL_TOKEN_INFTY) {
2209 isl_qpolynomial *qp;
2210 isl_token_free(tok);
2211 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2212 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2213 } else if (tok->type == ISL_TOKEN_NAN) {
2214 isl_qpolynomial *qp;
2215 isl_token_free(tok);
2216 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2217 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2218 } else if (tok->type == ISL_TOKEN_IDENT) {
2219 int n = v->n;
2220 int pos = vars_pos(v, tok->u.s, -1);
2221 int pow;
2222 isl_qpolynomial *qp;
2223 if (pos < 0) {
2224 isl_token_free(tok);
2225 return NULL;
2227 if (pos >= n) {
2228 vars_drop(v, v->n - n);
2229 isl_stream_error(s, tok, "unknown identifier");
2230 isl_token_free(tok);
2231 return NULL;
2233 isl_token_free(tok);
2234 pow = optional_power(s);
2235 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2236 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2237 } else if (is_start_of_div(tok)) {
2238 isl_pw_aff *pwaff;
2239 int pow;
2241 isl_stream_push_token(s, tok);
2242 pwaff = accept_div(s, isl_map_get_space(map), v);
2243 pow = optional_power(s);
2244 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2245 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2246 } else if (tok->type == '-') {
2247 isl_token_free(tok);
2248 pwqp = read_factor(s, map, v);
2249 pwqp = isl_pw_qpolynomial_neg(pwqp);
2250 } else {
2251 isl_stream_error(s, tok, "unexpected isl_token");
2252 isl_stream_push_token(s, tok);
2253 return NULL;
2256 if (isl_stream_eat_if_available(s, '*') ||
2257 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
2258 isl_pw_qpolynomial *pwqp2;
2260 pwqp2 = read_factor(s, map, v);
2261 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2264 return pwqp;
2265 error:
2266 isl_pw_qpolynomial_free(pwqp);
2267 return NULL;
2270 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2271 __isl_keep isl_map *map, struct vars *v)
2273 struct isl_token *tok;
2274 isl_pw_qpolynomial *pwqp;
2276 pwqp = read_factor(s, map, v);
2278 for (;;) {
2279 tok = next_token(s);
2280 if (!tok)
2281 return pwqp;
2283 if (tok->type == '+') {
2284 isl_pw_qpolynomial *pwqp2;
2286 isl_token_free(tok);
2287 pwqp2 = read_factor(s, map, v);
2288 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2289 } else if (tok->type == '-') {
2290 isl_pw_qpolynomial *pwqp2;
2292 isl_token_free(tok);
2293 pwqp2 = read_factor(s, map, v);
2294 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2295 } else if (tok->type == ISL_TOKEN_VALUE &&
2296 isl_int_is_neg(tok->u.v)) {
2297 isl_pw_qpolynomial *pwqp2;
2299 isl_stream_push_token(s, tok);
2300 pwqp2 = read_factor(s, map, v);
2301 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2302 } else {
2303 isl_stream_push_token(s, tok);
2304 break;
2308 return pwqp;
2311 static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2312 __isl_take isl_map *map, struct vars *v, int rational)
2314 struct isl_token *tok;
2316 tok = isl_stream_next_token(s);
2317 if (!tok) {
2318 isl_stream_error(s, NULL, "unexpected EOF");
2319 goto error;
2321 if (tok->type == ':' ||
2322 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
2323 isl_token_free(tok);
2324 map = read_formula(s, v, map, rational);
2325 } else
2326 isl_stream_push_token(s, tok);
2328 return map;
2329 error:
2330 isl_map_free(map);
2331 return NULL;
2334 static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2335 __isl_take isl_map *map, struct vars *v, int n)
2337 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2338 isl_pw_qpolynomial *pwqp;
2339 struct isl_set *set;
2341 pwqp = read_term(s, map, v);
2342 map = read_optional_formula(s, map, v, 0);
2343 set = isl_map_range(map);
2345 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2347 vars_drop(v, v->n - n);
2349 obj.v = pwqp;
2350 return obj;
2353 static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2354 __isl_take isl_set *set, struct vars *v, int n)
2356 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2357 isl_pw_qpolynomial *pwqp;
2358 isl_pw_qpolynomial_fold *pwf = NULL;
2360 if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
2361 return obj_read_poly(s, set, v, n);
2363 if (isl_stream_eat(s, '('))
2364 goto error;
2366 pwqp = read_term(s, set, v);
2367 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
2369 while (isl_stream_eat_if_available(s, ',')) {
2370 isl_pw_qpolynomial_fold *pwf_i;
2371 pwqp = read_term(s, set, v);
2372 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
2373 pwqp);
2374 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2377 if (isl_stream_eat(s, ')'))
2378 goto error;
2380 set = read_optional_formula(s, set, v, 0);
2381 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2383 vars_drop(v, v->n - n);
2385 obj.v = pwf;
2386 return obj;
2387 error:
2388 isl_set_free(set);
2389 isl_pw_qpolynomial_fold_free(pwf);
2390 obj.type = isl_obj_none;
2391 return obj;
2394 static int is_rational(__isl_keep isl_stream *s)
2396 struct isl_token *tok;
2398 tok = isl_stream_next_token(s);
2399 if (!tok)
2400 return 0;
2401 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
2402 isl_token_free(tok);
2403 isl_stream_eat(s, ':');
2404 return 1;
2407 isl_stream_push_token(s, tok);
2409 return 0;
2412 static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2413 __isl_take isl_map *map, struct vars *v)
2415 struct isl_token *tok;
2416 struct isl_obj obj = { isl_obj_set, NULL };
2417 int n = v->n;
2418 int rational;
2420 rational = is_rational(s);
2421 if (rational)
2422 map = isl_map_set_rational(map);
2424 if (isl_stream_next_token_is(s, ':')) {
2425 obj.type = isl_obj_set;
2426 obj.v = read_optional_formula(s, map, v, rational);
2427 return obj;
2430 if (!next_is_tuple(s))
2431 return obj_read_poly_or_fold(s, map, v, n);
2433 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2434 if (!map)
2435 goto error;
2436 tok = isl_stream_next_token(s);
2437 if (!tok)
2438 goto error;
2439 if (tok->type == ISL_TOKEN_TO) {
2440 obj.type = isl_obj_map;
2441 isl_token_free(tok);
2442 if (!next_is_tuple(s)) {
2443 isl_set *set = isl_map_domain(map);
2444 return obj_read_poly_or_fold(s, set, v, n);
2446 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2447 if (!map)
2448 goto error;
2449 } else {
2450 map = isl_map_domain(map);
2451 isl_stream_push_token(s, tok);
2454 map = read_optional_formula(s, map, v, rational);
2456 vars_drop(v, v->n - n);
2458 obj.v = map;
2459 return obj;
2460 error:
2461 isl_map_free(map);
2462 obj.type = isl_obj_none;
2463 return obj;
2466 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2468 if (obj.type == isl_obj_map) {
2469 obj.v = isl_union_map_from_map(obj.v);
2470 obj.type = isl_obj_union_map;
2471 } else if (obj.type == isl_obj_set) {
2472 obj.v = isl_union_set_from_set(obj.v);
2473 obj.type = isl_obj_union_set;
2474 } else if (obj.type == isl_obj_pw_qpolynomial) {
2475 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2476 obj.type = isl_obj_union_pw_qpolynomial;
2477 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2478 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2479 obj.type = isl_obj_union_pw_qpolynomial_fold;
2480 } else
2481 isl_assert(ctx, 0, goto error);
2482 return obj;
2483 error:
2484 obj.type->free(obj.v);
2485 obj.type = isl_obj_none;
2486 return obj;
2489 static struct isl_obj obj_add(__isl_keep isl_stream *s,
2490 struct isl_obj obj1, struct isl_obj obj2)
2492 if (obj2.type == isl_obj_none || !obj2.v)
2493 goto error;
2494 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2495 obj1 = to_union(s->ctx, obj1);
2496 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2497 obj2 = to_union(s->ctx, obj2);
2498 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2499 obj1 = to_union(s->ctx, obj1);
2500 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2501 obj2 = to_union(s->ctx, obj2);
2502 if (obj1.type == isl_obj_pw_qpolynomial &&
2503 obj2.type == isl_obj_union_pw_qpolynomial)
2504 obj1 = to_union(s->ctx, obj1);
2505 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2506 obj2.type == isl_obj_pw_qpolynomial)
2507 obj2 = to_union(s->ctx, obj2);
2508 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2509 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2510 obj1 = to_union(s->ctx, obj1);
2511 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2512 obj2.type == isl_obj_pw_qpolynomial_fold)
2513 obj2 = to_union(s->ctx, obj2);
2514 if (obj1.type != obj2.type) {
2515 isl_stream_error(s, NULL,
2516 "attempt to combine incompatible objects");
2517 goto error;
2519 if (!obj1.type->add)
2520 isl_die(s->ctx, isl_error_internal,
2521 "combination not supported on object type", goto error);
2522 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2523 obj1 = to_union(s->ctx, obj1);
2524 obj2 = to_union(s->ctx, obj2);
2526 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2527 obj1 = to_union(s->ctx, obj1);
2528 obj2 = to_union(s->ctx, obj2);
2530 if (obj1.type == isl_obj_pw_qpolynomial &&
2531 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2532 obj1 = to_union(s->ctx, obj1);
2533 obj2 = to_union(s->ctx, obj2);
2535 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2536 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2537 obj1 = to_union(s->ctx, obj1);
2538 obj2 = to_union(s->ctx, obj2);
2540 obj1.v = obj1.type->add(obj1.v, obj2.v);
2541 return obj1;
2542 error:
2543 obj1.type->free(obj1.v);
2544 obj2.type->free(obj2.v);
2545 obj1.type = isl_obj_none;
2546 obj1.v = NULL;
2547 return obj1;
2550 /* Are the first two tokens on "s", "domain" (either as a string
2551 * or as an identifier) followed by ":"?
2553 static int next_is_domain_colon(__isl_keep isl_stream *s)
2555 struct isl_token *tok;
2556 char *name;
2557 int res;
2559 tok = isl_stream_next_token(s);
2560 if (!tok)
2561 return 0;
2562 if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
2563 isl_stream_push_token(s, tok);
2564 return 0;
2567 name = isl_token_get_str(s->ctx, tok);
2568 res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2569 free(name);
2571 isl_stream_push_token(s, tok);
2573 return res;
2576 /* Do the first tokens on "s" look like a schedule?
2578 * The root of a schedule is always a domain node, so the first thing
2579 * we expect in the stream is a domain key, i.e., "domain" followed
2580 * by ":". If the schedule was printed in YAML flow style, then
2581 * we additionally expect a "{" to open the outer mapping.
2583 static int next_is_schedule(__isl_keep isl_stream *s)
2585 struct isl_token *tok;
2586 int is_schedule;
2588 tok = isl_stream_next_token(s);
2589 if (!tok)
2590 return 0;
2591 if (tok->type != '{') {
2592 isl_stream_push_token(s, tok);
2593 return next_is_domain_colon(s);
2596 is_schedule = next_is_domain_colon(s);
2597 isl_stream_push_token(s, tok);
2599 return is_schedule;
2602 /* Read an isl_schedule from "s" and store it in an isl_obj.
2604 static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2606 struct isl_obj obj;
2608 obj.type = isl_obj_schedule;
2609 obj.v = isl_stream_read_schedule(s);
2611 return obj;
2614 /* Read a disjunction of object bodies from "s".
2615 * That is, read the inside of the braces, but not the braces themselves.
2616 * "v" contains a description of the identifiers parsed so far.
2617 * "map" contains information about the parameters.
2619 static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
2620 struct vars *v, __isl_keep isl_map *map)
2622 struct isl_obj obj = { isl_obj_set, NULL };
2624 if (isl_stream_next_token_is(s, '}')) {
2625 obj.type = isl_obj_union_set;
2626 obj.v = isl_union_set_empty(isl_map_get_space(map));
2627 return obj;
2630 for (;;) {
2631 struct isl_obj o;
2632 o = obj_read_body(s, isl_map_copy(map), v);
2633 if (!obj.v)
2634 obj = o;
2635 else
2636 obj = obj_add(s, obj, o);
2637 if (obj.type == isl_obj_none || !obj.v)
2638 return obj;
2639 if (!isl_stream_eat_if_available(s, ';'))
2640 break;
2641 if (isl_stream_next_token_is(s, '}'))
2642 break;
2645 return obj;
2648 static struct isl_obj obj_read(__isl_keep isl_stream *s)
2650 isl_map *map = NULL;
2651 struct isl_token *tok;
2652 struct vars *v = NULL;
2653 struct isl_obj obj = { isl_obj_set, NULL };
2655 if (next_is_schedule(s))
2656 return schedule_read(s);
2658 tok = next_token(s);
2659 if (!tok) {
2660 isl_stream_error(s, NULL, "unexpected EOF");
2661 goto error;
2663 if (tok->type == ISL_TOKEN_VALUE) {
2664 struct isl_token *tok2;
2665 struct isl_map *map;
2667 tok2 = isl_stream_next_token(s);
2668 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2669 isl_int_is_neg(tok2->u.v)) {
2670 if (tok2)
2671 isl_stream_push_token(s, tok2);
2672 obj.type = isl_obj_val;
2673 obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2674 isl_token_free(tok);
2675 return obj;
2677 isl_stream_push_token(s, tok2);
2678 isl_stream_push_token(s, tok);
2679 map = map_read_polylib(s);
2680 if (!map)
2681 goto error;
2682 if (isl_map_may_be_set(map))
2683 obj.v = isl_map_range(map);
2684 else {
2685 obj.type = isl_obj_map;
2686 obj.v = map;
2688 return obj;
2690 v = vars_new(s->ctx);
2691 if (!v) {
2692 isl_stream_push_token(s, tok);
2693 goto error;
2695 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2696 if (tok->type == '[') {
2697 isl_stream_push_token(s, tok);
2698 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2699 if (!map)
2700 goto error;
2701 tok = isl_stream_next_token(s);
2702 if (!tok || tok->type != ISL_TOKEN_TO) {
2703 isl_stream_error(s, tok, "expecting '->'");
2704 if (tok)
2705 isl_stream_push_token(s, tok);
2706 goto error;
2708 isl_token_free(tok);
2709 tok = isl_stream_next_token(s);
2711 if (!tok || tok->type != '{') {
2712 isl_stream_error(s, tok, "expecting '{'");
2713 if (tok)
2714 isl_stream_push_token(s, tok);
2715 goto error;
2717 isl_token_free(tok);
2719 tok = isl_stream_next_token(s);
2720 if (!tok)
2722 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2723 isl_token_free(tok);
2724 if (isl_stream_eat(s, '='))
2725 goto error;
2726 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2727 if (!map)
2728 goto error;
2729 } else
2730 isl_stream_push_token(s, tok);
2732 obj = obj_read_disjuncts(s, v, map);
2733 if (obj.type == isl_obj_none || !obj.v)
2734 goto error;
2736 tok = isl_stream_next_token(s);
2737 if (tok && tok->type == '}') {
2738 isl_token_free(tok);
2739 } else {
2740 isl_stream_error(s, tok, "unexpected isl_token");
2741 if (tok)
2742 isl_token_free(tok);
2743 goto error;
2746 vars_free(v);
2747 isl_map_free(map);
2749 return obj;
2750 error:
2751 isl_map_free(map);
2752 obj.type->free(obj.v);
2753 if (v)
2754 vars_free(v);
2755 obj.v = NULL;
2756 return obj;
2759 struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2761 return obj_read(s);
2764 __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2766 struct isl_obj obj;
2768 obj = obj_read(s);
2769 if (obj.v)
2770 isl_assert(s->ctx, obj.type == isl_obj_map ||
2771 obj.type == isl_obj_set, goto error);
2773 if (obj.type == isl_obj_set)
2774 obj.v = isl_map_from_range(obj.v);
2776 return obj.v;
2777 error:
2778 obj.type->free(obj.v);
2779 return NULL;
2782 __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2784 struct isl_obj obj;
2786 obj = obj_read(s);
2787 if (obj.v) {
2788 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
2789 obj.v = isl_map_range(obj.v);
2790 obj.type = isl_obj_set;
2792 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2795 return obj.v;
2796 error:
2797 obj.type->free(obj.v);
2798 return NULL;
2801 __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2803 struct isl_obj obj;
2805 obj = obj_read(s);
2806 if (obj.type == isl_obj_map) {
2807 obj.type = isl_obj_union_map;
2808 obj.v = isl_union_map_from_map(obj.v);
2810 if (obj.type == isl_obj_set) {
2811 obj.type = isl_obj_union_set;
2812 obj.v = isl_union_set_from_set(obj.v);
2814 if (obj.v && obj.type == isl_obj_union_set &&
2815 isl_union_set_is_empty(obj.v))
2816 obj.type = isl_obj_union_map;
2817 if (obj.v && obj.type != isl_obj_union_map)
2818 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2820 return obj.v;
2821 error:
2822 obj.type->free(obj.v);
2823 return NULL;
2826 /* Extract an isl_union_set from "obj".
2827 * This only works if the object was detected as either a set
2828 * (in which case it is converted to a union set) or a union set.
2830 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
2831 struct isl_obj obj)
2833 if (obj.type == isl_obj_set) {
2834 obj.type = isl_obj_union_set;
2835 obj.v = isl_union_set_from_set(obj.v);
2837 if (obj.v)
2838 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
2840 return obj.v;
2841 error:
2842 obj.type->free(obj.v);
2843 return NULL;
2846 /* Read an isl_union_set from "s".
2847 * First read a generic object and then try and extract
2848 * an isl_union_set from that.
2850 __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2852 struct isl_obj obj;
2854 obj = obj_read(s);
2855 return extract_union_set(s->ctx, obj);
2858 static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2860 struct isl_obj obj;
2861 struct isl_map *map;
2862 struct isl_basic_map *bmap;
2864 obj = obj_read(s);
2865 if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
2866 isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
2867 goto error);
2868 map = obj.v;
2869 if (!map)
2870 return NULL;
2872 if (map->n > 1)
2873 isl_die(s->ctx, isl_error_invalid,
2874 "set or map description involves "
2875 "more than one disjunct", goto error);
2877 if (map->n == 0)
2878 bmap = isl_basic_map_empty(isl_map_get_space(map));
2879 else
2880 bmap = isl_basic_map_copy(map->p[0]);
2882 isl_map_free(map);
2884 return bmap;
2885 error:
2886 obj.type->free(obj.v);
2887 return NULL;
2890 static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2892 isl_basic_map *bmap;
2893 bmap = basic_map_read(s);
2894 if (!bmap)
2895 return NULL;
2896 if (!isl_basic_map_may_be_set(bmap))
2897 isl_die(s->ctx, isl_error_invalid,
2898 "input is not a set", goto error);
2899 return isl_basic_map_range(bmap);
2900 error:
2901 isl_basic_map_free(bmap);
2902 return NULL;
2905 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2906 FILE *input)
2908 struct isl_basic_map *bmap;
2909 isl_stream *s = isl_stream_new_file(ctx, input);
2910 if (!s)
2911 return NULL;
2912 bmap = basic_map_read(s);
2913 isl_stream_free(s);
2914 return bmap;
2917 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2918 FILE *input)
2920 isl_basic_set *bset;
2921 isl_stream *s = isl_stream_new_file(ctx, input);
2922 if (!s)
2923 return NULL;
2924 bset = basic_set_read(s);
2925 isl_stream_free(s);
2926 return bset;
2929 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2930 const char *str)
2932 struct isl_basic_map *bmap;
2933 isl_stream *s = isl_stream_new_str(ctx, str);
2934 if (!s)
2935 return NULL;
2936 bmap = basic_map_read(s);
2937 isl_stream_free(s);
2938 return bmap;
2941 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2942 const char *str)
2944 isl_basic_set *bset;
2945 isl_stream *s = isl_stream_new_str(ctx, str);
2946 if (!s)
2947 return NULL;
2948 bset = basic_set_read(s);
2949 isl_stream_free(s);
2950 return bset;
2953 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2954 FILE *input)
2956 struct isl_map *map;
2957 isl_stream *s = isl_stream_new_file(ctx, input);
2958 if (!s)
2959 return NULL;
2960 map = isl_stream_read_map(s);
2961 isl_stream_free(s);
2962 return map;
2965 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2966 const char *str)
2968 struct isl_map *map;
2969 isl_stream *s = isl_stream_new_str(ctx, str);
2970 if (!s)
2971 return NULL;
2972 map = isl_stream_read_map(s);
2973 isl_stream_free(s);
2974 return map;
2977 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2978 FILE *input)
2980 isl_set *set;
2981 isl_stream *s = isl_stream_new_file(ctx, input);
2982 if (!s)
2983 return NULL;
2984 set = isl_stream_read_set(s);
2985 isl_stream_free(s);
2986 return set;
2989 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2990 const char *str)
2992 isl_set *set;
2993 isl_stream *s = isl_stream_new_str(ctx, str);
2994 if (!s)
2995 return NULL;
2996 set = isl_stream_read_set(s);
2997 isl_stream_free(s);
2998 return set;
3001 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
3002 FILE *input)
3004 isl_union_map *umap;
3005 isl_stream *s = isl_stream_new_file(ctx, input);
3006 if (!s)
3007 return NULL;
3008 umap = isl_stream_read_union_map(s);
3009 isl_stream_free(s);
3010 return umap;
3013 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
3014 const char *str)
3016 isl_union_map *umap;
3017 isl_stream *s = isl_stream_new_str(ctx, str);
3018 if (!s)
3019 return NULL;
3020 umap = isl_stream_read_union_map(s);
3021 isl_stream_free(s);
3022 return umap;
3025 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
3026 FILE *input)
3028 isl_union_set *uset;
3029 isl_stream *s = isl_stream_new_file(ctx, input);
3030 if (!s)
3031 return NULL;
3032 uset = isl_stream_read_union_set(s);
3033 isl_stream_free(s);
3034 return uset;
3037 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3038 const char *str)
3040 isl_union_set *uset;
3041 isl_stream *s = isl_stream_new_str(ctx, str);
3042 if (!s)
3043 return NULL;
3044 uset = isl_stream_read_union_set(s);
3045 isl_stream_free(s);
3046 return uset;
3049 static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3051 struct isl_vec *vec = NULL;
3052 struct isl_token *tok;
3053 unsigned size;
3054 int j;
3056 tok = isl_stream_next_token(s);
3057 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3058 isl_stream_error(s, tok, "expecting vector length");
3059 goto error;
3062 size = isl_int_get_si(tok->u.v);
3063 isl_token_free(tok);
3065 vec = isl_vec_alloc(s->ctx, size);
3067 for (j = 0; j < size; ++j) {
3068 tok = isl_stream_next_token(s);
3069 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3070 isl_stream_error(s, tok, "expecting constant value");
3071 goto error;
3073 isl_int_set(vec->el[j], tok->u.v);
3074 isl_token_free(tok);
3077 return vec;
3078 error:
3079 isl_token_free(tok);
3080 isl_vec_free(vec);
3081 return NULL;
3084 static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3086 return isl_vec_read_polylib(s);
3089 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3091 isl_vec *v;
3092 isl_stream *s = isl_stream_new_file(ctx, input);
3093 if (!s)
3094 return NULL;
3095 v = vec_read(s);
3096 isl_stream_free(s);
3097 return v;
3100 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3101 __isl_keep isl_stream *s)
3103 struct isl_obj obj;
3105 obj = obj_read(s);
3106 if (obj.v)
3107 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3108 goto error);
3110 return obj.v;
3111 error:
3112 obj.type->free(obj.v);
3113 return NULL;
3116 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3117 const char *str)
3119 isl_pw_qpolynomial *pwqp;
3120 isl_stream *s = isl_stream_new_str(ctx, str);
3121 if (!s)
3122 return NULL;
3123 pwqp = isl_stream_read_pw_qpolynomial(s);
3124 isl_stream_free(s);
3125 return pwqp;
3128 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3129 FILE *input)
3131 isl_pw_qpolynomial *pwqp;
3132 isl_stream *s = isl_stream_new_file(ctx, input);
3133 if (!s)
3134 return NULL;
3135 pwqp = isl_stream_read_pw_qpolynomial(s);
3136 isl_stream_free(s);
3137 return pwqp;
3140 /* Is the next token an identifer not in "v"?
3142 static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3144 int n = v->n;
3145 int fresh;
3146 struct isl_token *tok;
3148 tok = isl_stream_next_token(s);
3149 if (!tok)
3150 return 0;
3151 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
3152 isl_stream_push_token(s, tok);
3154 vars_drop(v, v->n - n);
3156 return fresh;
3159 /* First read the domain of the affine expression, which may be
3160 * a parameter space or a set.
3161 * The tricky part is that we don't know if the domain is a set or not,
3162 * so when we are trying to read the domain, we may actually be reading
3163 * the affine expression itself (defined on a parameter domains)
3164 * If the tuple we are reading is named, we assume it's the domain.
3165 * Also, if inside the tuple, the first thing we find is a nested tuple
3166 * or a new identifier, we again assume it's the domain.
3167 * Finally, if the tuple is empty, then it must be the domain
3168 * since it does not contain an affine expression.
3169 * Otherwise, we assume we are reading an affine expression.
3171 static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3172 __isl_take isl_set *dom, struct vars *v)
3174 struct isl_token *tok, *tok2;
3175 int is_empty;
3177 tok = isl_stream_next_token(s);
3178 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
3179 isl_stream_push_token(s, tok);
3180 return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3182 if (!tok || tok->type != '[') {
3183 isl_stream_error(s, tok, "expecting '['");
3184 goto error;
3186 tok2 = isl_stream_next_token(s);
3187 is_empty = tok2 && tok2->type == ']';
3188 if (tok2)
3189 isl_stream_push_token(s, tok2);
3190 if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
3191 isl_stream_push_token(s, tok);
3192 dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3193 } else
3194 isl_stream_push_token(s, tok);
3196 return dom;
3197 error:
3198 if (tok)
3199 isl_stream_push_token(s, tok);
3200 isl_set_free(dom);
3201 return NULL;
3204 /* Read an affine expression from "s".
3206 __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3208 isl_aff *aff;
3209 isl_multi_aff *ma;
3210 isl_size dim;
3212 ma = isl_stream_read_multi_aff(s);
3213 dim = isl_multi_aff_dim(ma, isl_dim_out);
3214 if (dim < 0)
3215 goto error;
3216 if (dim != 1)
3217 isl_die(s->ctx, isl_error_invalid,
3218 "expecting single affine expression",
3219 goto error);
3221 aff = isl_multi_aff_get_aff(ma, 0);
3222 isl_multi_aff_free(ma);
3223 return aff;
3224 error:
3225 isl_multi_aff_free(ma);
3226 return NULL;
3229 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3231 static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3232 __isl_take isl_set *dom, struct vars *v)
3234 isl_pw_aff *pwaff = NULL;
3236 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
3237 goto error;
3239 if (isl_stream_eat(s, '['))
3240 goto error;
3242 pwaff = accept_affine(s, isl_set_get_space(dom), v);
3244 if (isl_stream_eat(s, ']'))
3245 goto error;
3247 dom = read_optional_formula(s, dom, v, 0);
3248 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3250 return pwaff;
3251 error:
3252 isl_set_free(dom);
3253 isl_pw_aff_free(pwaff);
3254 return NULL;
3257 __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3259 struct vars *v;
3260 isl_set *dom = NULL;
3261 isl_set *aff_dom;
3262 isl_pw_aff *pa = NULL;
3263 int n;
3265 v = vars_new(s->ctx);
3266 if (!v)
3267 return NULL;
3269 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3270 if (next_is_tuple(s)) {
3271 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3272 if (isl_stream_eat(s, ISL_TOKEN_TO))
3273 goto error;
3275 if (isl_stream_eat(s, '{'))
3276 goto error;
3278 n = v->n;
3279 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3280 pa = read_pw_aff_with_dom(s, aff_dom, v);
3281 vars_drop(v, v->n - n);
3283 while (isl_stream_eat_if_available(s, ';')) {
3284 isl_pw_aff *pa_i;
3286 n = v->n;
3287 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3288 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3289 vars_drop(v, v->n - n);
3291 pa = isl_pw_aff_union_add(pa, pa_i);
3294 if (isl_stream_eat(s, '}'))
3295 goto error;
3297 vars_free(v);
3298 isl_set_free(dom);
3299 return pa;
3300 error:
3301 vars_free(v);
3302 isl_set_free(dom);
3303 isl_pw_aff_free(pa);
3304 return NULL;
3307 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3309 isl_aff *aff;
3310 isl_stream *s = isl_stream_new_str(ctx, str);
3311 if (!s)
3312 return NULL;
3313 aff = isl_stream_read_aff(s);
3314 isl_stream_free(s);
3315 return aff;
3318 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3320 isl_pw_aff *pa;
3321 isl_stream *s = isl_stream_new_str(ctx, str);
3322 if (!s)
3323 return NULL;
3324 pa = isl_stream_read_pw_aff(s);
3325 isl_stream_free(s);
3326 return pa;
3329 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3330 * from a tuple "tuple" read by read_tuple.
3332 * Note that the function read_tuple accepts tuples where some output or
3333 * set dimensions are defined in terms of other output or set dimensions
3334 * since this function is also used to read maps. As a special case,
3335 * read_tuple also accept dimensions that are defined in terms of themselves
3336 * (i.e., that are not defined).
3337 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3338 * that the definitions of the output/set dimensions do not involve any
3339 * output/set dimensions.
3340 * Finally, drop the output dimensions from the domain of the result
3341 * of read_tuple (which is of the form [input, output] -> [output],
3342 * with anonymous domain) and reset the space.
3344 static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3345 __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3347 int i;
3348 isl_size dim, n;
3349 isl_space *space;
3350 isl_multi_pw_aff *mpa;
3352 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3353 dim = isl_space_dim(dom_space, isl_dim_all);
3354 if (n < 0 || dim < 0)
3355 dom_space = isl_space_free(dom_space);
3356 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3357 space = isl_space_align_params(space, isl_space_copy(dom_space));
3358 if (!isl_space_is_params(dom_space))
3359 space = isl_space_map_from_domain_and_range(
3360 isl_space_copy(dom_space), space);
3361 isl_space_free(dom_space);
3362 mpa = isl_multi_pw_aff_alloc(space);
3364 for (i = 0; i < n; ++i) {
3365 isl_pw_aff *pa;
3366 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3367 if (!pa)
3368 return isl_multi_pw_aff_free(mpa);
3369 if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3370 isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3371 isl_pw_aff_free(pa);
3372 isl_die(ctx, isl_error_invalid,
3373 "not an affine expression",
3374 return isl_multi_pw_aff_free(mpa));
3376 pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3377 space = isl_multi_pw_aff_get_domain_space(mpa);
3378 pa = isl_pw_aff_reset_domain_space(pa, space);
3379 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3382 return mpa;
3385 /* Read a tuple of affine expressions, together with optional constraints
3386 * on the domain from "s". "dom" represents the initial constraints
3387 * on the domain.
3389 * The isl_multi_aff may live in either a set or a map space.
3390 * First read the first tuple and check if it is followed by a "->".
3391 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3392 * read in the next tuple. This tuple (or the first tuple if it was
3393 * not followed by a "->") is then converted into an isl_multi_pw_aff
3394 * through a call to extract_mpa_from_tuple.
3395 * The result is converted to an isl_pw_multi_aff and
3396 * its domain is intersected with the domain.
3398 static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3399 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3401 isl_multi_pw_aff *tuple;
3402 isl_multi_pw_aff *mpa;
3403 isl_pw_multi_aff *pma;
3404 int n = v->n;
3406 tuple = read_tuple(s, v, 0, 0);
3407 if (!tuple)
3408 goto error;
3409 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3410 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3411 dom = isl_map_domain(map);
3412 tuple = read_tuple(s, v, 0, 0);
3413 if (!tuple)
3414 goto error;
3417 dom = read_optional_formula(s, dom, v, 0);
3419 vars_drop(v, v->n - n);
3421 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3422 isl_multi_pw_aff_free(tuple);
3423 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3424 pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3426 return pma;
3427 error:
3428 isl_set_free(dom);
3429 return NULL;
3432 /* Read an isl_pw_multi_aff from "s".
3434 * In particular, first read the parameters and then read a sequence
3435 * of one or more tuples of affine expressions with optional conditions and
3436 * add them up.
3438 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3439 __isl_keep isl_stream *s)
3441 struct vars *v;
3442 isl_set *dom;
3443 isl_pw_multi_aff *pma = NULL;
3445 v = vars_new(s->ctx);
3446 if (!v)
3447 return NULL;
3449 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3450 if (next_is_tuple(s)) {
3451 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3452 if (isl_stream_eat(s, ISL_TOKEN_TO))
3453 goto error;
3455 if (isl_stream_eat(s, '{'))
3456 goto error;
3458 pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3460 while (isl_stream_eat_if_available(s, ';')) {
3461 isl_pw_multi_aff *pma2;
3463 pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3464 pma = isl_pw_multi_aff_union_add(pma, pma2);
3465 if (!pma)
3466 goto error;
3469 if (isl_stream_eat(s, '}'))
3470 goto error;
3472 isl_set_free(dom);
3473 vars_free(v);
3474 return pma;
3475 error:
3476 isl_pw_multi_aff_free(pma);
3477 isl_set_free(dom);
3478 vars_free(v);
3479 return NULL;
3482 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3483 const char *str)
3485 isl_pw_multi_aff *pma;
3486 isl_stream *s = isl_stream_new_str(ctx, str);
3487 if (!s)
3488 return NULL;
3489 pma = isl_stream_read_pw_multi_aff(s);
3490 isl_stream_free(s);
3491 return pma;
3494 /* Read an isl_union_pw_multi_aff from "s".
3495 * We currently read a generic object and if it turns out to be a set or
3496 * a map, we convert that to an isl_union_pw_multi_aff.
3497 * It would be more efficient if we were to construct
3498 * the isl_union_pw_multi_aff directly.
3500 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3501 __isl_keep isl_stream *s)
3503 struct isl_obj obj;
3505 obj = obj_read(s);
3506 if (!obj.v)
3507 return NULL;
3509 if (obj.type == isl_obj_map || obj.type == isl_obj_set)
3510 obj = to_union(s->ctx, obj);
3511 if (obj.type == isl_obj_union_map)
3512 return isl_union_pw_multi_aff_from_union_map(obj.v);
3513 if (obj.type == isl_obj_union_set)
3514 return isl_union_pw_multi_aff_from_union_set(obj.v);
3516 obj.type->free(obj.v);
3517 isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3518 return NULL);
3521 /* Read an isl_union_pw_multi_aff from "str".
3523 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3524 isl_ctx *ctx, const char *str)
3526 isl_union_pw_multi_aff *upma;
3527 isl_stream *s = isl_stream_new_str(ctx, str);
3528 if (!s)
3529 return NULL;
3530 upma = isl_stream_read_union_pw_multi_aff(s);
3531 isl_stream_free(s);
3532 return upma;
3535 /* Assuming "pa" represents a single affine expression defined on a universe
3536 * domain, extract this affine expression.
3538 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3540 isl_aff *aff;
3542 if (!pa)
3543 return NULL;
3544 if (pa->n != 1)
3545 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3546 "expecting single affine expression",
3547 goto error);
3548 if (!isl_set_plain_is_universe(pa->p[0].set))
3549 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3550 "expecting universe domain",
3551 goto error);
3553 aff = isl_aff_copy(pa->p[0].aff);
3554 isl_pw_aff_free(pa);
3555 return aff;
3556 error:
3557 isl_pw_aff_free(pa);
3558 return NULL;
3561 #undef BASE
3562 #define BASE val
3564 #include <isl_multi_read_no_explicit_domain_templ.c>
3566 #undef BASE
3567 #define BASE id
3569 #include <isl_multi_read_no_explicit_domain_templ.c>
3571 /* Read a multi-affine expression from "s".
3572 * If the multi-affine expression has a domain, then the tuple
3573 * representing this domain cannot involve any affine expressions.
3574 * The tuple representing the actual expressions needs to consist
3575 * of only affine expressions. Moreover, these expressions can
3576 * only depend on parameters and input dimensions and not on other
3577 * output dimensions.
3579 __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3581 struct vars *v;
3582 isl_set *dom = NULL;
3583 isl_multi_pw_aff *tuple = NULL;
3584 int i;
3585 isl_size dim, n;
3586 isl_space *space, *dom_space;
3587 isl_multi_aff *ma = NULL;
3589 v = vars_new(s->ctx);
3590 if (!v)
3591 return NULL;
3593 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3594 if (next_is_tuple(s)) {
3595 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3596 if (isl_stream_eat(s, ISL_TOKEN_TO))
3597 goto error;
3599 if (!isl_set_plain_is_universe(dom))
3600 isl_die(s->ctx, isl_error_invalid,
3601 "expecting universe parameter domain", goto error);
3602 if (isl_stream_eat(s, '{'))
3603 goto error;
3605 tuple = read_tuple(s, v, 0, 0);
3606 if (!tuple)
3607 goto error;
3608 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3609 isl_set *set;
3610 isl_space *space;
3611 isl_bool has_expr;
3613 has_expr = tuple_has_expr(tuple);
3614 if (has_expr < 0)
3615 goto error;
3616 if (has_expr)
3617 isl_die(s->ctx, isl_error_invalid,
3618 "expecting universe domain", goto error);
3619 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3620 set = isl_set_universe(space);
3621 dom = isl_set_intersect_params(set, dom);
3622 isl_multi_pw_aff_free(tuple);
3623 tuple = read_tuple(s, v, 0, 0);
3624 if (!tuple)
3625 goto error;
3628 if (isl_stream_eat(s, '}'))
3629 goto error;
3631 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3632 dim = isl_set_dim(dom, isl_dim_all);
3633 if (n < 0 || dim < 0)
3634 goto error;
3635 dom_space = isl_set_get_space(dom);
3636 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3637 space = isl_space_align_params(space, isl_space_copy(dom_space));
3638 if (!isl_space_is_params(dom_space))
3639 space = isl_space_map_from_domain_and_range(
3640 isl_space_copy(dom_space), space);
3641 isl_space_free(dom_space);
3642 ma = isl_multi_aff_alloc(space);
3644 for (i = 0; i < n; ++i) {
3645 isl_pw_aff *pa;
3646 isl_aff *aff;
3647 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3648 aff = aff_from_pw_aff(pa);
3649 if (!aff)
3650 goto error;
3651 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3652 isl_aff_free(aff);
3653 isl_die(s->ctx, isl_error_invalid,
3654 "not an affine expression", goto error);
3656 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3657 space = isl_multi_aff_get_domain_space(ma);
3658 aff = isl_aff_reset_domain_space(aff, space);
3659 ma = isl_multi_aff_set_aff(ma, i, aff);
3662 isl_multi_pw_aff_free(tuple);
3663 vars_free(v);
3664 isl_set_free(dom);
3665 return ma;
3666 error:
3667 isl_multi_pw_aff_free(tuple);
3668 vars_free(v);
3669 isl_set_free(dom);
3670 isl_multi_aff_free(ma);
3671 return NULL;
3674 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3675 const char *str)
3677 isl_multi_aff *maff;
3678 isl_stream *s = isl_stream_new_str(ctx, str);
3679 if (!s)
3680 return NULL;
3681 maff = isl_stream_read_multi_aff(s);
3682 isl_stream_free(s);
3683 return maff;
3686 /* Read an isl_multi_pw_aff from "s".
3688 * The input format is similar to that of map, except that any conditions
3689 * on the domains should be specified inside the tuple since each
3690 * piecewise affine expression may have a different domain.
3691 * However, additional, shared conditions can also be specified.
3692 * This is especially useful for setting the explicit domain
3693 * of a zero-dimensional isl_multi_pw_aff.
3695 * Since we do not know in advance if the isl_multi_pw_aff lives
3696 * in a set or a map space, we first read the first tuple and check
3697 * if it is followed by a "->". If so, we convert the tuple into
3698 * the domain of the isl_multi_pw_aff and read in the next tuple.
3699 * This tuple (or the first tuple if it was not followed by a "->")
3700 * is then converted into the isl_multi_pw_aff through a call
3701 * to extract_mpa_from_tuple and the domain of the result
3702 * is intersected with the domain.
3704 __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3705 __isl_keep isl_stream *s)
3707 struct vars *v;
3708 isl_set *dom = NULL;
3709 isl_multi_pw_aff *tuple = NULL;
3710 isl_multi_pw_aff *mpa = NULL;
3712 v = vars_new(s->ctx);
3713 if (!v)
3714 return NULL;
3716 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3717 if (next_is_tuple(s)) {
3718 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3719 if (isl_stream_eat(s, ISL_TOKEN_TO))
3720 goto error;
3722 if (isl_stream_eat(s, '{'))
3723 goto error;
3725 tuple = read_tuple(s, v, 0, 0);
3726 if (!tuple)
3727 goto error;
3728 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3729 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3730 dom = isl_map_domain(map);
3731 tuple = read_tuple(s, v, 0, 0);
3732 if (!tuple)
3733 goto error;
3736 if (isl_stream_eat_if_available(s, ':'))
3737 dom = read_formula(s, v, dom, 0);
3739 if (isl_stream_eat(s, '}'))
3740 goto error;
3742 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3744 isl_multi_pw_aff_free(tuple);
3745 vars_free(v);
3746 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3747 return mpa;
3748 error:
3749 isl_multi_pw_aff_free(tuple);
3750 vars_free(v);
3751 isl_set_free(dom);
3752 isl_multi_pw_aff_free(mpa);
3753 return NULL;
3756 /* Read an isl_multi_pw_aff from "str".
3758 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3759 const char *str)
3761 isl_multi_pw_aff *mpa;
3762 isl_stream *s = isl_stream_new_str(ctx, str);
3763 if (!s)
3764 return NULL;
3765 mpa = isl_stream_read_multi_pw_aff(s);
3766 isl_stream_free(s);
3767 return mpa;
3770 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3772 static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3773 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3775 isl_pw_aff *pa;
3776 isl_union_pw_aff *upa = NULL;
3777 isl_set *aff_dom;
3778 int n;
3780 n = v->n;
3781 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3782 pa = read_pw_aff_with_dom(s, aff_dom, v);
3783 vars_drop(v, v->n - n);
3785 upa = isl_union_pw_aff_from_pw_aff(pa);
3787 while (isl_stream_eat_if_available(s, ';')) {
3788 isl_pw_aff *pa_i;
3789 isl_union_pw_aff *upa_i;
3791 n = v->n;
3792 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3793 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3794 vars_drop(v, v->n - n);
3796 upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3797 upa = isl_union_pw_aff_union_add(upa, upa_i);
3800 isl_set_free(dom);
3801 return upa;
3804 /* Read an isl_union_pw_aff from "s".
3806 * First check if there are any paramters, then read in the opening brace
3807 * and use read_union_pw_aff_with_dom to read in the body of
3808 * the isl_union_pw_aff. Finally, read the closing brace.
3810 __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3811 __isl_keep isl_stream *s)
3813 struct vars *v;
3814 isl_set *dom;
3815 isl_union_pw_aff *upa = NULL;
3817 v = vars_new(s->ctx);
3818 if (!v)
3819 return NULL;
3821 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3822 if (next_is_tuple(s)) {
3823 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3824 if (isl_stream_eat(s, ISL_TOKEN_TO))
3825 goto error;
3827 if (isl_stream_eat(s, '{'))
3828 goto error;
3830 upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3832 if (isl_stream_eat(s, '}'))
3833 goto error;
3835 vars_free(v);
3836 isl_set_free(dom);
3837 return upa;
3838 error:
3839 vars_free(v);
3840 isl_set_free(dom);
3841 isl_union_pw_aff_free(upa);
3842 return NULL;
3845 /* Read an isl_union_pw_aff from "str".
3847 __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3848 const char *str)
3850 isl_union_pw_aff *upa;
3851 isl_stream *s = isl_stream_new_str(ctx, str);
3852 if (!s)
3853 return NULL;
3854 upa = isl_stream_read_union_pw_aff(s);
3855 isl_stream_free(s);
3856 return upa;
3859 /* This function is called for each element in a tuple inside
3860 * isl_stream_read_multi_union_pw_aff.
3862 * Read a '{', the union piecewise affine expression body and a '}' and
3863 * add the isl_union_pw_aff to *list.
3865 static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3866 struct vars *v, __isl_take isl_space *space, int rational, void *user)
3868 isl_set *dom;
3869 isl_union_pw_aff *upa;
3870 isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3872 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3873 if (isl_stream_eat(s, '{'))
3874 goto error;
3875 upa = read_union_pw_aff_with_dom(s, dom, v);
3876 *list = isl_union_pw_aff_list_add(*list, upa);
3877 if (isl_stream_eat(s, '}'))
3878 return isl_space_free(space);
3879 if (!*list)
3880 return isl_space_free(space);
3881 return space;
3882 error:
3883 isl_set_free(dom);
3884 return isl_space_free(space);
3887 /* Do the next tokens in "s" correspond to an empty tuple?
3888 * In particular, does the stream start with a '[', followed by a ']',
3889 * not followed by a "->"?
3891 static int next_is_empty_tuple(__isl_keep isl_stream *s)
3893 struct isl_token *tok, *tok2, *tok3;
3894 int is_empty_tuple = 0;
3896 tok = isl_stream_next_token(s);
3897 if (!tok)
3898 return 0;
3899 if (tok->type != '[') {
3900 isl_stream_push_token(s, tok);
3901 return 0;
3904 tok2 = isl_stream_next_token(s);
3905 if (tok2 && tok2->type == ']') {
3906 tok3 = isl_stream_next_token(s);
3907 is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3908 if (tok3)
3909 isl_stream_push_token(s, tok3);
3911 if (tok2)
3912 isl_stream_push_token(s, tok2);
3913 isl_stream_push_token(s, tok);
3915 return is_empty_tuple;
3918 /* Do the next tokens in "s" correspond to a tuple of parameters?
3919 * In particular, does the stream start with a '[' that is not
3920 * followed by a '{' or a nested tuple?
3922 static int next_is_param_tuple(__isl_keep isl_stream *s)
3924 struct isl_token *tok, *tok2;
3925 int is_tuple;
3927 tok = isl_stream_next_token(s);
3928 if (!tok)
3929 return 0;
3930 if (tok->type != '[' || next_is_tuple(s)) {
3931 isl_stream_push_token(s, tok);
3932 return 0;
3935 tok2 = isl_stream_next_token(s);
3936 is_tuple = tok2 && tok2->type != '{';
3937 if (tok2)
3938 isl_stream_push_token(s, tok2);
3939 isl_stream_push_token(s, tok);
3941 return is_tuple;
3944 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
3945 * i.e., everything except the parameter specification and
3946 * without shared domain constraints.
3947 * "v" contains a description of the identifiers parsed so far.
3948 * The parameters, if any, are specified by "space".
3950 * The body is of the form
3952 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3954 * Read the tuple, collecting the individual isl_union_pw_aff
3955 * elements in a list and construct the result from the tuple space and
3956 * the list.
3958 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
3959 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
3961 isl_union_pw_aff_list *list;
3962 isl_multi_union_pw_aff *mupa;
3964 list = isl_union_pw_aff_list_alloc(s->ctx, 0);
3965 space = read_tuple_space(s, v, space, 1, 0,
3966 &read_union_pw_aff_el, &list);
3967 mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
3969 return mupa;
3972 /* Read the body of an isl_union_set from "s",
3973 * i.e., everything except the parameter specification.
3974 * "v" contains a description of the identifiers parsed so far.
3975 * The parameters, if any, are specified by "space".
3977 * First read a generic disjunction of object bodies and then try and extract
3978 * an isl_union_set from that.
3980 static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
3981 struct vars *v, __isl_take isl_space *space)
3983 struct isl_obj obj = { isl_obj_set, NULL };
3984 isl_map *map;
3986 map = isl_set_universe(space);
3987 if (isl_stream_eat(s, '{') < 0)
3988 goto error;
3989 obj = obj_read_disjuncts(s, v, map);
3990 if (isl_stream_eat(s, '}') < 0)
3991 goto error;
3992 isl_map_free(map);
3994 return extract_union_set(s->ctx, obj);
3995 error:
3996 obj.type->free(obj.v);
3997 isl_map_free(map);
3998 return NULL;
4001 /* Read the body of an isl_multi_union_pw_aff from "s",
4002 * i.e., everything except the parameter specification.
4003 * "v" contains a description of the identifiers parsed so far.
4004 * The parameters, if any, are specified by "space".
4006 * In particular, handle the special case with shared domain constraints.
4007 * These are specified as
4009 * ([...] : ...)
4011 * and are especially useful for setting the explicit domain
4012 * of a zero-dimensional isl_multi_union_pw_aff.
4013 * The core isl_multi_union_pw_aff body ([...]) is read by
4014 * read_multi_union_pw_aff_body_core.
4016 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
4017 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4019 isl_multi_union_pw_aff *mupa;
4021 if (!isl_stream_next_token_is(s, '('))
4022 return read_multi_union_pw_aff_body_core(s, v, space);
4024 if (isl_stream_eat(s, '(') < 0)
4025 goto error;
4026 mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
4027 if (isl_stream_eat_if_available(s, ':')) {
4028 isl_union_set *dom;
4030 dom = read_union_set_body(s, v, space);
4031 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4032 } else {
4033 isl_space_free(space);
4035 if (isl_stream_eat(s, ')') < 0)
4036 return isl_multi_union_pw_aff_free(mupa);
4038 return mupa;
4039 error:
4040 isl_space_free(space);
4041 return NULL;
4044 /* Read an isl_multi_union_pw_aff from "s".
4046 * The input has the form
4048 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4050 * or
4052 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4054 * Additionally, a shared domain may be specified as
4056 * ([..] : ...)
4058 * or
4060 * [..] -> ([..] : ...)
4062 * The first case is handled by the caller, the second case
4063 * is handled by read_multi_union_pw_aff_body.
4065 * We first check for the special case of an empty tuple "[]".
4066 * Then we check if there are any parameters.
4067 * Finally, read the tuple and construct the result.
4069 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
4070 __isl_keep isl_stream *s)
4072 struct vars *v;
4073 isl_set *dom = NULL;
4074 isl_space *space;
4075 isl_multi_union_pw_aff *mupa = NULL;
4077 if (next_is_empty_tuple(s)) {
4078 if (isl_stream_eat(s, '['))
4079 return NULL;
4080 if (isl_stream_eat(s, ']'))
4081 return NULL;
4082 space = isl_space_set_alloc(s->ctx, 0, 0);
4083 return isl_multi_union_pw_aff_zero(space);
4086 v = vars_new(s->ctx);
4087 if (!v)
4088 return NULL;
4090 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4091 if (next_is_param_tuple(s)) {
4092 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4093 if (isl_stream_eat(s, ISL_TOKEN_TO))
4094 goto error;
4096 space = isl_set_get_space(dom);
4097 isl_set_free(dom);
4098 mupa = read_multi_union_pw_aff_body(s, v, space);
4100 vars_free(v);
4102 return mupa;
4103 error:
4104 vars_free(v);
4105 isl_set_free(dom);
4106 isl_multi_union_pw_aff_free(mupa);
4107 return NULL;
4110 /* Read an isl_multi_union_pw_aff from "s".
4112 * In particular, handle the special case with shared domain constraints.
4113 * These are specified as
4115 * ([...] : ...)
4117 * and are especially useful for setting the explicit domain
4118 * of a zero-dimensional isl_multi_union_pw_aff.
4119 * The core isl_multi_union_pw_aff ([...]) is read by
4120 * read_multi_union_pw_aff_core.
4122 __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4123 __isl_keep isl_stream *s)
4125 isl_multi_union_pw_aff *mupa;
4127 if (!isl_stream_next_token_is(s, '('))
4128 return read_multi_union_pw_aff_core(s);
4130 if (isl_stream_eat(s, '(') < 0)
4131 return NULL;
4132 mupa = read_multi_union_pw_aff_core(s);
4133 if (isl_stream_eat_if_available(s, ':')) {
4134 isl_union_set *dom;
4136 dom = isl_stream_read_union_set(s);
4137 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4139 if (isl_stream_eat(s, ')') < 0)
4140 return isl_multi_union_pw_aff_free(mupa);
4141 return mupa;
4144 /* Read an isl_multi_union_pw_aff from "str".
4146 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4147 isl_ctx *ctx, const char *str)
4149 isl_multi_union_pw_aff *mupa;
4150 isl_stream *s = isl_stream_new_str(ctx, str);
4151 if (!s)
4152 return NULL;
4153 mupa = isl_stream_read_multi_union_pw_aff(s);
4154 isl_stream_free(s);
4155 return mupa;
4158 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4159 __isl_keep isl_stream *s)
4161 struct isl_obj obj;
4163 obj = obj_read(s);
4164 if (obj.type == isl_obj_pw_qpolynomial) {
4165 obj.type = isl_obj_union_pw_qpolynomial;
4166 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4168 if (obj.v)
4169 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4170 goto error);
4172 return obj.v;
4173 error:
4174 obj.type->free(obj.v);
4175 return NULL;
4178 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4179 isl_ctx *ctx, const char *str)
4181 isl_union_pw_qpolynomial *upwqp;
4182 isl_stream *s = isl_stream_new_str(ctx, str);
4183 if (!s)
4184 return NULL;
4185 upwqp = isl_stream_read_union_pw_qpolynomial(s);
4186 isl_stream_free(s);
4187 return upwqp;