isl_*_copy_tuple_id: use BASE instead of TYPE to select variant
[isl.git] / isl_input.c
blobe0eb2bf05663a265d36f0d0d2f508ea40b4839db
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2019,2022 Cerebras Systems
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_id_private.h>
23 #include <isl/set.h>
24 #include <isl_seq.h>
25 #include <isl_stream_private.h>
26 #include <isl/obj.h>
27 #include "isl_polynomial_private.h"
28 #include <isl/union_set.h>
29 #include <isl/union_map.h>
30 #include <isl_mat_private.h>
31 #include <isl_aff_private.h>
32 #include <isl_vec_private.h>
33 #include <isl/list.h>
34 #include <isl_val_private.h>
36 struct variable {
37 char *name;
38 int pos;
39 struct variable *next;
42 struct vars {
43 struct isl_ctx *ctx;
44 int n;
45 struct variable *v;
48 static struct vars *vars_new(struct isl_ctx *ctx)
50 struct vars *v;
51 v = isl_alloc_type(ctx, struct vars);
52 if (!v)
53 return NULL;
54 v->ctx = ctx;
55 v->n = 0;
56 v->v = NULL;
57 return v;
60 static void variable_free(struct variable *var)
62 while (var) {
63 struct variable *next = var->next;
64 free(var->name);
65 free(var);
66 var = next;
70 static void vars_free(struct vars *v)
72 if (!v)
73 return;
74 variable_free(v->v);
75 free(v);
78 static void vars_drop(struct vars *v, int n)
80 struct variable *var;
82 if (!v || !v->v)
83 return;
85 v->n -= n;
87 var = v->v;
88 while (--n >= 0) {
89 struct variable *next = var->next;
90 free(var->name);
91 free(var);
92 var = next;
94 v->v = var;
97 static struct variable *variable_new(struct vars *v, const char *name, int len,
98 int pos)
100 struct variable *var;
101 var = isl_calloc_type(v->ctx, struct variable);
102 if (!var)
103 goto error;
104 var->name = strdup(name);
105 var->name[len] = '\0';
106 var->pos = pos;
107 var->next = v->v;
108 return var;
109 error:
110 variable_free(v->v);
111 return NULL;
114 static int vars_pos(struct vars *v, const char *s, int len)
116 int pos;
117 struct variable *q;
119 if (len == -1)
120 len = strlen(s);
121 for (q = v->v; q; q = q->next) {
122 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
123 break;
125 if (q)
126 pos = q->pos;
127 else {
128 pos = v->n;
129 v->v = variable_new(v, s, len, v->n);
130 if (!v->v)
131 return -1;
132 v->n++;
134 return pos;
137 static int vars_add_anon(struct vars *v)
139 v->v = variable_new(v, "", 0, v->n);
141 if (!v->v)
142 return -1;
143 v->n++;
145 return 0;
148 /* Obtain next token, with some preprocessing.
149 * In particular, evaluate expressions of the form x^y,
150 * with x and y values.
152 static struct isl_token *next_token(__isl_keep isl_stream *s)
154 struct isl_token *tok, *tok2;
156 tok = isl_stream_next_token(s);
157 if (!tok || tok->type != ISL_TOKEN_VALUE)
158 return tok;
159 if (!isl_stream_eat_if_available(s, '^'))
160 return tok;
161 tok2 = isl_stream_next_token(s);
162 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
163 isl_stream_error(s, tok2, "expecting constant value");
164 goto error;
167 isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
169 isl_token_free(tok2);
170 return tok;
171 error:
172 isl_token_free(tok);
173 isl_token_free(tok2);
174 return NULL;
177 /* Read an isl_val from "s".
179 * The following token sequences are recognized
181 * "infty" -> infty
182 * "-" "infty" -> -infty
183 * "NaN" -> NaN
184 * n "/" d -> n/d
185 * v -> v
187 * where n, d and v are integer constants.
189 __isl_give isl_val *isl_stream_read_val(__isl_keep isl_stream *s)
191 struct isl_token *tok = NULL;
192 struct isl_token *tok2 = NULL;
193 isl_val *val;
195 tok = next_token(s);
196 if (!tok) {
197 isl_stream_error(s, NULL, "unexpected EOF");
198 goto error;
200 if (tok->type == ISL_TOKEN_INFTY) {
201 isl_token_free(tok);
202 return isl_val_infty(s->ctx);
204 if (tok->type == '-' &&
205 isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)) {
206 isl_token_free(tok);
207 return isl_val_neginfty(s->ctx);
209 if (tok->type == ISL_TOKEN_NAN) {
210 isl_token_free(tok);
211 return isl_val_nan(s->ctx);
213 if (tok->type != ISL_TOKEN_VALUE) {
214 isl_stream_error(s, tok, "expecting value");
215 goto error;
218 if (isl_stream_eat_if_available(s, '/')) {
219 tok2 = next_token(s);
220 if (!tok2) {
221 isl_stream_error(s, NULL, "unexpected EOF");
222 goto error;
224 if (tok2->type != ISL_TOKEN_VALUE) {
225 isl_stream_error(s, tok2, "expecting value");
226 goto error;
228 val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
229 val = isl_val_normalize(val);
230 } else {
231 val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
234 isl_token_free(tok);
235 isl_token_free(tok2);
236 return val;
237 error:
238 isl_token_free(tok);
239 isl_token_free(tok2);
240 return NULL;
243 /* Read an isl_val from "str".
245 __isl_give isl_val *isl_val_read_from_str(isl_ctx *ctx, const char *str)
247 isl_val *val;
248 isl_stream *s = isl_stream_new_str(ctx, str);
249 if (!s)
250 return NULL;
251 val = isl_stream_read_val(s);
252 isl_stream_free(s);
253 return val;
256 /* Perform an integer division on *f and
257 * an integer value read from the stream.
259 static isl_stat int_div_by_cst(__isl_keep isl_stream *s, isl_int *f)
261 struct isl_token *tok;
263 tok = next_token(s);
264 if (!tok || tok->type != ISL_TOKEN_VALUE) {
265 isl_stream_error(s, tok, "expecting constant value");
266 goto error;
269 isl_int_fdiv_q(*f, *f, tok->u.v);
271 isl_token_free(tok);
273 return isl_stat_ok;
274 error:
275 isl_token_free(tok);
276 return isl_stat_error;
279 static isl_stat accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
281 struct isl_token *tok;
283 tok = next_token(s);
284 if (!tok || tok->type != ISL_TOKEN_VALUE) {
285 isl_stream_error(s, tok, "expecting constant value");
286 goto error;
289 isl_int_mul(*f, *f, tok->u.v);
291 isl_token_free(tok);
293 if (isl_stream_eat_if_available(s, '*'))
294 return accept_cst_factor(s, f);
296 return isl_stat_ok;
297 error:
298 isl_token_free(tok);
299 return isl_stat_error;
302 /* Given an affine expression aff, return an affine expression
303 * for aff % d, with d the next token on the stream, which is
304 * assumed to be a constant.
306 * We introduce an integer division q = [aff/d] and the result
307 * is set to aff - d q.
309 static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
310 struct vars *v, __isl_take isl_pw_aff *aff)
312 struct isl_token *tok;
313 isl_pw_aff *q;
315 tok = next_token(s);
316 if (!tok || tok->type != ISL_TOKEN_VALUE) {
317 isl_stream_error(s, tok, "expecting constant value");
318 goto error;
321 q = isl_pw_aff_copy(aff);
322 q = isl_pw_aff_scale_down(q, tok->u.v);
323 q = isl_pw_aff_floor(q);
324 q = isl_pw_aff_scale(q, tok->u.v);
326 aff = isl_pw_aff_sub(aff, q);
328 isl_token_free(tok);
329 return aff;
330 error:
331 isl_pw_aff_free(aff);
332 isl_token_free(tok);
333 return NULL;
336 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
337 __isl_take isl_space *space, struct vars *v);
338 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
339 __isl_take isl_space *space, struct vars *v);
341 static __isl_give isl_pw_aff *accept_minmax(__isl_keep isl_stream *s,
342 __isl_take isl_space *space, struct vars *v)
344 struct isl_token *tok;
345 isl_pw_aff_list *list = NULL;
346 int min;
348 tok = isl_stream_next_token(s);
349 if (!tok)
350 goto error;
351 min = tok->type == ISL_TOKEN_MIN;
352 isl_token_free(tok);
354 if (isl_stream_eat(s, '('))
355 goto error;
357 list = accept_affine_list(s, isl_space_copy(space), v);
358 if (!list)
359 goto error;
361 if (isl_stream_eat(s, ')'))
362 goto error;
364 isl_space_free(space);
365 return min ? isl_pw_aff_list_min(list) : isl_pw_aff_list_max(list);
366 error:
367 isl_space_free(space);
368 isl_pw_aff_list_free(list);
369 return NULL;
372 /* Is "tok" the start of an integer division?
374 static int is_start_of_div(struct isl_token *tok)
376 if (!tok)
377 return 0;
378 if (tok->type == '[')
379 return 1;
380 if (tok->type == ISL_TOKEN_FLOOR)
381 return 1;
382 if (tok->type == ISL_TOKEN_CEIL)
383 return 1;
384 if (tok->type == ISL_TOKEN_FLOORD)
385 return 1;
386 if (tok->type == ISL_TOKEN_CEILD)
387 return 1;
388 return 0;
391 /* Read an integer division from "s" and return it as an isl_pw_aff.
393 * The integer division can be of the form
395 * [<affine expression>]
396 * floor(<affine expression>)
397 * ceil(<affine expression>)
398 * floord(<affine expression>,<denominator>)
399 * ceild(<affine expression>,<denominator>)
401 static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
402 __isl_take isl_space *space, struct vars *v)
404 struct isl_token *tok;
405 int f = 0;
406 int c = 0;
407 int extra = 0;
408 isl_pw_aff *pwaff = NULL;
410 if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
411 extra = f = 1;
412 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
413 extra = c = 1;
414 else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
415 f = 1;
416 else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
417 c = 1;
418 if (f || c) {
419 if (isl_stream_eat(s, '('))
420 goto error;
421 } else {
422 if (isl_stream_eat(s, '['))
423 goto error;
426 pwaff = accept_affine(s, isl_space_copy(space), v);
428 if (extra) {
429 if (isl_stream_eat(s, ','))
430 goto error;
432 tok = next_token(s);
433 if (!tok)
434 goto error;
435 if (tok->type != ISL_TOKEN_VALUE) {
436 isl_stream_error(s, tok, "expected denominator");
437 isl_stream_push_token(s, tok);
438 goto error;
440 pwaff = isl_pw_aff_scale_down(pwaff, tok->u.v);
441 isl_token_free(tok);
444 if (c)
445 pwaff = isl_pw_aff_ceil(pwaff);
446 else
447 pwaff = isl_pw_aff_floor(pwaff);
449 if (f || c) {
450 if (isl_stream_eat(s, ')'))
451 goto error;
452 } else {
453 if (isl_stream_eat(s, ']'))
454 goto error;
457 isl_space_free(space);
458 return pwaff;
459 error:
460 isl_space_free(space);
461 isl_pw_aff_free(pwaff);
462 return NULL;
465 /* Divide "pa" by an integer constant read from the stream.
467 static __isl_give isl_pw_aff *pw_aff_div_by_cst(__isl_keep isl_stream *s,
468 __isl_take isl_pw_aff *pa)
470 isl_int f;
471 isl_int_init(f);
472 isl_int_set_si(f, 1);
473 if (accept_cst_factor(s, &f) < 0)
474 pa = isl_pw_aff_free(pa);
475 pa = isl_pw_aff_scale_down(pa, f);
476 isl_int_clear(f);
478 return pa;
481 static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
482 __isl_take isl_space *space, struct vars *v)
484 struct isl_token *tok = NULL;
485 isl_pw_aff *res = NULL;
487 tok = next_token(s);
488 if (!tok) {
489 isl_stream_error(s, NULL, "unexpected EOF");
490 goto error;
493 if (tok->type == ISL_TOKEN_AFF) {
494 res = isl_pw_aff_copy(tok->u.pwaff);
495 isl_token_free(tok);
496 } else if (tok->type == ISL_TOKEN_IDENT) {
497 int n = v->n;
498 int pos = vars_pos(v, tok->u.s, -1);
499 isl_aff *aff;
501 if (pos < 0)
502 goto error;
503 if (pos >= n) {
504 vars_drop(v, v->n - n);
505 isl_stream_error(s, tok, "unknown identifier");
506 goto error;
509 aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(space)));
510 if (!aff)
511 goto error;
512 aff->v = isl_vec_set_element_si(aff->v, 2 + pos, 1);
513 if (!aff->v)
514 aff = isl_aff_free(aff);
515 res = isl_pw_aff_from_aff(aff);
516 isl_token_free(tok);
517 } else if (tok->type == ISL_TOKEN_VALUE) {
518 if (isl_stream_eat_if_available(s, '*')) {
519 res = accept_affine_factor(s, isl_space_copy(space), v);
520 res = isl_pw_aff_scale(res, tok->u.v);
521 } else {
522 isl_local_space *ls;
523 isl_aff *aff;
524 ls = isl_local_space_from_space(isl_space_copy(space));
525 aff = isl_aff_zero_on_domain(ls);
526 aff = isl_aff_add_constant(aff, tok->u.v);
527 res = isl_pw_aff_from_aff(aff);
529 isl_token_free(tok);
530 } else if (tok->type == '(') {
531 isl_token_free(tok);
532 tok = NULL;
533 res = accept_affine(s, isl_space_copy(space), v);
534 if (!res)
535 goto error;
536 if (isl_stream_eat(s, ')'))
537 goto error;
538 } else if (is_start_of_div(tok)) {
539 isl_stream_push_token(s, tok);
540 tok = NULL;
541 res = accept_div(s, isl_space_copy(space), v);
542 } else if (tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX) {
543 isl_stream_push_token(s, tok);
544 tok = NULL;
545 res = accept_minmax(s, isl_space_copy(space), v);
546 } else {
547 isl_stream_error(s, tok, "expecting factor");
548 goto error;
550 if (isl_stream_eat_if_available(s, '%') ||
551 isl_stream_eat_if_available(s, ISL_TOKEN_MOD)) {
552 isl_space_free(space);
553 return affine_mod(s, v, res);
555 if (isl_stream_eat_if_available(s, '*')) {
556 isl_int f;
557 isl_int_init(f);
558 isl_int_set_si(f, 1);
559 if (accept_cst_factor(s, &f) < 0) {
560 isl_int_clear(f);
561 goto error2;
563 res = isl_pw_aff_scale(res, f);
564 isl_int_clear(f);
566 if (isl_stream_eat_if_available(s, '/'))
567 res = pw_aff_div_by_cst(s, res);
568 if (isl_stream_eat_if_available(s, ISL_TOKEN_INT_DIV))
569 res = isl_pw_aff_floor(pw_aff_div_by_cst(s, res));
571 isl_space_free(space);
572 return res;
573 error:
574 isl_token_free(tok);
575 error2:
576 isl_pw_aff_free(res);
577 isl_space_free(space);
578 return NULL;
581 static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
583 isl_aff *aff;
584 isl_space *space;
586 space = isl_pw_aff_get_domain_space(pwaff);
587 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
588 aff = isl_aff_add_constant(aff, v);
590 return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
593 /* Return a piecewise affine expression defined on the specified domain
594 * that represents NaN.
596 static __isl_give isl_pw_aff *nan_on_domain(__isl_keep isl_space *space)
598 return isl_pw_aff_nan_on_domain_space(isl_space_copy(space));
601 static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
602 __isl_take isl_space *space, struct vars *v)
604 struct isl_token *tok = NULL;
605 isl_local_space *ls;
606 isl_pw_aff *res;
607 int sign = 1;
609 ls = isl_local_space_from_space(isl_space_copy(space));
610 res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
611 if (!res)
612 goto error;
614 for (;;) {
615 tok = next_token(s);
616 if (!tok) {
617 isl_stream_error(s, NULL, "unexpected EOF");
618 goto error;
620 if (tok->type == '-') {
621 sign = -sign;
622 isl_token_free(tok);
623 continue;
625 if (tok->type == '(' || is_start_of_div(tok) ||
626 tok->type == ISL_TOKEN_MIN || tok->type == ISL_TOKEN_MAX ||
627 tok->type == ISL_TOKEN_IDENT ||
628 tok->type == ISL_TOKEN_AFF) {
629 isl_pw_aff *term;
630 isl_stream_push_token(s, tok);
631 tok = NULL;
632 term = accept_affine_factor(s,
633 isl_space_copy(space), v);
634 if (sign < 0)
635 res = isl_pw_aff_sub(res, term);
636 else
637 res = isl_pw_aff_add(res, term);
638 if (!res)
639 goto error;
640 sign = 1;
641 } else if (tok->type == ISL_TOKEN_VALUE) {
642 if (sign < 0)
643 isl_int_neg(tok->u.v, tok->u.v);
644 if (isl_stream_eat_if_available(s, '*') ||
645 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
646 isl_pw_aff *term;
647 term = accept_affine_factor(s,
648 isl_space_copy(space), v);
649 term = isl_pw_aff_scale(term, tok->u.v);
650 res = isl_pw_aff_add(res, term);
651 if (!res)
652 goto error;
653 } else {
654 if (isl_stream_eat_if_available(s,
655 ISL_TOKEN_INT_DIV) &&
656 int_div_by_cst(s, &tok->u.v) < 0)
657 goto error;
658 res = add_cst(res, tok->u.v);
660 sign = 1;
661 } else if (tok->type == ISL_TOKEN_NAN) {
662 res = isl_pw_aff_add(res, nan_on_domain(space));
663 } else {
664 isl_stream_error(s, tok, "unexpected isl_token");
665 isl_stream_push_token(s, tok);
666 isl_pw_aff_free(res);
667 isl_space_free(space);
668 return NULL;
670 isl_token_free(tok);
672 tok = next_token(s);
673 if (tok && tok->type == '-') {
674 sign = -sign;
675 isl_token_free(tok);
676 } else if (tok && tok->type == '+') {
677 /* nothing */
678 isl_token_free(tok);
679 } else if (tok && tok->type == ISL_TOKEN_VALUE &&
680 isl_int_is_neg(tok->u.v)) {
681 isl_stream_push_token(s, tok);
682 } else {
683 if (tok)
684 isl_stream_push_token(s, tok);
685 break;
689 isl_space_free(space);
690 return res;
691 error:
692 isl_space_free(space);
693 isl_token_free(tok);
694 isl_pw_aff_free(res);
695 return NULL;
698 /* Is "type" the type of a comparison operator between lists
699 * of affine expressions?
701 static int is_list_comparator_type(int type)
703 switch (type) {
704 case ISL_TOKEN_LEX_LT:
705 case ISL_TOKEN_LEX_GT:
706 case ISL_TOKEN_LEX_LE:
707 case ISL_TOKEN_LEX_GE:
708 return 1;
709 default:
710 return 0;
714 static int is_comparator(struct isl_token *tok)
716 if (!tok)
717 return 0;
718 if (is_list_comparator_type(tok->type))
719 return 1;
721 switch (tok->type) {
722 case ISL_TOKEN_LT:
723 case ISL_TOKEN_GT:
724 case ISL_TOKEN_LE:
725 case ISL_TOKEN_GE:
726 case ISL_TOKEN_NE:
727 case '=':
728 return 1;
729 default:
730 return 0;
734 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
735 struct vars *v, __isl_take isl_map *map, int rational);
736 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
737 __isl_take isl_space *space, struct vars *v, int rational);
739 /* Accept a ternary operator, given the first argument.
741 static __isl_give isl_pw_aff *accept_ternary(__isl_keep isl_stream *s,
742 __isl_take isl_map *cond, struct vars *v, int rational)
744 isl_space *space;
745 isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
747 if (!cond)
748 return NULL;
750 if (isl_stream_eat(s, '?'))
751 goto error;
753 space = isl_space_wrap(isl_map_get_space(cond));
754 pwaff1 = accept_extended_affine(s, space, v, rational);
755 if (!pwaff1)
756 goto error;
758 if (isl_stream_eat(s, ':'))
759 goto error;
761 space = isl_pw_aff_get_domain_space(pwaff1);
762 pwaff2 = accept_extended_affine(s, space, v, rational);
763 if (!pwaff2)
764 goto error;
766 pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
767 return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
768 error:
769 isl_map_free(cond);
770 isl_pw_aff_free(pwaff1);
771 isl_pw_aff_free(pwaff2);
772 return NULL;
775 /* Set *line and *col to those of the next token, if any.
777 static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
779 struct isl_token *tok;
781 tok = isl_stream_next_token(s);
782 if (!tok)
783 return;
785 *line = tok->line;
786 *col = tok->col;
787 isl_stream_push_token(s, tok);
790 /* Push a token encapsulating "pa" onto "s", with the given
791 * line and column.
793 static isl_stat push_aff(__isl_keep isl_stream *s, int line, int col,
794 __isl_take isl_pw_aff *pa)
796 struct isl_token *tok;
798 tok = isl_token_new(s->ctx, line, col, 0);
799 if (!tok)
800 goto error;
801 tok->type = ISL_TOKEN_AFF;
802 tok->u.pwaff = pa;
803 isl_stream_push_token(s, tok);
805 return isl_stat_ok;
806 error:
807 isl_pw_aff_free(pa);
808 return isl_stat_error;
811 /* Is the next token a comparison operator?
813 static int next_is_comparator(__isl_keep isl_stream *s)
815 int is_comp;
816 struct isl_token *tok;
818 tok = isl_stream_next_token(s);
819 if (!tok)
820 return 0;
822 is_comp = is_comparator(tok);
823 isl_stream_push_token(s, tok);
825 return is_comp;
828 /* Accept an affine expression that may involve ternary operators.
829 * We first read an affine expression.
830 * If it is not followed by a comparison operator, we simply return it.
831 * Otherwise, we assume the affine expression is part of the first
832 * argument of a ternary operator and try to parse that.
834 static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
835 __isl_take isl_space *space, struct vars *v, int rational)
837 isl_map *cond;
838 isl_pw_aff *pwaff;
839 int line = -1, col = -1;
841 set_current_line_col(s, &line, &col);
843 pwaff = accept_affine(s, space, v);
844 if (rational)
845 pwaff = isl_pw_aff_set_rational(pwaff);
846 if (!pwaff)
847 return NULL;
848 if (!next_is_comparator(s))
849 return pwaff;
851 space = isl_pw_aff_get_domain_space(pwaff);
852 cond = isl_map_universe(isl_space_unwrap(space));
854 if (push_aff(s, line, col, pwaff) < 0)
855 cond = isl_map_free(cond);
856 if (!cond)
857 return NULL;
859 cond = read_formula(s, v, cond, rational);
861 return accept_ternary(s, cond, v, rational);
864 static __isl_give isl_map *read_var_def(__isl_keep isl_stream *s,
865 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
866 int rational)
868 isl_pw_aff *def;
869 isl_size pos;
870 isl_map *def_map;
872 if (type == isl_dim_param)
873 pos = isl_map_dim(map, isl_dim_param);
874 else {
875 pos = isl_map_dim(map, isl_dim_in);
876 if (type == isl_dim_out) {
877 isl_size n_out = isl_map_dim(map, isl_dim_out);
878 if (pos < 0 || n_out < 0)
879 return isl_map_free(map);
880 pos += n_out;
882 type = isl_dim_in;
884 if (pos < 0)
885 return isl_map_free(map);
886 --pos;
888 def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
889 v, rational);
890 def_map = isl_map_from_pw_aff(def);
891 def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
892 def_map = isl_set_unwrap(isl_map_domain(def_map));
894 map = isl_map_intersect(map, def_map);
896 return map;
899 static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
900 __isl_take isl_space *space, struct vars *v)
902 isl_pw_aff *pwaff;
903 isl_pw_aff_list *list;
904 struct isl_token *tok = NULL;
906 pwaff = accept_affine(s, isl_space_copy(space), v);
907 list = isl_pw_aff_list_from_pw_aff(pwaff);
908 if (!list)
909 goto error;
911 for (;;) {
912 tok = isl_stream_next_token(s);
913 if (!tok) {
914 isl_stream_error(s, NULL, "unexpected EOF");
915 goto error;
917 if (tok->type != ',') {
918 isl_stream_push_token(s, tok);
919 break;
921 isl_token_free(tok);
923 pwaff = accept_affine(s, isl_space_copy(space), v);
924 list = isl_pw_aff_list_concat(list,
925 isl_pw_aff_list_from_pw_aff(pwaff));
926 if (!list)
927 goto error;
930 isl_space_free(space);
931 return list;
932 error:
933 isl_space_free(space);
934 isl_pw_aff_list_free(list);
935 return NULL;
938 static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
939 struct vars *v, __isl_take isl_map *map, int rational)
941 struct isl_token *tok;
943 while ((tok = isl_stream_next_token(s)) != NULL) {
944 int p;
945 int n = v->n;
947 if (tok->type != ISL_TOKEN_IDENT)
948 break;
950 p = vars_pos(v, tok->u.s, -1);
951 if (p < 0)
952 goto error;
953 if (p < n) {
954 isl_stream_error(s, tok, "expecting unique identifier");
955 goto error;
958 map = isl_map_add_dims(map, isl_dim_out, 1);
960 isl_token_free(tok);
961 tok = isl_stream_next_token(s);
962 if (tok && tok->type == '=') {
963 isl_token_free(tok);
964 map = read_var_def(s, map, isl_dim_out, v, rational);
965 tok = isl_stream_next_token(s);
968 if (!tok || tok->type != ',')
969 break;
971 isl_token_free(tok);
973 if (tok)
974 isl_stream_push_token(s, tok);
976 return map;
977 error:
978 isl_token_free(tok);
979 isl_map_free(map);
980 return NULL;
983 static int next_is_tuple(__isl_keep isl_stream *s)
985 struct isl_token *tok;
986 int is_tuple;
988 tok = isl_stream_next_token(s);
989 if (!tok)
990 return 0;
991 if (tok->type == '[') {
992 isl_stream_push_token(s, tok);
993 return 1;
995 if (tok->type != ISL_TOKEN_IDENT && !tok->is_keyword) {
996 isl_stream_push_token(s, tok);
997 return 0;
1000 is_tuple = isl_stream_next_token_is(s, '[');
1002 isl_stream_push_token(s, tok);
1004 return is_tuple;
1007 /* Does the next token mark the end of a tuple element?
1009 static int next_is_end_tuple_element(__isl_keep isl_stream *s)
1011 return isl_stream_next_token_is(s, ',') ||
1012 isl_stream_next_token_is(s, ']');
1015 /* Is the next token one that necessarily forms the start of a condition?
1017 static int next_is_condition_start(__isl_keep isl_stream *s)
1019 return isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1020 isl_stream_next_token_is(s, ISL_TOKEN_NOT) ||
1021 isl_stream_next_token_is(s, ISL_TOKEN_TRUE) ||
1022 isl_stream_next_token_is(s, ISL_TOKEN_FALSE) ||
1023 isl_stream_next_token_is(s, ISL_TOKEN_MAP);
1026 /* Is "pa" an expression in term of earlier dimensions?
1027 * The alternative is that the dimension is defined to be equal to itself,
1028 * meaning that it has a universe domain and an expression that depends
1029 * on itself. "i" is the position of the expression in a sequence
1030 * of "n" expressions. The final dimensions of "pa" correspond to
1031 * these "n" expressions.
1033 static isl_bool pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
1035 isl_aff *aff;
1037 if (!pa)
1038 return isl_bool_error;
1039 if (pa->n != 1)
1040 return isl_bool_true;
1041 if (!isl_set_plain_is_universe(pa->p[0].set))
1042 return isl_bool_true;
1044 aff = pa->p[0].aff;
1045 if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
1046 return isl_bool_true;
1047 return isl_bool_false;
1050 /* Does the tuple contain any dimensions that are defined
1051 * in terms of earlier dimensions?
1053 static isl_bool tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
1055 int i;
1056 isl_size n;
1057 isl_bool has_expr = isl_bool_false;
1058 isl_pw_aff *pa;
1060 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1061 if (n < 0)
1062 return isl_bool_error;
1063 for (i = 0; i < n; ++i) {
1064 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1065 has_expr = pw_aff_is_expr(pa, i, n);
1066 isl_pw_aff_free(pa);
1067 if (has_expr < 0 || has_expr)
1068 break;
1071 return has_expr;
1074 /* Set the name of dimension "pos" in "space" to "name".
1075 * During printing, we add primes if the same name appears more than once
1076 * to distinguish the occurrences. Here, we remove those primes from "name"
1077 * before setting the name of the dimension.
1079 static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
1080 int pos, char *name)
1082 char *prime;
1084 if (!name)
1085 return space;
1087 prime = strchr(name, '\'');
1088 if (prime)
1089 *prime = '\0';
1090 space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1091 if (prime)
1092 *prime = '\'';
1094 return space;
1097 /* Set the name of the last (output) dimension of "space" to "name",
1098 * ignoring any primes in "name".
1100 static __isl_give isl_space *space_set_last_dim_name(
1101 __isl_take isl_space *space, char *name)
1103 isl_size pos;
1105 pos = isl_space_dim(space, isl_dim_out);
1106 if (pos < 0)
1107 return isl_space_free(space);
1108 return space_set_dim_name(space, pos - 1, name);
1111 /* Construct an isl_pw_aff defined on a "space" (with v->n variables)
1112 * that is equal to the last of those variables.
1114 static __isl_give isl_pw_aff *identity_tuple_el_on_space(
1115 __isl_take isl_space *space, struct vars *v)
1117 isl_aff *aff;
1119 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1120 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1121 return isl_pw_aff_from_aff(aff);
1124 /* Construct an isl_pw_aff defined on the domain space of "pa"
1125 * that is equal to the last variable in "v".
1127 * That is, if D is the domain space of "pa", then construct
1129 * D[..., i] -> i.
1131 static __isl_give isl_pw_aff *init_range(__isl_keep isl_pw_aff *pa,
1132 struct vars *v)
1134 isl_space *space;
1136 space = isl_pw_aff_get_domain_space(pa);
1137 return identity_tuple_el_on_space(space, v);
1140 /* Impose the lower bound "lower" on the variable represented by "range_pa".
1142 * In particular, "range_pa" is of the form
1144 * D[..., i] -> i : C
1146 * with D also the domains space of "lower' and "C" some constraints.
1148 * Return the expression
1150 * D[..., i] -> i : C and i >= lower
1152 static __isl_give isl_pw_aff *set_lower(__isl_take isl_pw_aff *range_pa,
1153 __isl_take isl_pw_aff *lower)
1155 isl_set *range;
1157 range = isl_pw_aff_ge_set(isl_pw_aff_copy(range_pa), lower);
1158 return isl_pw_aff_intersect_domain(range_pa, range);
1161 /* Impose the upper bound "upper" on the variable represented by "range_pa".
1163 * In particular, "range_pa" is of the form
1165 * D[..., i] -> i : C
1167 * with D also the domains space of "upper' and "C" some constraints.
1169 * Return the expression
1171 * D[..., i] -> i : C and i <= upper
1173 static __isl_give isl_pw_aff *set_upper(__isl_take isl_pw_aff *range_pa,
1174 __isl_take isl_pw_aff *upper)
1176 isl_set *range;
1178 range = isl_pw_aff_le_set(isl_pw_aff_copy(range_pa), upper);
1179 return isl_pw_aff_intersect_domain(range_pa, range);
1182 /* Construct a piecewise affine expression corresponding
1183 * to the last variable in "v" that is greater than or equal to "pa".
1185 * In particular, if D is the domain space of "pa",
1186 * then construct the expression
1188 * D[..., i] -> i,
1190 * impose lower bound "pa" and return
1192 * D[..., i] -> i : i >= pa
1194 static __isl_give isl_pw_aff *construct_lower(__isl_take isl_pw_aff *pa,
1195 struct vars *v)
1197 return set_lower(init_range(pa, v), pa);
1200 /* Construct a piecewise affine expression corresponding
1201 * to the last variable in "v" that is smaller than or equal to "pa".
1203 * In particular, if D is the domain space of "pa",
1204 * then construct the expression
1206 * D[..., i] -> i,
1208 * impose lower bound "pa" and return
1210 * D[..., i] -> i : i <= pa
1212 static __isl_give isl_pw_aff *construct_upper(__isl_take isl_pw_aff *pa,
1213 struct vars *v)
1215 return set_upper(init_range(pa, v), pa);
1218 /* Construct a piecewise affine expression corresponding
1219 * to the last variable in "v" that ranges between "pa" and "pa2".
1221 * In particular, if D is the domain space of "pa" (and "pa2"),
1222 * then construct the expression
1224 * D[..., i] -> i,
1226 * impose lower bound "pa" and upper bound "pa2" and return
1228 * D[..., i] -> i : pa <= i <= pa2
1230 static __isl_give isl_pw_aff *construct_range(__isl_take isl_pw_aff *pa,
1231 __isl_take isl_pw_aff *pa2, struct vars *v)
1233 return set_upper(set_lower(init_range(pa, v), pa), pa2);
1236 static int resolve_paren_expr(__isl_keep isl_stream *s,
1237 struct vars *v, __isl_take isl_map *map, int rational);
1239 /* Given that the (piecewise) affine expression "pa"
1240 * has just been parsed, followed by a colon,
1241 * continue parsing as part of a piecewise affine expression.
1243 * In particular, check if the colon is followed by a condition.
1244 * If so, parse the conditions(a) on "pa" and include them in the domain.
1245 * Otherwise, if the colon is followed by another (piecewise) affine expression
1246 * then consider the two expressions as endpoints of a range of values and
1247 * return a piecewise affine expression that takes values in that range.
1248 * Note that an affine expression followed by a comparison operator
1249 * is considered to be part of a condition.
1250 * If the colon is not followed by anything (inside the tuple element),
1251 * then consider "pa" as a lower bound on a range of values without upper bound
1252 * and return a piecewise affine expression that takes values in that range.
1254 static __isl_give isl_pw_aff *update_piecewise_affine_colon(
1255 __isl_take isl_pw_aff *pa, __isl_keep isl_stream *s,
1256 struct vars *v, int rational)
1258 isl_space *dom_space;
1259 isl_map *map;
1261 dom_space = isl_pw_aff_get_domain_space(pa);
1262 map = isl_map_universe(isl_space_from_domain(dom_space));
1264 if (isl_stream_next_token_is(s, '('))
1265 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1266 goto error;
1267 if (next_is_end_tuple_element(s)) {
1268 isl_map_free(map);
1269 return construct_lower(pa, v);
1271 if (!next_is_condition_start(s)) {
1272 int line = -1, col = -1;
1273 isl_space *space;
1274 isl_pw_aff *pa2;
1276 set_current_line_col(s, &line, &col);
1277 space = isl_space_wrap(isl_map_get_space(map));
1278 pa2 = accept_affine(s, space, v);
1279 if (rational)
1280 pa2 = isl_pw_aff_set_rational(pa2);
1281 if (!next_is_comparator(s)) {
1282 isl_map_free(map);
1283 pa2 = isl_pw_aff_domain_factor_domain(pa2);
1284 return construct_range(pa, pa2, v);
1286 if (push_aff(s, line, col, pa2) < 0)
1287 goto error;
1290 map = read_formula(s, v, map, rational);
1291 pa = isl_pw_aff_intersect_domain(pa, isl_map_domain(map));
1293 return pa;
1294 error:
1295 isl_map_free(map);
1296 isl_pw_aff_free(pa);
1297 return NULL;
1300 /* Accept a piecewise affine expression.
1302 * At the outer level, the piecewise affine expression may be of the form
1304 * aff1 : condition1; aff2 : conditions2; ...
1306 * or one of
1308 * aff :
1309 * aff1 : aff2
1310 * : aff
1313 * or simply
1315 * aff
1317 * each of the affine expressions may in turn include ternary operators.
1319 * If the first token is a colon, then the expression must be
1320 * ":" or ": aff2", depending on whether anything follows the colon
1321 * inside the tuple element.
1322 * The first is considered to represent an arbitrary value.
1323 * The second is considered to represent a range of values
1324 * with the given upper bound and no lower bound.
1326 * There may be parentheses around some subexpression of "aff1"
1327 * around "aff1" itself, around "aff1 : condition1" and/or
1328 * around the entire piecewise affine expression.
1329 * We therefore remove the opening parenthesis (if any) from the stream
1330 * in case the closing parenthesis follows the colon, but if the closing
1331 * parenthesis is the first thing in the stream after the parsed affine
1332 * expression, we push the parsed expression onto the stream and parse
1333 * again in case the parentheses enclose some subexpression of "aff1".
1335 static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1336 __isl_take isl_space *space, struct vars *v, int rational)
1338 isl_pw_aff *res;
1339 isl_space *res_space;
1341 if (isl_stream_eat_if_available(s, ':')) {
1342 if (next_is_end_tuple_element(s))
1343 return identity_tuple_el_on_space(space, v);
1344 else
1345 return construct_upper(accept_affine(s, space, v), v);
1348 res_space = isl_space_from_domain(isl_space_copy(space));
1349 res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1350 res = isl_pw_aff_empty(res_space);
1351 do {
1352 isl_pw_aff *pa;
1353 int seen_paren;
1354 int line = -1, col = -1;
1356 set_current_line_col(s, &line, &col);
1357 seen_paren = isl_stream_eat_if_available(s, '(');
1358 if (seen_paren)
1359 pa = accept_piecewise_affine(s, isl_space_copy(space),
1360 v, rational);
1361 else
1362 pa = accept_extended_affine(s, isl_space_copy(space),
1363 v, rational);
1364 if (seen_paren && isl_stream_eat_if_available(s, ')')) {
1365 seen_paren = 0;
1366 if (push_aff(s, line, col, pa) < 0)
1367 goto error;
1368 pa = accept_extended_affine(s, isl_space_copy(space),
1369 v, rational);
1371 if (isl_stream_eat_if_available(s, ':'))
1372 pa = update_piecewise_affine_colon(pa, s, v, rational);
1374 res = isl_pw_aff_union_add(res, pa);
1376 if (seen_paren && isl_stream_eat(s, ')'))
1377 goto error;
1378 } while (isl_stream_eat_if_available(s, ';'));
1380 isl_space_free(space);
1382 return res;
1383 error:
1384 isl_space_free(space);
1385 return isl_pw_aff_free(res);
1388 /* Read an affine expression from "s" for use in read_tuple.
1390 * accept_extended_affine requires a wrapped space as input.
1391 * read_tuple on the other hand expects each isl_pw_aff
1392 * to have an anonymous space. We therefore adjust the space
1393 * of the isl_pw_aff before returning it.
1395 static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1396 struct vars *v, int rational)
1398 isl_space *space;
1399 isl_pw_aff *def;
1401 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1403 def = accept_piecewise_affine(s, space, v, rational);
1404 def = isl_pw_aff_domain_factor_domain(def);
1406 return def;
1409 /* Read a list of tuple elements by calling "read_el" on each of them and
1410 * return a space with the same number of set dimensions derived from
1411 * the parameter space "space" and possibly updated by "read_el".
1412 * The elements in the list are separated by either "," or "][".
1413 * If "comma" is set then only "," is allowed.
1415 static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1416 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1417 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1418 struct vars *v, __isl_take isl_space *space, int rational,
1419 void *user),
1420 void *user)
1422 if (!space)
1423 return NULL;
1425 space = isl_space_set_from_params(space);
1427 if (isl_stream_next_token_is(s, ']'))
1428 return space;
1430 for (;;) {
1431 struct isl_token *tok;
1433 space = isl_space_add_dims(space, isl_dim_set, 1);
1435 space = read_el(s, v, space, rational, user);
1436 if (!space)
1437 return NULL;
1439 tok = isl_stream_next_token(s);
1440 if (!comma && tok && tok->type == ']' &&
1441 isl_stream_next_token_is(s, '[')) {
1442 isl_token_free(tok);
1443 tok = isl_stream_next_token(s);
1444 } else if (!tok || tok->type != ',') {
1445 if (tok)
1446 isl_stream_push_token(s, tok);
1447 break;
1450 isl_token_free(tok);
1453 return space;
1456 /* Read a tuple space from "s" derived from the parameter space "space".
1457 * Call "read_el" on each element in the tuples.
1459 static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1460 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1461 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1462 struct vars *v, __isl_take isl_space *space, int rational,
1463 void *user),
1464 void *user)
1466 struct isl_token *tok;
1467 char *name = NULL;
1468 isl_space *res = NULL;
1470 tok = isl_stream_next_token(s);
1471 if (!tok)
1472 goto error;
1473 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1474 name = strdup(tok->u.s);
1475 isl_token_free(tok);
1476 if (!name)
1477 goto error;
1478 } else
1479 isl_stream_push_token(s, tok);
1480 if (isl_stream_eat(s, '['))
1481 goto error;
1482 if (next_is_tuple(s)) {
1483 isl_space *out;
1484 res = read_tuple_space(s, v, isl_space_copy(space),
1485 rational, comma, read_el, user);
1486 if (isl_stream_eat(s, ISL_TOKEN_TO))
1487 goto error;
1488 out = read_tuple_space(s, v, isl_space_copy(space),
1489 rational, comma, read_el, user);
1490 res = isl_space_product(res, out);
1491 } else
1492 res = read_tuple_list(s, v, isl_space_copy(space),
1493 rational, comma, read_el, user);
1494 if (isl_stream_eat(s, ']'))
1495 goto error;
1497 if (name) {
1498 res = isl_space_set_tuple_name(res, isl_dim_set, name);
1499 free(name);
1502 isl_space_free(space);
1503 return res;
1504 error:
1505 free(name);
1506 isl_space_free(res);
1507 isl_space_free(space);
1508 return NULL;
1511 /* Construct an isl_pw_aff defined on a space with v->n variables
1512 * that is equal to the last of those variables.
1514 static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1516 isl_space *space;
1518 space = isl_space_set_alloc(v->ctx, 0, v->n);
1519 return identity_tuple_el_on_space(space, v);
1522 /* This function is called for each element in a tuple inside read_tuple.
1523 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1524 * over a space containing all variables in "v" defined so far.
1525 * The isl_pw_aff expresses the new variable in terms of earlier variables
1526 * if a definition is provided. Otherwise, it is represented as being
1527 * equal to itself.
1528 * Add the isl_pw_aff to *list.
1529 * If the new variable was named, then adjust "space" accordingly and
1530 * return the updated space.
1532 static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1533 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1535 isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1536 isl_pw_aff *pa;
1537 struct isl_token *tok;
1538 int new_name = 0;
1540 tok = next_token(s);
1541 if (!tok) {
1542 isl_stream_error(s, NULL, "unexpected EOF");
1543 return isl_space_free(space);
1546 if (tok->type == ISL_TOKEN_IDENT) {
1547 int n = v->n;
1548 int p = vars_pos(v, tok->u.s, -1);
1549 if (p < 0)
1550 goto error;
1551 new_name = p >= n;
1554 if (tok->type == '*') {
1555 if (vars_add_anon(v) < 0)
1556 goto error;
1557 isl_token_free(tok);
1558 pa = identity_tuple_el(v);
1559 } else if (new_name) {
1560 space = space_set_last_dim_name(space, v->v->name);
1561 isl_token_free(tok);
1562 if (isl_stream_eat_if_available(s, '='))
1563 pa = read_tuple_var_def(s, v, rational);
1564 else
1565 pa = identity_tuple_el(v);
1566 } else {
1567 isl_stream_push_token(s, tok);
1568 tok = NULL;
1569 if (vars_add_anon(v) < 0)
1570 goto error;
1571 pa = read_tuple_var_def(s, v, rational);
1574 *list = isl_pw_aff_list_add(*list, pa);
1575 if (!*list)
1576 return isl_space_free(space);
1578 return space;
1579 error:
1580 isl_token_free(tok);
1581 return isl_space_free(space);
1584 /* Read a tuple and represent it as an isl_multi_pw_aff.
1585 * The range space of the isl_multi_pw_aff is the space of the tuple.
1586 * The domain space is an anonymous space
1587 * with a dimension for each variable in the set of variables in "v",
1588 * including the variables in the range.
1589 * If a given dimension is not defined in terms of earlier dimensions in
1590 * the input, then the corresponding isl_pw_aff is set equal to one time
1591 * the variable corresponding to the dimension being defined.
1593 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1594 * Each element in this list is defined over a space representing
1595 * the variables defined so far. We need to adjust the earlier
1596 * elements to have as many variables in the domain as the final
1597 * element in the list.
1599 static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1600 struct vars *v, int rational, int comma)
1602 int i;
1603 isl_size n;
1604 isl_space *space;
1605 isl_pw_aff_list *list;
1607 space = isl_space_params_alloc(v->ctx, 0);
1608 list = isl_pw_aff_list_alloc(s->ctx, 0);
1609 space = read_tuple_space(s, v, space, rational, comma,
1610 &read_tuple_pw_aff_el, &list);
1611 n = isl_space_dim(space, isl_dim_set);
1612 if (n < 0)
1613 space = isl_space_free(space);
1614 for (i = 0; i + 1 < n; ++i) {
1615 isl_pw_aff *pa;
1617 pa = isl_pw_aff_list_get_pw_aff(list, i);
1618 pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1619 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1622 space = isl_space_from_range(space);
1623 space = isl_space_add_dims(space, isl_dim_in, v->n);
1624 return isl_multi_pw_aff_from_pw_aff_list(space, list);
1627 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1628 * We first create the appropriate space in "map" based on the range
1629 * space of this isl_multi_pw_aff. Then, we add equalities based
1630 * on the affine expressions. These live in an anonymous space,
1631 * however, so we first need to reset the space to that of "map".
1633 static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1634 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1635 int rational)
1637 int i;
1638 isl_size n;
1639 isl_ctx *ctx;
1640 isl_space *space = NULL;
1642 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1643 if (!map || n < 0)
1644 goto error;
1645 ctx = isl_multi_pw_aff_get_ctx(tuple);
1646 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1647 if (!space)
1648 goto error;
1650 if (type == isl_dim_param) {
1651 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1652 isl_space_is_wrapping(space)) {
1653 isl_die(ctx, isl_error_invalid,
1654 "parameter tuples cannot be named or nested",
1655 goto error);
1657 map = isl_map_add_dims(map, type, n);
1658 for (i = 0; i < n; ++i) {
1659 isl_id *id;
1660 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1661 isl_die(ctx, isl_error_invalid,
1662 "parameters must be named",
1663 goto error);
1664 id = isl_space_get_dim_id(space, isl_dim_set, i);
1665 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1667 } else if (type == isl_dim_in) {
1668 isl_set *set;
1670 set = isl_set_universe(isl_space_copy(space));
1671 if (rational)
1672 set = isl_set_set_rational(set);
1673 set = isl_set_intersect_params(set, isl_map_params(map));
1674 map = isl_map_from_domain(set);
1675 } else {
1676 isl_set *set;
1678 set = isl_set_universe(isl_space_copy(space));
1679 if (rational)
1680 set = isl_set_set_rational(set);
1681 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1684 for (i = 0; i < n; ++i) {
1685 isl_pw_aff *pa;
1686 isl_space *space;
1687 isl_aff *aff;
1688 isl_set *set;
1689 isl_map *map_i;
1691 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1692 space = isl_pw_aff_get_domain_space(pa);
1693 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1694 aff = isl_aff_add_coefficient_si(aff,
1695 isl_dim_in, v->n - n + i, -1);
1696 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1697 if (rational)
1698 pa = isl_pw_aff_set_rational(pa);
1699 set = isl_pw_aff_zero_set(pa);
1700 map_i = isl_map_from_range(set);
1701 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1702 map = isl_map_intersect(map, map_i);
1705 isl_space_free(space);
1706 isl_multi_pw_aff_free(tuple);
1707 return map;
1708 error:
1709 isl_space_free(space);
1710 isl_multi_pw_aff_free(tuple);
1711 isl_map_free(map);
1712 return NULL;
1715 /* Read a tuple from "s" and add it to "map".
1716 * The tuple is initially represented as an isl_multi_pw_aff and
1717 * then added to "map".
1719 static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1720 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1721 int rational, int comma)
1723 isl_multi_pw_aff *tuple;
1725 tuple = read_tuple(s, v, rational, comma);
1726 if (!tuple)
1727 return isl_map_free(map);
1729 return map_from_tuple(tuple, map, type, v, rational);
1732 /* Read the parameter domain of an expression from "s" (if any) and
1733 * check that it does not involve any constraints.
1734 * "v" contains a description of the identifiers parsed so far
1735 * (of which there should not be any at this point) and is extended
1736 * by this function.
1738 static __isl_give isl_set *read_universe_params(__isl_keep isl_stream *s,
1739 struct vars *v)
1741 isl_set *dom;
1743 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
1744 if (next_is_tuple(s)) {
1745 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
1746 if (isl_stream_eat(s, ISL_TOKEN_TO))
1747 return isl_set_free(dom);
1749 if (!isl_set_plain_is_universe(dom))
1750 isl_die(s->ctx, isl_error_invalid,
1751 "expecting universe parameter domain",
1752 return isl_set_free(dom));
1754 return dom;
1757 /* This function is called for each element in a tuple inside read_space_tuples.
1758 * Add a new variable to "v" and adjust "space" accordingly
1759 * if the variable has a name.
1761 static __isl_give isl_space *read_tuple_id(__isl_keep isl_stream *s,
1762 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1764 struct isl_token *tok;
1766 tok = next_token(s);
1767 if (!tok) {
1768 isl_stream_error(s, NULL, "unexpected EOF");
1769 return isl_space_free(space);
1772 if (tok->type == ISL_TOKEN_IDENT) {
1773 int n = v->n;
1774 int p = vars_pos(v, tok->u.s, -1);
1775 if (p < 0)
1776 goto error;
1777 if (p < n) {
1778 isl_stream_error(s, tok, "expecting fresh identifier");
1779 goto error;
1781 space = space_set_last_dim_name(space, v->v->name);
1782 } else if (tok->type == '*') {
1783 if (vars_add_anon(v) < 0)
1784 goto error;
1785 } else {
1786 isl_stream_error(s, tok, "expecting identifier or '*'");
1787 goto error;
1790 isl_token_free(tok);
1791 return space;
1792 error:
1793 isl_token_free(tok);
1794 return isl_space_free(space);
1797 /* Given a parameter space "params", extend it with one or two tuples
1798 * read from "s".
1799 * "v" contains a description of the identifiers parsed so far and is extended
1800 * by this function.
1802 static __isl_give isl_space *read_space_tuples(__isl_keep isl_stream *s,
1803 struct vars *v, __isl_take isl_space *params)
1805 isl_space *space, *ran;
1807 space = read_tuple_space(s, v, isl_space_copy(params), 1, 1,
1808 &read_tuple_id, NULL);
1809 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
1810 ran = read_tuple_space(s, v, isl_space_copy(params), 1, 1,
1811 &read_tuple_id, NULL);
1812 space = isl_space_unwrap(isl_space_product(space, ran));
1814 isl_space_free(params);
1816 return space;
1819 /* Read an isl_space object from "s".
1821 * First read the parameters (if any).
1823 * Then check if the description is of the special form "{ : }",
1824 * in which case it represents a parameter space.
1825 * Otherwise, it has one or two tuples.
1827 __isl_give isl_space *isl_stream_read_space(__isl_keep isl_stream *s)
1829 struct vars *v;
1830 isl_set *dom;
1831 isl_space *space;
1833 v = vars_new(s->ctx);
1834 if (!v)
1835 return NULL;
1836 dom = read_universe_params(s, v);
1837 space = isl_set_get_space(dom);
1838 isl_set_free(dom);
1840 if (isl_stream_eat(s, '{'))
1841 goto error;
1843 if (!isl_stream_eat_if_available(s, ':'))
1844 space = read_space_tuples(s, v, space);
1846 if (isl_stream_eat(s, '}'))
1847 goto error;
1849 vars_free(v);
1850 return space;
1851 error:
1852 vars_free(v);
1853 isl_space_free(space);
1854 return NULL;
1857 /* Read an isl_space object from "str".
1859 __isl_give isl_space *isl_space_read_from_str(isl_ctx *ctx,
1860 const char *str)
1862 struct isl_space *space;
1863 isl_stream *s = isl_stream_new_str(ctx, str);
1864 if (!s)
1865 return NULL;
1866 space = isl_stream_read_space(s);
1867 isl_stream_free(s);
1868 return space;
1871 /* Given two equal-length lists of piecewise affine expression with the space
1872 * of "set" as domain, construct a set in the same space that expresses
1873 * that "left" and "right" satisfy the comparison "type".
1875 * A space is constructed of the same dimension as the number of elements
1876 * in the two lists. The comparison is then expressed in a map from
1877 * this space to itself and wrapped into a set. Finally the two lists
1878 * of piecewise affine expressions are plugged into this set.
1880 * Let S be the space of "set" and T the constructed space.
1881 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1882 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1883 * while the comparison is first expressed in T -> T, then [T -> T]
1884 * and finally in S.
1886 static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1887 __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1889 isl_space *space;
1890 isl_size n;
1891 isl_multi_pw_aff *mpa1, *mpa2;
1893 n = isl_pw_aff_list_n_pw_aff(left);
1894 if (!set || n < 0 || !right)
1895 goto error;
1897 space = isl_set_get_space(set);
1898 space = isl_space_from_domain(space);
1899 space = isl_space_add_dims(space, isl_dim_out, n);
1900 mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1901 mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1902 mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1904 space = isl_space_range(space);
1905 switch (type) {
1906 case ISL_TOKEN_LEX_LT:
1907 set = isl_map_wrap(isl_map_lex_lt(space));
1908 break;
1909 case ISL_TOKEN_LEX_GT:
1910 set = isl_map_wrap(isl_map_lex_gt(space));
1911 break;
1912 case ISL_TOKEN_LEX_LE:
1913 set = isl_map_wrap(isl_map_lex_le(space));
1914 break;
1915 case ISL_TOKEN_LEX_GE:
1916 set = isl_map_wrap(isl_map_lex_ge(space));
1917 break;
1918 default:
1919 isl_multi_pw_aff_free(mpa1);
1920 isl_space_free(space);
1921 isl_die(isl_set_get_ctx(set), isl_error_internal,
1922 "unhandled list comparison type", return NULL);
1924 set = isl_set_preimage_multi_pw_aff(set, mpa1);
1925 return set;
1926 error:
1927 isl_pw_aff_list_free(left);
1928 isl_pw_aff_list_free(right);
1929 return NULL;
1932 /* Construct constraints of the form
1934 * a op b
1936 * where a is an element in "left", op is an operator of type "type" and
1937 * b is an element in "right", add the constraints to "set" and return
1938 * the result.
1939 * "rational" is set if the constraints should be treated as
1940 * a rational constraints.
1942 * If "type" is the type of a comparison operator between lists
1943 * of affine expressions, then a single (compound) constraint
1944 * is constructed by list_cmp instead.
1946 static __isl_give isl_set *construct_constraints(
1947 __isl_take isl_set *set, int type,
1948 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1949 int rational)
1951 isl_set *cond;
1953 left = isl_pw_aff_list_copy(left);
1954 right = isl_pw_aff_list_copy(right);
1955 if (rational) {
1956 left = isl_pw_aff_list_set_rational(left);
1957 right = isl_pw_aff_list_set_rational(right);
1959 if (is_list_comparator_type(type))
1960 cond = list_cmp(set, type, left, right);
1961 else if (type == ISL_TOKEN_LE)
1962 cond = isl_pw_aff_list_le_set(left, right);
1963 else if (type == ISL_TOKEN_GE)
1964 cond = isl_pw_aff_list_ge_set(left, right);
1965 else if (type == ISL_TOKEN_LT)
1966 cond = isl_pw_aff_list_lt_set(left, right);
1967 else if (type == ISL_TOKEN_GT)
1968 cond = isl_pw_aff_list_gt_set(left, right);
1969 else if (type == ISL_TOKEN_NE)
1970 cond = isl_pw_aff_list_ne_set(left, right);
1971 else
1972 cond = isl_pw_aff_list_eq_set(left, right);
1974 return isl_set_intersect(set, cond);
1977 /* Read a constraint from "s", add it to "map" and return the result.
1978 * "v" contains a description of the identifiers parsed so far.
1979 * "rational" is set if the constraint should be treated as
1980 * a rational constraint.
1981 * The constraint read from "s" may be applied to multiple pairs
1982 * of affine expressions and may be chained.
1983 * In particular, a list of affine expressions is read, followed
1984 * by a comparison operator and another list of affine expressions.
1985 * The comparison operator is then applied to each pair of elements
1986 * in the two lists and the results are added to "map".
1987 * However, if the operator expects two lists of affine expressions,
1988 * then it is applied directly to those lists and the two lists
1989 * are required to have the same length.
1990 * If the next token is another comparison operator, then another
1991 * list of affine expressions is read and the process repeats.
1993 * The processing is performed on a wrapped copy of "map" because
1994 * an affine expression cannot have a binary relation as domain.
1996 static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1997 struct vars *v, __isl_take isl_map *map, int rational)
1999 struct isl_token *tok;
2000 int type;
2001 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
2002 isl_size n1, n2;
2003 isl_set *set;
2005 set = isl_map_wrap(map);
2006 list1 = accept_affine_list(s, isl_set_get_space(set), v);
2007 if (!list1)
2008 goto error;
2009 tok = isl_stream_next_token(s);
2010 if (!is_comparator(tok)) {
2011 isl_stream_error(s, tok, "missing operator");
2012 if (tok)
2013 isl_stream_push_token(s, tok);
2014 goto error;
2016 type = tok->type;
2017 isl_token_free(tok);
2018 for (;;) {
2019 list2 = accept_affine_list(s, isl_set_get_space(set), v);
2020 n1 = isl_pw_aff_list_n_pw_aff(list1);
2021 n2 = isl_pw_aff_list_n_pw_aff(list2);
2022 if (n1 < 0 || n2 < 0)
2023 goto error;
2024 if (is_list_comparator_type(type) && n1 != n2) {
2025 isl_stream_error(s, NULL,
2026 "list arguments not of same size");
2027 goto error;
2030 set = construct_constraints(set, type, list1, list2, rational);
2031 isl_pw_aff_list_free(list1);
2032 list1 = list2;
2034 if (!next_is_comparator(s))
2035 break;
2036 tok = isl_stream_next_token(s);
2037 type = tok->type;
2038 isl_token_free(tok);
2040 isl_pw_aff_list_free(list1);
2042 return isl_set_unwrap(set);
2043 error:
2044 isl_pw_aff_list_free(list1);
2045 isl_pw_aff_list_free(list2);
2046 isl_set_free(set);
2047 return NULL;
2050 static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
2051 struct vars *v, __isl_take isl_map *map, int rational)
2053 int n = v->n;
2054 int seen_paren = isl_stream_eat_if_available(s, '(');
2056 map = isl_map_from_domain(isl_map_wrap(map));
2057 map = read_defined_var_list(s, v, map, rational);
2059 if (isl_stream_eat(s, ':'))
2060 goto error;
2062 map = read_formula(s, v, map, rational);
2063 map = isl_set_unwrap(isl_map_domain(map));
2065 vars_drop(v, v->n - n);
2066 if (seen_paren && isl_stream_eat(s, ')'))
2067 goto error;
2069 return map;
2070 error:
2071 isl_map_free(map);
2072 return NULL;
2075 /* Parse an expression between parentheses and push the result
2076 * back on the stream.
2078 * The parsed expression may be either an affine expression
2079 * or a condition. The first type is pushed onto the stream
2080 * as an isl_pw_aff, while the second is pushed as an isl_map.
2082 * If the initial token indicates the start of a condition,
2083 * we parse it as such.
2084 * Otherwise, we first parse an affine expression and push
2085 * that onto the stream. If the affine expression covers the
2086 * entire expression between parentheses, we return.
2087 * Otherwise, we assume that the affine expression is the
2088 * start of a condition and continue parsing.
2090 static int resolve_paren_expr(__isl_keep isl_stream *s,
2091 struct vars *v, __isl_take isl_map *map, int rational)
2093 struct isl_token *tok, *tok2;
2094 int has_paren;
2095 int line, col;
2096 isl_pw_aff *pwaff;
2098 tok = isl_stream_next_token(s);
2099 if (!tok || tok->type != '(')
2100 goto error;
2102 if (isl_stream_next_token_is(s, '('))
2103 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
2104 goto error;
2106 if (next_is_condition_start(s)) {
2107 map = read_formula(s, v, map, rational);
2108 if (isl_stream_eat(s, ')'))
2109 goto error;
2110 tok->type = ISL_TOKEN_MAP;
2111 tok->u.map = map;
2112 isl_stream_push_token(s, tok);
2113 return 0;
2116 tok2 = isl_stream_next_token(s);
2117 if (!tok2)
2118 goto error;
2119 line = tok2->line;
2120 col = tok2->col;
2121 isl_stream_push_token(s, tok2);
2123 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
2124 if (!pwaff)
2125 goto error;
2127 has_paren = isl_stream_eat_if_available(s, ')');
2129 if (push_aff(s, line, col, pwaff) < 0)
2130 goto error;
2132 if (has_paren) {
2133 isl_token_free(tok);
2134 isl_map_free(map);
2135 return 0;
2138 map = read_formula(s, v, map, rational);
2139 if (isl_stream_eat(s, ')'))
2140 goto error;
2142 tok->type = ISL_TOKEN_MAP;
2143 tok->u.map = map;
2144 isl_stream_push_token(s, tok);
2146 return 0;
2147 error:
2148 isl_token_free(tok);
2149 isl_map_free(map);
2150 return -1;
2153 static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
2154 struct vars *v, __isl_take isl_map *map, int rational)
2156 if (isl_stream_next_token_is(s, '('))
2157 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
2158 goto error;
2160 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
2161 struct isl_token *tok;
2162 tok = isl_stream_next_token(s);
2163 if (!tok)
2164 goto error;
2165 isl_map_free(map);
2166 map = isl_map_copy(tok->u.map);
2167 isl_token_free(tok);
2168 return map;
2171 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
2172 return read_exists(s, v, map, rational);
2174 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
2175 return map;
2177 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
2178 isl_space *space = isl_map_get_space(map);
2179 isl_map_free(map);
2180 return isl_map_empty(space);
2183 return add_constraint(s, v, map, rational);
2184 error:
2185 isl_map_free(map);
2186 return NULL;
2189 static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
2190 struct vars *v, __isl_take isl_map *map, int rational)
2192 isl_map *res;
2193 int negate;
2195 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
2196 res = read_conjunct(s, v, isl_map_copy(map), rational);
2197 if (negate)
2198 res = isl_map_subtract(isl_map_copy(map), res);
2200 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
2201 isl_map *res_i;
2203 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
2204 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
2205 if (negate)
2206 res = isl_map_subtract(res, res_i);
2207 else
2208 res = isl_map_intersect(res, res_i);
2211 isl_map_free(map);
2212 return res;
2215 static __isl_give isl_map *read_disjuncts(__isl_keep isl_stream *s,
2216 struct vars *v, __isl_take isl_map *map, int rational)
2218 isl_map *res;
2220 if (isl_stream_next_token_is(s, '}'))
2221 return map;
2223 res = read_conjuncts(s, v, isl_map_copy(map), rational);
2224 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
2225 isl_map *res_i;
2227 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
2228 res = isl_map_union(res, res_i);
2231 isl_map_free(map);
2232 return res;
2235 /* Read a first order formula from "s", add the corresponding
2236 * constraints to "map" and return the result.
2238 * In particular, read a formula of the form
2242 * or
2244 * a implies b
2246 * where a and b are disjunctions.
2248 * In the first case, map is replaced by
2250 * map \cap { [..] : a }
2252 * In the second case, it is replaced by
2254 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
2256 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
2257 struct vars *v, __isl_take isl_map *map, int rational)
2259 isl_map *res;
2261 res = read_disjuncts(s, v, isl_map_copy(map), rational);
2263 if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
2264 isl_map *res2;
2266 res = isl_map_subtract(isl_map_copy(map), res);
2267 res2 = read_disjuncts(s, v, map, rational);
2268 res = isl_map_union(res, res2);
2269 } else
2270 isl_map_free(map);
2272 return res;
2275 static isl_size polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
2277 isl_size n_out, n_in, n_param, n_div;
2279 n_param = isl_basic_map_dim(bmap, isl_dim_param);
2280 n_in = isl_basic_map_dim(bmap, isl_dim_in);
2281 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2282 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2283 if (n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
2284 return isl_size_error;
2286 if (pos < n_out)
2287 return 1 + n_param + n_in + pos;
2288 pos -= n_out;
2290 if (pos < n_in)
2291 return 1 + n_param + pos;
2292 pos -= n_in;
2294 if (pos < n_div)
2295 return 1 + n_param + n_in + n_out + pos;
2296 pos -= n_div;
2298 if (pos < n_param)
2299 return 1 + pos;
2301 return 0;
2304 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
2305 __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
2307 int j;
2308 struct isl_token *tok;
2309 int type;
2310 int k;
2311 isl_int *c;
2312 isl_size total;
2314 if (!bmap)
2315 return NULL;
2317 tok = isl_stream_next_token(s);
2318 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2319 isl_stream_error(s, tok, "expecting coefficient");
2320 if (tok)
2321 isl_stream_push_token(s, tok);
2322 goto error;
2324 if (!tok->on_new_line) {
2325 isl_stream_error(s, tok, "coefficient should appear on new line");
2326 isl_stream_push_token(s, tok);
2327 goto error;
2330 type = isl_int_get_si(tok->u.v);
2331 isl_token_free(tok);
2333 isl_assert(s->ctx, type == 0 || type == 1, goto error);
2334 if (type == 0) {
2335 k = isl_basic_map_alloc_equality(bmap);
2336 c = bmap->eq[k];
2337 } else {
2338 k = isl_basic_map_alloc_inequality(bmap);
2339 c = bmap->ineq[k];
2341 if (k < 0)
2342 goto error;
2344 total = isl_basic_map_dim(bmap, isl_dim_all);
2345 if (total < 0)
2346 return isl_basic_map_free(bmap);
2347 for (j = 0; j < 1 + total; ++j) {
2348 isl_size pos;
2349 tok = isl_stream_next_token(s);
2350 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2351 isl_stream_error(s, tok, "expecting coefficient");
2352 if (tok)
2353 isl_stream_push_token(s, tok);
2354 goto error;
2356 if (tok->on_new_line) {
2357 isl_stream_error(s, tok,
2358 "coefficient should not appear on new line");
2359 isl_stream_push_token(s, tok);
2360 goto error;
2362 pos = polylib_pos_to_isl_pos(bmap, j);
2363 if (pos >= 0)
2364 isl_int_set(c[pos], tok->u.v);
2365 isl_token_free(tok);
2366 if (pos < 0)
2367 return isl_basic_map_free(bmap);
2370 return bmap;
2371 error:
2372 isl_basic_map_free(bmap);
2373 return NULL;
2376 static __isl_give isl_basic_map *basic_map_read_polylib(
2377 __isl_keep isl_stream *s)
2379 int i;
2380 struct isl_token *tok;
2381 struct isl_token *tok2;
2382 int n_row, n_col;
2383 int on_new_line;
2384 unsigned in = 0, out, local = 0;
2385 struct isl_basic_map *bmap = NULL;
2386 int nparam = 0;
2388 tok = isl_stream_next_token(s);
2389 if (!tok) {
2390 isl_stream_error(s, NULL, "unexpected EOF");
2391 return NULL;
2393 tok2 = isl_stream_next_token(s);
2394 if (!tok2) {
2395 isl_token_free(tok);
2396 isl_stream_error(s, NULL, "unexpected EOF");
2397 return NULL;
2399 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
2400 isl_stream_push_token(s, tok2);
2401 isl_stream_push_token(s, tok);
2402 isl_stream_error(s, NULL,
2403 "expecting constraint matrix dimensions");
2404 return NULL;
2406 n_row = isl_int_get_si(tok->u.v);
2407 n_col = isl_int_get_si(tok2->u.v);
2408 on_new_line = tok2->on_new_line;
2409 isl_token_free(tok2);
2410 isl_token_free(tok);
2411 isl_assert(s->ctx, !on_new_line, return NULL);
2412 isl_assert(s->ctx, n_row >= 0, return NULL);
2413 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
2414 tok = isl_stream_next_token_on_same_line(s);
2415 if (tok) {
2416 if (tok->type != ISL_TOKEN_VALUE) {
2417 isl_stream_error(s, tok,
2418 "expecting number of output dimensions");
2419 isl_stream_push_token(s, tok);
2420 goto error;
2422 out = isl_int_get_si(tok->u.v);
2423 isl_token_free(tok);
2425 tok = isl_stream_next_token_on_same_line(s);
2426 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2427 isl_stream_error(s, tok,
2428 "expecting number of input dimensions");
2429 if (tok)
2430 isl_stream_push_token(s, tok);
2431 goto error;
2433 in = isl_int_get_si(tok->u.v);
2434 isl_token_free(tok);
2436 tok = isl_stream_next_token_on_same_line(s);
2437 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2438 isl_stream_error(s, tok,
2439 "expecting number of existentials");
2440 if (tok)
2441 isl_stream_push_token(s, tok);
2442 goto error;
2444 local = isl_int_get_si(tok->u.v);
2445 isl_token_free(tok);
2447 tok = isl_stream_next_token_on_same_line(s);
2448 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2449 isl_stream_error(s, tok,
2450 "expecting number of parameters");
2451 if (tok)
2452 isl_stream_push_token(s, tok);
2453 goto error;
2455 nparam = isl_int_get_si(tok->u.v);
2456 isl_token_free(tok);
2457 if (n_col != 1 + out + in + local + nparam + 1) {
2458 isl_stream_error(s, NULL,
2459 "dimensions don't match");
2460 goto error;
2462 } else
2463 out = n_col - 2 - nparam;
2464 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2465 if (!bmap)
2466 return NULL;
2468 for (i = 0; i < local; ++i) {
2469 int k = isl_basic_map_alloc_div(bmap);
2470 if (k < 0)
2471 goto error;
2472 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2475 for (i = 0; i < n_row; ++i)
2476 bmap = basic_map_read_polylib_constraint(s, bmap);
2478 tok = isl_stream_next_token_on_same_line(s);
2479 if (tok) {
2480 isl_stream_error(s, tok, "unexpected extra token on line");
2481 isl_stream_push_token(s, tok);
2482 goto error;
2485 bmap = isl_basic_map_simplify(bmap);
2486 bmap = isl_basic_map_finalize(bmap);
2487 return bmap;
2488 error:
2489 isl_basic_map_free(bmap);
2490 return NULL;
2493 static __isl_give isl_map *map_read_polylib(__isl_keep isl_stream *s)
2495 struct isl_token *tok;
2496 struct isl_token *tok2;
2497 int i, n;
2498 struct isl_map *map;
2500 tok = isl_stream_next_token(s);
2501 if (!tok) {
2502 isl_stream_error(s, NULL, "unexpected EOF");
2503 return NULL;
2505 tok2 = isl_stream_next_token_on_same_line(s);
2506 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2507 isl_stream_push_token(s, tok2);
2508 isl_stream_push_token(s, tok);
2509 return isl_map_from_basic_map(basic_map_read_polylib(s));
2511 if (tok2) {
2512 isl_stream_error(s, tok2, "unexpected token");
2513 isl_stream_push_token(s, tok2);
2514 isl_stream_push_token(s, tok);
2515 return NULL;
2517 n = isl_int_get_si(tok->u.v);
2518 isl_token_free(tok);
2520 isl_assert(s->ctx, n >= 1, return NULL);
2522 map = isl_map_from_basic_map(basic_map_read_polylib(s));
2524 for (i = 1; map && i < n; ++i)
2525 map = isl_map_union(map,
2526 isl_map_from_basic_map(basic_map_read_polylib(s)));
2528 return map;
2531 static int optional_power(__isl_keep isl_stream *s)
2533 int pow;
2534 struct isl_token *tok;
2536 tok = isl_stream_next_token(s);
2537 if (!tok)
2538 return 1;
2539 if (tok->type != '^') {
2540 isl_stream_push_token(s, tok);
2541 return 1;
2543 isl_token_free(tok);
2544 tok = isl_stream_next_token(s);
2545 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2546 isl_stream_error(s, tok, "expecting exponent");
2547 if (tok)
2548 isl_stream_push_token(s, tok);
2549 return 1;
2551 pow = isl_int_get_si(tok->u.v);
2552 isl_token_free(tok);
2553 return pow;
2556 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2557 __isl_keep isl_map *map, struct vars *v);
2559 static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2560 __isl_keep isl_map *map, struct vars *v)
2562 isl_pw_qpolynomial *pwqp;
2563 struct isl_token *tok;
2565 tok = next_token(s);
2566 if (!tok) {
2567 isl_stream_error(s, NULL, "unexpected EOF");
2568 return NULL;
2570 if (tok->type == '(') {
2571 int pow;
2573 isl_token_free(tok);
2574 pwqp = read_term(s, map, v);
2575 if (!pwqp)
2576 return NULL;
2577 if (isl_stream_eat(s, ')'))
2578 goto error;
2579 pow = optional_power(s);
2580 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2581 } else if (tok->type == ISL_TOKEN_VALUE) {
2582 struct isl_token *tok2;
2583 isl_qpolynomial *qp;
2585 tok2 = isl_stream_next_token(s);
2586 if (tok2 && tok2->type == '/') {
2587 isl_token_free(tok2);
2588 tok2 = next_token(s);
2589 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2590 isl_stream_error(s, tok2, "expected denominator");
2591 isl_token_free(tok);
2592 isl_token_free(tok2);
2593 return NULL;
2595 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2596 tok->u.v, tok2->u.v);
2597 isl_token_free(tok2);
2598 } else {
2599 isl_stream_push_token(s, tok2);
2600 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2601 tok->u.v);
2603 isl_token_free(tok);
2604 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2605 } else if (tok->type == ISL_TOKEN_INFTY) {
2606 isl_qpolynomial *qp;
2607 isl_token_free(tok);
2608 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2609 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2610 } else if (tok->type == ISL_TOKEN_NAN) {
2611 isl_qpolynomial *qp;
2612 isl_token_free(tok);
2613 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2614 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2615 } else if (tok->type == ISL_TOKEN_IDENT) {
2616 int n = v->n;
2617 int pos = vars_pos(v, tok->u.s, -1);
2618 int pow;
2619 isl_qpolynomial *qp;
2620 if (pos < 0) {
2621 isl_token_free(tok);
2622 return NULL;
2624 if (pos >= n) {
2625 vars_drop(v, v->n - n);
2626 isl_stream_error(s, tok, "unknown identifier");
2627 isl_token_free(tok);
2628 return NULL;
2630 isl_token_free(tok);
2631 pow = optional_power(s);
2632 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2633 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2634 } else if (is_start_of_div(tok)) {
2635 isl_pw_aff *pwaff;
2636 int pow;
2638 isl_stream_push_token(s, tok);
2639 pwaff = accept_div(s, isl_map_get_space(map), v);
2640 pow = optional_power(s);
2641 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2642 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2643 } else if (tok->type == '-') {
2644 isl_token_free(tok);
2645 pwqp = read_factor(s, map, v);
2646 pwqp = isl_pw_qpolynomial_neg(pwqp);
2647 } else {
2648 isl_stream_error(s, tok, "unexpected isl_token");
2649 isl_stream_push_token(s, tok);
2650 return NULL;
2653 if (isl_stream_eat_if_available(s, '*') ||
2654 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
2655 isl_pw_qpolynomial *pwqp2;
2657 pwqp2 = read_factor(s, map, v);
2658 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2661 return pwqp;
2662 error:
2663 isl_pw_qpolynomial_free(pwqp);
2664 return NULL;
2667 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2668 __isl_keep isl_map *map, struct vars *v)
2670 struct isl_token *tok;
2671 isl_pw_qpolynomial *pwqp;
2673 pwqp = read_factor(s, map, v);
2675 for (;;) {
2676 tok = next_token(s);
2677 if (!tok)
2678 return pwqp;
2680 if (tok->type == '+') {
2681 isl_pw_qpolynomial *pwqp2;
2683 isl_token_free(tok);
2684 pwqp2 = read_factor(s, map, v);
2685 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2686 } else if (tok->type == '-') {
2687 isl_pw_qpolynomial *pwqp2;
2689 isl_token_free(tok);
2690 pwqp2 = read_factor(s, map, v);
2691 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2692 } else if (tok->type == ISL_TOKEN_VALUE &&
2693 isl_int_is_neg(tok->u.v)) {
2694 isl_pw_qpolynomial *pwqp2;
2696 isl_stream_push_token(s, tok);
2697 pwqp2 = read_factor(s, map, v);
2698 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2699 } else {
2700 isl_stream_push_token(s, tok);
2701 break;
2705 return pwqp;
2708 static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2709 __isl_take isl_map *map, struct vars *v, int rational)
2711 struct isl_token *tok;
2713 tok = isl_stream_next_token(s);
2714 if (!tok) {
2715 isl_stream_error(s, NULL, "unexpected EOF");
2716 goto error;
2718 if (tok->type == ':' ||
2719 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
2720 isl_token_free(tok);
2721 map = read_formula(s, v, map, rational);
2722 } else
2723 isl_stream_push_token(s, tok);
2725 return map;
2726 error:
2727 isl_map_free(map);
2728 return NULL;
2731 static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2732 __isl_take isl_map *map, struct vars *v, int n)
2734 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2735 isl_pw_qpolynomial *pwqp;
2736 struct isl_set *set;
2738 pwqp = read_term(s, map, v);
2739 map = read_optional_formula(s, map, v, 0);
2740 set = isl_map_range(map);
2742 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2744 vars_drop(v, v->n - n);
2746 obj.v = pwqp;
2747 return obj;
2750 static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2751 __isl_take isl_set *set, struct vars *v, int n)
2753 int min, max;
2754 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2755 isl_pw_qpolynomial *pwqp;
2756 isl_pw_qpolynomial_fold *pwf = NULL;
2757 enum isl_fold fold;
2759 max = isl_stream_eat_if_available(s, ISL_TOKEN_MAX);
2760 min = !max && isl_stream_eat_if_available(s, ISL_TOKEN_MIN);
2761 if (!min && !max)
2762 return obj_read_poly(s, set, v, n);
2763 fold = max ? isl_fold_max : isl_fold_min;
2765 if (isl_stream_eat(s, '('))
2766 goto error;
2768 pwqp = read_term(s, set, v);
2769 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
2771 while (isl_stream_eat_if_available(s, ',')) {
2772 isl_pw_qpolynomial_fold *pwf_i;
2773 pwqp = read_term(s, set, v);
2774 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
2775 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2778 if (isl_stream_eat(s, ')'))
2779 goto error;
2781 set = read_optional_formula(s, set, v, 0);
2782 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2784 vars_drop(v, v->n - n);
2786 obj.v = pwf;
2787 return obj;
2788 error:
2789 isl_set_free(set);
2790 isl_pw_qpolynomial_fold_free(pwf);
2791 obj.type = isl_obj_none;
2792 return obj;
2795 static int is_rational(__isl_keep isl_stream *s)
2797 struct isl_token *tok;
2799 tok = isl_stream_next_token(s);
2800 if (!tok)
2801 return 0;
2802 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
2803 isl_token_free(tok);
2804 isl_stream_eat(s, ':');
2805 return 1;
2808 isl_stream_push_token(s, tok);
2810 return 0;
2813 static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2814 __isl_take isl_map *map, struct vars *v)
2816 struct isl_token *tok;
2817 struct isl_obj obj = { isl_obj_set, NULL };
2818 int n = v->n;
2819 int rational;
2821 rational = is_rational(s);
2822 if (rational)
2823 map = isl_map_set_rational(map);
2825 if (isl_stream_next_token_is(s, ':')) {
2826 obj.type = isl_obj_set;
2827 obj.v = read_optional_formula(s, map, v, rational);
2828 return obj;
2831 if (!next_is_tuple(s))
2832 return obj_read_poly_or_fold(s, map, v, n);
2834 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2835 if (!map)
2836 goto error;
2837 tok = isl_stream_next_token(s);
2838 if (!tok)
2839 goto error;
2840 if (tok->type == ISL_TOKEN_TO) {
2841 obj.type = isl_obj_map;
2842 isl_token_free(tok);
2843 if (!next_is_tuple(s)) {
2844 isl_set *set = isl_map_domain(map);
2845 return obj_read_poly_or_fold(s, set, v, n);
2847 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2848 if (!map)
2849 goto error;
2850 } else {
2851 map = isl_map_domain(map);
2852 isl_stream_push_token(s, tok);
2855 map = read_optional_formula(s, map, v, rational);
2857 vars_drop(v, v->n - n);
2859 obj.v = map;
2860 return obj;
2861 error:
2862 isl_map_free(map);
2863 obj.type = isl_obj_none;
2864 return obj;
2867 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2869 if (obj.type == isl_obj_map) {
2870 obj.v = isl_union_map_from_map(obj.v);
2871 obj.type = isl_obj_union_map;
2872 } else if (obj.type == isl_obj_set) {
2873 obj.v = isl_union_set_from_set(obj.v);
2874 obj.type = isl_obj_union_set;
2875 } else if (obj.type == isl_obj_pw_qpolynomial) {
2876 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2877 obj.type = isl_obj_union_pw_qpolynomial;
2878 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2879 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2880 obj.type = isl_obj_union_pw_qpolynomial_fold;
2881 } else
2882 isl_assert(ctx, 0, goto error);
2883 return obj;
2884 error:
2885 obj.type->free(obj.v);
2886 obj.type = isl_obj_none;
2887 return obj;
2890 static struct isl_obj obj_add(__isl_keep isl_stream *s,
2891 struct isl_obj obj1, struct isl_obj obj2)
2893 if (obj2.type == isl_obj_none || !obj2.v)
2894 goto error;
2895 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2896 obj1 = to_union(s->ctx, obj1);
2897 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2898 obj2 = to_union(s->ctx, obj2);
2899 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2900 obj1 = to_union(s->ctx, obj1);
2901 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2902 obj2 = to_union(s->ctx, obj2);
2903 if (obj1.type == isl_obj_pw_qpolynomial &&
2904 obj2.type == isl_obj_union_pw_qpolynomial)
2905 obj1 = to_union(s->ctx, obj1);
2906 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2907 obj2.type == isl_obj_pw_qpolynomial)
2908 obj2 = to_union(s->ctx, obj2);
2909 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2910 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2911 obj1 = to_union(s->ctx, obj1);
2912 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2913 obj2.type == isl_obj_pw_qpolynomial_fold)
2914 obj2 = to_union(s->ctx, obj2);
2915 if (obj1.type != obj2.type) {
2916 isl_stream_error(s, NULL,
2917 "attempt to combine incompatible objects");
2918 goto error;
2920 if (!obj1.type->add)
2921 isl_die(s->ctx, isl_error_internal,
2922 "combination not supported on object type", goto error);
2923 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2924 obj1 = to_union(s->ctx, obj1);
2925 obj2 = to_union(s->ctx, obj2);
2927 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2928 obj1 = to_union(s->ctx, obj1);
2929 obj2 = to_union(s->ctx, obj2);
2931 if (obj1.type == isl_obj_pw_qpolynomial &&
2932 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2933 obj1 = to_union(s->ctx, obj1);
2934 obj2 = to_union(s->ctx, obj2);
2936 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2937 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2938 obj1 = to_union(s->ctx, obj1);
2939 obj2 = to_union(s->ctx, obj2);
2941 obj1.v = obj1.type->add(obj1.v, obj2.v);
2942 return obj1;
2943 error:
2944 obj1.type->free(obj1.v);
2945 obj2.type->free(obj2.v);
2946 obj1.type = isl_obj_none;
2947 obj1.v = NULL;
2948 return obj1;
2951 /* Are the first two tokens on "s", "domain" (either as a string
2952 * or as an identifier) followed by ":"?
2954 static int next_is_domain_colon(__isl_keep isl_stream *s)
2956 struct isl_token *tok;
2957 char *name;
2958 int res;
2960 tok = isl_stream_next_token(s);
2961 if (!tok)
2962 return 0;
2963 if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
2964 isl_stream_push_token(s, tok);
2965 return 0;
2968 name = isl_token_get_str(s->ctx, tok);
2969 res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2970 free(name);
2972 isl_stream_push_token(s, tok);
2974 return res;
2977 /* Do the first tokens on "s" look like a schedule?
2979 * The root of a schedule is always a domain node, so the first thing
2980 * we expect in the stream is a domain key, i.e., "domain" followed
2981 * by ":". If the schedule was printed in YAML flow style, then
2982 * we additionally expect a "{" to open the outer mapping.
2984 static int next_is_schedule(__isl_keep isl_stream *s)
2986 struct isl_token *tok;
2987 int is_schedule;
2989 tok = isl_stream_next_token(s);
2990 if (!tok)
2991 return 0;
2992 if (tok->type != '{') {
2993 isl_stream_push_token(s, tok);
2994 return next_is_domain_colon(s);
2997 is_schedule = next_is_domain_colon(s);
2998 isl_stream_push_token(s, tok);
3000 return is_schedule;
3003 /* Read an isl_schedule from "s" and store it in an isl_obj.
3005 static struct isl_obj schedule_read(__isl_keep isl_stream *s)
3007 struct isl_obj obj;
3009 obj.type = isl_obj_schedule;
3010 obj.v = isl_stream_read_schedule(s);
3012 return obj;
3015 /* Read a disjunction of object bodies from "s".
3016 * That is, read the inside of the braces, but not the braces themselves.
3017 * "v" contains a description of the identifiers parsed so far.
3018 * "map" contains information about the parameters.
3020 static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
3021 struct vars *v, __isl_keep isl_map *map)
3023 struct isl_obj obj = { isl_obj_set, NULL };
3025 if (isl_stream_next_token_is(s, '}')) {
3026 obj.type = isl_obj_union_set;
3027 obj.v = isl_union_set_empty(isl_map_get_space(map));
3028 return obj;
3031 for (;;) {
3032 struct isl_obj o;
3033 o = obj_read_body(s, isl_map_copy(map), v);
3034 if (!obj.v)
3035 obj = o;
3036 else
3037 obj = obj_add(s, obj, o);
3038 if (obj.type == isl_obj_none || !obj.v)
3039 return obj;
3040 if (!isl_stream_eat_if_available(s, ';'))
3041 break;
3042 if (isl_stream_next_token_is(s, '}'))
3043 break;
3046 return obj;
3049 static struct isl_obj obj_read(__isl_keep isl_stream *s)
3051 isl_map *map = NULL;
3052 struct isl_token *tok;
3053 struct vars *v = NULL;
3054 struct isl_obj obj = { isl_obj_set, NULL };
3056 if (next_is_schedule(s))
3057 return schedule_read(s);
3059 tok = next_token(s);
3060 if (!tok) {
3061 isl_stream_error(s, NULL, "unexpected EOF");
3062 goto error;
3064 if (tok->type == ISL_TOKEN_VALUE) {
3065 struct isl_token *tok2;
3066 struct isl_map *map;
3068 tok2 = isl_stream_next_token(s);
3069 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
3070 isl_int_is_neg(tok2->u.v)) {
3071 if (tok2)
3072 isl_stream_push_token(s, tok2);
3073 obj.type = isl_obj_val;
3074 obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
3075 isl_token_free(tok);
3076 return obj;
3078 isl_stream_push_token(s, tok2);
3079 isl_stream_push_token(s, tok);
3080 map = map_read_polylib(s);
3081 if (!map)
3082 goto error;
3083 if (isl_map_may_be_set(map))
3084 obj.v = isl_map_range(map);
3085 else {
3086 obj.type = isl_obj_map;
3087 obj.v = map;
3089 return obj;
3091 v = vars_new(s->ctx);
3092 if (!v) {
3093 isl_stream_push_token(s, tok);
3094 goto error;
3096 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
3097 if (tok->type == '[') {
3098 isl_stream_push_token(s, tok);
3099 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
3100 if (!map)
3101 goto error;
3102 tok = isl_stream_next_token(s);
3103 if (!tok || tok->type != ISL_TOKEN_TO) {
3104 isl_stream_error(s, tok, "expecting '->'");
3105 if (tok)
3106 isl_stream_push_token(s, tok);
3107 goto error;
3109 isl_token_free(tok);
3110 tok = isl_stream_next_token(s);
3112 if (!tok || tok->type != '{') {
3113 isl_stream_error(s, tok, "expecting '{'");
3114 if (tok)
3115 isl_stream_push_token(s, tok);
3116 goto error;
3118 isl_token_free(tok);
3120 tok = isl_stream_next_token(s);
3121 if (!tok)
3123 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
3124 isl_token_free(tok);
3125 if (isl_stream_eat(s, '='))
3126 goto error;
3127 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
3128 if (!map)
3129 goto error;
3130 } else
3131 isl_stream_push_token(s, tok);
3133 obj = obj_read_disjuncts(s, v, map);
3134 if (obj.type == isl_obj_none || !obj.v)
3135 goto error;
3137 tok = isl_stream_next_token(s);
3138 if (tok && tok->type == '}') {
3139 isl_token_free(tok);
3140 } else {
3141 isl_stream_error(s, tok, "unexpected isl_token");
3142 if (tok)
3143 isl_token_free(tok);
3144 goto error;
3147 vars_free(v);
3148 isl_map_free(map);
3150 return obj;
3151 error:
3152 isl_map_free(map);
3153 obj.type->free(obj.v);
3154 if (v)
3155 vars_free(v);
3156 obj.v = NULL;
3157 return obj;
3160 struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
3162 return obj_read(s);
3165 __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
3167 struct isl_obj obj;
3169 obj = obj_read(s);
3170 if (obj.v)
3171 isl_assert(s->ctx, obj.type == isl_obj_map ||
3172 obj.type == isl_obj_set, goto error);
3174 if (obj.type == isl_obj_set)
3175 obj.v = isl_map_from_range(obj.v);
3177 return obj.v;
3178 error:
3179 obj.type->free(obj.v);
3180 return NULL;
3183 __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
3185 struct isl_obj obj;
3187 obj = obj_read(s);
3188 if (obj.v) {
3189 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
3190 obj.v = isl_map_range(obj.v);
3191 obj.type = isl_obj_set;
3193 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
3196 return obj.v;
3197 error:
3198 obj.type->free(obj.v);
3199 return NULL;
3202 __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
3204 struct isl_obj obj;
3206 obj = obj_read(s);
3207 if (obj.type == isl_obj_map) {
3208 obj.type = isl_obj_union_map;
3209 obj.v = isl_union_map_from_map(obj.v);
3211 if (obj.type == isl_obj_set) {
3212 obj.type = isl_obj_union_set;
3213 obj.v = isl_union_set_from_set(obj.v);
3215 if (obj.v && obj.type == isl_obj_union_set &&
3216 isl_union_set_is_empty(obj.v))
3217 obj.type = isl_obj_union_map;
3218 if (obj.v && obj.type != isl_obj_union_map)
3219 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
3221 return obj.v;
3222 error:
3223 obj.type->free(obj.v);
3224 return NULL;
3227 /* Extract an isl_union_set from "obj".
3228 * This only works if the object was detected as either a set
3229 * (in which case it is converted to a union set) or a union set.
3231 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
3232 struct isl_obj obj)
3234 if (obj.type == isl_obj_set) {
3235 obj.type = isl_obj_union_set;
3236 obj.v = isl_union_set_from_set(obj.v);
3238 if (obj.v)
3239 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
3241 return obj.v;
3242 error:
3243 obj.type->free(obj.v);
3244 return NULL;
3247 /* Read an isl_union_set from "s".
3248 * First read a generic object and then try and extract
3249 * an isl_union_set from that.
3251 __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
3253 struct isl_obj obj;
3255 obj = obj_read(s);
3256 return extract_union_set(s->ctx, obj);
3259 static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
3261 struct isl_obj obj;
3262 struct isl_map *map;
3263 struct isl_basic_map *bmap;
3265 obj = obj_read(s);
3266 if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
3267 isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
3268 goto error);
3269 map = obj.v;
3270 if (!map)
3271 return NULL;
3273 if (map->n > 1)
3274 isl_die(s->ctx, isl_error_invalid,
3275 "set or map description involves "
3276 "more than one disjunct", goto error);
3278 if (map->n == 0)
3279 bmap = isl_basic_map_empty(isl_map_get_space(map));
3280 else
3281 bmap = isl_basic_map_copy(map->p[0]);
3283 isl_map_free(map);
3285 return bmap;
3286 error:
3287 obj.type->free(obj.v);
3288 return NULL;
3291 static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
3293 isl_basic_map *bmap;
3294 bmap = basic_map_read(s);
3295 if (!bmap)
3296 return NULL;
3297 if (!isl_basic_map_may_be_set(bmap))
3298 isl_die(s->ctx, isl_error_invalid,
3299 "input is not a set", goto error);
3300 return isl_basic_map_range(bmap);
3301 error:
3302 isl_basic_map_free(bmap);
3303 return NULL;
3306 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
3307 FILE *input)
3309 struct isl_basic_map *bmap;
3310 isl_stream *s = isl_stream_new_file(ctx, input);
3311 if (!s)
3312 return NULL;
3313 bmap = basic_map_read(s);
3314 isl_stream_free(s);
3315 return bmap;
3318 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
3319 FILE *input)
3321 isl_basic_set *bset;
3322 isl_stream *s = isl_stream_new_file(ctx, input);
3323 if (!s)
3324 return NULL;
3325 bset = basic_set_read(s);
3326 isl_stream_free(s);
3327 return bset;
3330 __isl_give isl_basic_map *isl_basic_map_read_from_str(isl_ctx *ctx,
3331 const char *str)
3333 struct isl_basic_map *bmap;
3334 isl_stream *s = isl_stream_new_str(ctx, str);
3335 if (!s)
3336 return NULL;
3337 bmap = basic_map_read(s);
3338 isl_stream_free(s);
3339 return bmap;
3342 __isl_give isl_basic_set *isl_basic_set_read_from_str(isl_ctx *ctx,
3343 const char *str)
3345 isl_basic_set *bset;
3346 isl_stream *s = isl_stream_new_str(ctx, str);
3347 if (!s)
3348 return NULL;
3349 bset = basic_set_read(s);
3350 isl_stream_free(s);
3351 return bset;
3354 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
3355 FILE *input)
3357 struct isl_map *map;
3358 isl_stream *s = isl_stream_new_file(ctx, input);
3359 if (!s)
3360 return NULL;
3361 map = isl_stream_read_map(s);
3362 isl_stream_free(s);
3363 return map;
3366 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
3367 const char *str)
3369 struct isl_map *map;
3370 isl_stream *s = isl_stream_new_str(ctx, str);
3371 if (!s)
3372 return NULL;
3373 map = isl_stream_read_map(s);
3374 isl_stream_free(s);
3375 return map;
3378 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
3379 FILE *input)
3381 isl_set *set;
3382 isl_stream *s = isl_stream_new_file(ctx, input);
3383 if (!s)
3384 return NULL;
3385 set = isl_stream_read_set(s);
3386 isl_stream_free(s);
3387 return set;
3390 __isl_give isl_set *isl_set_read_from_str(isl_ctx *ctx, const char *str)
3392 isl_set *set;
3393 isl_stream *s = isl_stream_new_str(ctx, str);
3394 if (!s)
3395 return NULL;
3396 set = isl_stream_read_set(s);
3397 isl_stream_free(s);
3398 return set;
3401 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
3402 FILE *input)
3404 isl_union_map *umap;
3405 isl_stream *s = isl_stream_new_file(ctx, input);
3406 if (!s)
3407 return NULL;
3408 umap = isl_stream_read_union_map(s);
3409 isl_stream_free(s);
3410 return umap;
3413 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
3414 const char *str)
3416 isl_union_map *umap;
3417 isl_stream *s = isl_stream_new_str(ctx, str);
3418 if (!s)
3419 return NULL;
3420 umap = isl_stream_read_union_map(s);
3421 isl_stream_free(s);
3422 return umap;
3425 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
3426 FILE *input)
3428 isl_union_set *uset;
3429 isl_stream *s = isl_stream_new_file(ctx, input);
3430 if (!s)
3431 return NULL;
3432 uset = isl_stream_read_union_set(s);
3433 isl_stream_free(s);
3434 return uset;
3437 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3438 const char *str)
3440 isl_union_set *uset;
3441 isl_stream *s = isl_stream_new_str(ctx, str);
3442 if (!s)
3443 return NULL;
3444 uset = isl_stream_read_union_set(s);
3445 isl_stream_free(s);
3446 return uset;
3449 static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3451 struct isl_vec *vec = NULL;
3452 struct isl_token *tok;
3453 unsigned size;
3454 int j;
3456 tok = isl_stream_next_token(s);
3457 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3458 isl_stream_error(s, tok, "expecting vector length");
3459 goto error;
3462 size = isl_int_get_si(tok->u.v);
3463 isl_token_free(tok);
3465 vec = isl_vec_alloc(s->ctx, size);
3467 for (j = 0; j < size; ++j) {
3468 tok = isl_stream_next_token(s);
3469 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3470 isl_stream_error(s, tok, "expecting constant value");
3471 goto error;
3473 isl_int_set(vec->el[j], tok->u.v);
3474 isl_token_free(tok);
3477 return vec;
3478 error:
3479 isl_token_free(tok);
3480 isl_vec_free(vec);
3481 return NULL;
3484 static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3486 return isl_vec_read_polylib(s);
3489 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3491 isl_vec *v;
3492 isl_stream *s = isl_stream_new_file(ctx, input);
3493 if (!s)
3494 return NULL;
3495 v = vec_read(s);
3496 isl_stream_free(s);
3497 return v;
3500 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3501 __isl_keep isl_stream *s)
3503 struct isl_obj obj;
3505 obj = obj_read(s);
3506 if (obj.v)
3507 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3508 goto error);
3510 return obj.v;
3511 error:
3512 obj.type->free(obj.v);
3513 return NULL;
3516 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3517 const char *str)
3519 isl_pw_qpolynomial *pwqp;
3520 isl_stream *s = isl_stream_new_str(ctx, str);
3521 if (!s)
3522 return NULL;
3523 pwqp = isl_stream_read_pw_qpolynomial(s);
3524 isl_stream_free(s);
3525 return pwqp;
3528 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3529 FILE *input)
3531 isl_pw_qpolynomial *pwqp;
3532 isl_stream *s = isl_stream_new_file(ctx, input);
3533 if (!s)
3534 return NULL;
3535 pwqp = isl_stream_read_pw_qpolynomial(s);
3536 isl_stream_free(s);
3537 return pwqp;
3540 /* Read an isl_pw_qpolynomial_fold from "s".
3541 * First read a generic object and
3542 * then check that it is an isl_pw_qpolynomial_fold.
3544 __isl_give isl_pw_qpolynomial_fold *isl_stream_read_pw_qpolynomial_fold(
3545 __isl_keep isl_stream *s)
3547 struct isl_obj obj;
3549 obj = obj_read(s);
3550 if (obj.v && obj.type != isl_obj_pw_qpolynomial_fold)
3551 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
3553 return obj.v;
3554 error:
3555 obj.type->free(obj.v);
3556 return NULL;
3559 /* Read an isl_pw_qpolynomial_fold from "str".
3561 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_read_from_str(
3562 isl_ctx *ctx, const char *str)
3564 isl_pw_qpolynomial_fold *pwqp;
3565 isl_stream *s;
3567 s = isl_stream_new_str(ctx, str);
3568 if (!s)
3569 return NULL;
3570 pwqp = isl_stream_read_pw_qpolynomial_fold(s);
3571 isl_stream_free(s);
3573 return pwqp;
3576 /* Is the next token an identifier not in "v"?
3578 static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3580 int n = v->n;
3581 int fresh;
3582 struct isl_token *tok;
3584 tok = isl_stream_next_token(s);
3585 if (!tok)
3586 return 0;
3587 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
3588 isl_stream_push_token(s, tok);
3590 vars_drop(v, v->n - n);
3592 return fresh;
3595 /* First read the domain of the affine expression, which may be
3596 * a parameter space or a set.
3597 * The tricky part is that we don't know if the domain is a set or not,
3598 * so when we are trying to read the domain, we may actually be reading
3599 * the affine expression itself (defined on a parameter domains)
3600 * If the tuple we are reading is named, we assume it's the domain.
3601 * Also, if inside the tuple, the first thing we find is a nested tuple
3602 * or a new identifier, we again assume it's the domain.
3603 * Finally, if the tuple is empty, then it must be the domain
3604 * since it does not contain an affine expression.
3605 * Otherwise, we assume we are reading an affine expression.
3607 static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3608 __isl_take isl_set *dom, struct vars *v)
3610 struct isl_token *tok, *tok2;
3611 int is_empty;
3613 tok = isl_stream_next_token(s);
3614 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
3615 isl_stream_push_token(s, tok);
3616 return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3618 if (!tok || tok->type != '[') {
3619 isl_stream_error(s, tok, "expecting '['");
3620 goto error;
3622 tok2 = isl_stream_next_token(s);
3623 is_empty = tok2 && tok2->type == ']';
3624 if (tok2)
3625 isl_stream_push_token(s, tok2);
3626 if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
3627 isl_stream_push_token(s, tok);
3628 dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3629 } else
3630 isl_stream_push_token(s, tok);
3632 return dom;
3633 error:
3634 if (tok)
3635 isl_stream_push_token(s, tok);
3636 isl_set_free(dom);
3637 return NULL;
3640 /* Read an affine expression from "s".
3642 __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3644 isl_aff *aff;
3645 isl_multi_aff *ma;
3646 isl_size dim;
3648 ma = isl_stream_read_multi_aff(s);
3649 dim = isl_multi_aff_dim(ma, isl_dim_out);
3650 if (dim < 0)
3651 goto error;
3652 if (dim != 1)
3653 isl_die(s->ctx, isl_error_invalid,
3654 "expecting single affine expression",
3655 goto error);
3657 aff = isl_multi_aff_get_aff(ma, 0);
3658 isl_multi_aff_free(ma);
3659 return aff;
3660 error:
3661 isl_multi_aff_free(ma);
3662 return NULL;
3665 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3667 static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3668 __isl_take isl_set *dom, struct vars *v)
3670 isl_pw_aff *pwaff = NULL;
3672 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
3673 goto error;
3675 if (isl_stream_eat(s, '['))
3676 goto error;
3678 pwaff = accept_affine(s, isl_set_get_space(dom), v);
3680 if (isl_stream_eat(s, ']'))
3681 goto error;
3683 dom = read_optional_formula(s, dom, v, 0);
3684 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3686 return pwaff;
3687 error:
3688 isl_set_free(dom);
3689 isl_pw_aff_free(pwaff);
3690 return NULL;
3693 __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3695 struct vars *v;
3696 isl_set *dom = NULL;
3697 isl_set *aff_dom;
3698 isl_pw_aff *pa = NULL;
3699 int n;
3701 v = vars_new(s->ctx);
3702 if (!v)
3703 return NULL;
3705 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3706 if (next_is_tuple(s)) {
3707 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3708 if (isl_stream_eat(s, ISL_TOKEN_TO))
3709 goto error;
3711 if (isl_stream_eat(s, '{'))
3712 goto error;
3714 n = v->n;
3715 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3716 pa = read_pw_aff_with_dom(s, aff_dom, v);
3717 vars_drop(v, v->n - n);
3719 while (isl_stream_eat_if_available(s, ';')) {
3720 isl_pw_aff *pa_i;
3722 n = v->n;
3723 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3724 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3725 vars_drop(v, v->n - n);
3727 pa = isl_pw_aff_union_add(pa, pa_i);
3730 if (isl_stream_eat(s, '}'))
3731 goto error;
3733 vars_free(v);
3734 isl_set_free(dom);
3735 return pa;
3736 error:
3737 vars_free(v);
3738 isl_set_free(dom);
3739 isl_pw_aff_free(pa);
3740 return NULL;
3743 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3745 isl_aff *aff;
3746 isl_stream *s = isl_stream_new_str(ctx, str);
3747 if (!s)
3748 return NULL;
3749 aff = isl_stream_read_aff(s);
3750 isl_stream_free(s);
3751 return aff;
3754 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3756 isl_pw_aff *pa;
3757 isl_stream *s = isl_stream_new_str(ctx, str);
3758 if (!s)
3759 return NULL;
3760 pa = isl_stream_read_pw_aff(s);
3761 isl_stream_free(s);
3762 return pa;
3765 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3766 * from a tuple "tuple" read by read_tuple.
3768 * Note that the function read_tuple accepts tuples where some output or
3769 * set dimensions are defined in terms of other output or set dimensions
3770 * since this function is also used to read maps. As a special case,
3771 * read_tuple also accept dimensions that are defined in terms of themselves
3772 * (i.e., that are not defined).
3773 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3774 * that the definitions of the output/set dimensions do not involve any
3775 * output/set dimensions.
3776 * Finally, drop the output dimensions from the domain of the result
3777 * of read_tuple (which is of the form [input, output] -> [output],
3778 * with anonymous domain) and reset the space.
3780 static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3781 __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3783 int i;
3784 isl_size dim, n;
3785 isl_space *space;
3786 isl_multi_pw_aff *mpa;
3788 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3789 dim = isl_space_dim(dom_space, isl_dim_all);
3790 if (n < 0 || dim < 0)
3791 dom_space = isl_space_free(dom_space);
3792 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3793 space = isl_space_align_params(space, isl_space_copy(dom_space));
3794 if (!isl_space_is_params(dom_space))
3795 space = isl_space_map_from_domain_and_range(
3796 isl_space_copy(dom_space), space);
3797 isl_space_free(dom_space);
3798 mpa = isl_multi_pw_aff_alloc(space);
3800 for (i = 0; i < n; ++i) {
3801 isl_pw_aff *pa;
3802 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3803 if (!pa)
3804 return isl_multi_pw_aff_free(mpa);
3805 if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3806 isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3807 isl_pw_aff_free(pa);
3808 isl_die(ctx, isl_error_invalid,
3809 "not an affine expression",
3810 return isl_multi_pw_aff_free(mpa));
3812 pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3813 space = isl_multi_pw_aff_get_domain_space(mpa);
3814 pa = isl_pw_aff_reset_domain_space(pa, space);
3815 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3818 return mpa;
3821 /* Read a tuple of affine expressions, together with optional constraints
3822 * on the domain from "s". "dom" represents the initial constraints
3823 * on the domain.
3825 * The isl_multi_aff may live in either a set or a map space.
3826 * First read the first tuple and check if it is followed by a "->".
3827 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3828 * read in the next tuple. This tuple (or the first tuple if it was
3829 * not followed by a "->") is then converted into an isl_multi_pw_aff
3830 * through a call to extract_mpa_from_tuple.
3831 * The result is converted to an isl_pw_multi_aff and
3832 * its domain is intersected with the domain.
3834 * Note that the last tuple may introduce new identifiers,
3835 * but these cannot be referenced in the description of the domain.
3837 static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3838 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3840 isl_multi_pw_aff *tuple;
3841 isl_multi_pw_aff *mpa;
3842 isl_pw_multi_aff *pma;
3843 int n = v->n;
3844 int n_dom;
3846 n_dom = v->n;
3847 tuple = read_tuple(s, v, 0, 0);
3848 if (!tuple)
3849 goto error;
3850 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3851 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3852 dom = isl_map_domain(map);
3853 n_dom = v->n;
3854 tuple = read_tuple(s, v, 0, 0);
3855 if (!tuple)
3856 goto error;
3858 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3859 isl_multi_pw_aff_free(tuple);
3860 if (!mpa)
3861 dom = isl_set_free(dom);
3863 vars_drop(v, v->n - n_dom);
3864 dom = read_optional_formula(s, dom, v, 0);
3866 vars_drop(v, v->n - n);
3868 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3869 pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3871 return pma;
3872 error:
3873 isl_set_free(dom);
3874 return NULL;
3877 /* Read an isl_union_pw_multi_aff from "s".
3879 * In particular, first read the parameters and then read a sequence
3880 * of zero or more tuples of affine expressions with optional conditions and
3881 * add them up.
3883 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3884 __isl_keep isl_stream *s)
3886 struct vars *v;
3887 isl_set *dom;
3888 isl_union_pw_multi_aff *upma = NULL;
3890 v = vars_new(s->ctx);
3891 if (!v)
3892 return NULL;
3894 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3895 if (next_is_tuple(s)) {
3896 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3897 if (isl_stream_eat(s, ISL_TOKEN_TO))
3898 goto error;
3900 if (isl_stream_eat(s, '{'))
3901 goto error;
3903 upma = isl_union_pw_multi_aff_empty(isl_set_get_space(dom));
3905 do {
3906 isl_pw_multi_aff *pma;
3907 isl_union_pw_multi_aff *upma2;
3909 if (isl_stream_next_token_is(s, '}'))
3910 break;
3912 pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3913 upma2 = isl_union_pw_multi_aff_from_pw_multi_aff(pma);
3914 upma = isl_union_pw_multi_aff_union_add(upma, upma2);
3915 if (!upma)
3916 goto error;
3917 } while (isl_stream_eat_if_available(s, ';'));
3919 if (isl_stream_eat(s, '}'))
3920 goto error;
3922 isl_set_free(dom);
3923 vars_free(v);
3924 return upma;
3925 error:
3926 isl_union_pw_multi_aff_free(upma);
3927 isl_set_free(dom);
3928 vars_free(v);
3929 return NULL;
3932 /* Read an isl_pw_multi_aff from "s".
3934 * Read a more generic isl_union_pw_multi_aff first and
3935 * then check that the result lives in a single space.
3937 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3938 __isl_keep isl_stream *s)
3940 isl_bool single_space;
3941 isl_union_pw_multi_aff *upma;
3943 upma = isl_stream_read_union_pw_multi_aff(s);
3944 single_space = isl_union_pw_multi_aff_isa_pw_multi_aff(upma);
3945 if (single_space < 0)
3946 upma = isl_union_pw_multi_aff_free(upma);
3947 else if (!single_space)
3948 isl_die(s->ctx, isl_error_invalid,
3949 "expecting expression in single space",
3950 upma = isl_union_pw_multi_aff_free(upma));
3951 return isl_union_pw_multi_aff_as_pw_multi_aff(upma);
3954 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3955 const char *str)
3957 isl_pw_multi_aff *pma;
3958 isl_stream *s = isl_stream_new_str(ctx, str);
3959 if (!s)
3960 return NULL;
3961 pma = isl_stream_read_pw_multi_aff(s);
3962 isl_stream_free(s);
3963 return pma;
3966 /* Read an isl_union_pw_multi_aff from "str".
3968 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3969 isl_ctx *ctx, const char *str)
3971 isl_union_pw_multi_aff *upma;
3972 isl_stream *s = isl_stream_new_str(ctx, str);
3973 if (!s)
3974 return NULL;
3975 upma = isl_stream_read_union_pw_multi_aff(s);
3976 isl_stream_free(s);
3977 return upma;
3980 /* Assuming "pa" represents a single affine expression defined on a universe
3981 * domain, extract this affine expression.
3983 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3985 isl_aff *aff;
3987 if (!pa)
3988 return NULL;
3989 if (pa->n != 1)
3990 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3991 "expecting single affine expression",
3992 goto error);
3993 if (!isl_set_plain_is_universe(pa->p[0].set))
3994 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3995 "expecting universe domain",
3996 goto error);
3998 aff = isl_aff_copy(pa->p[0].aff);
3999 isl_pw_aff_free(pa);
4000 return aff;
4001 error:
4002 isl_pw_aff_free(pa);
4003 return NULL;
4006 #undef BASE
4007 #define BASE val
4009 #include <isl_multi_read_no_explicit_domain_templ.c>
4011 #undef BASE
4012 #define BASE id
4014 #include <isl_multi_read_no_explicit_domain_templ.c>
4016 /* Read a multi-affine expression from "s".
4017 * If the multi-affine expression has a domain, then the tuple
4018 * representing this domain cannot involve any affine expressions.
4019 * The tuple representing the actual expressions needs to consist
4020 * of only affine expressions. Moreover, these expressions can
4021 * only depend on parameters and input dimensions and not on other
4022 * output dimensions.
4024 __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
4026 struct vars *v;
4027 isl_set *dom = NULL;
4028 isl_multi_pw_aff *tuple = NULL;
4029 int i;
4030 isl_size dim, n;
4031 isl_space *space, *dom_space;
4032 isl_multi_aff *ma = NULL;
4034 v = vars_new(s->ctx);
4035 if (!v)
4036 return NULL;
4038 dom = read_universe_params(s, v);
4039 if (!dom)
4040 goto error;
4041 if (isl_stream_eat(s, '{'))
4042 goto error;
4044 tuple = read_tuple(s, v, 0, 0);
4045 if (!tuple)
4046 goto error;
4047 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
4048 isl_set *set;
4049 isl_space *space;
4050 isl_bool has_expr;
4052 has_expr = tuple_has_expr(tuple);
4053 if (has_expr < 0)
4054 goto error;
4055 if (has_expr)
4056 isl_die(s->ctx, isl_error_invalid,
4057 "expecting universe domain", goto error);
4058 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
4059 set = isl_set_universe(space);
4060 dom = isl_set_intersect_params(set, dom);
4061 isl_multi_pw_aff_free(tuple);
4062 tuple = read_tuple(s, v, 0, 0);
4063 if (!tuple)
4064 goto error;
4067 if (isl_stream_eat(s, '}'))
4068 goto error;
4070 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
4071 dim = isl_set_dim(dom, isl_dim_all);
4072 if (n < 0 || dim < 0)
4073 goto error;
4074 dom_space = isl_set_get_space(dom);
4075 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
4076 space = isl_space_align_params(space, isl_space_copy(dom_space));
4077 if (!isl_space_is_params(dom_space))
4078 space = isl_space_map_from_domain_and_range(
4079 isl_space_copy(dom_space), space);
4080 isl_space_free(dom_space);
4081 ma = isl_multi_aff_alloc(space);
4083 for (i = 0; i < n; ++i) {
4084 isl_pw_aff *pa;
4085 isl_aff *aff;
4086 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
4087 aff = aff_from_pw_aff(pa);
4088 if (!aff)
4089 goto error;
4090 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
4091 isl_aff_free(aff);
4092 isl_die(s->ctx, isl_error_invalid,
4093 "not an affine expression", goto error);
4095 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
4096 space = isl_multi_aff_get_domain_space(ma);
4097 aff = isl_aff_reset_domain_space(aff, space);
4098 ma = isl_multi_aff_set_aff(ma, i, aff);
4101 isl_multi_pw_aff_free(tuple);
4102 vars_free(v);
4103 isl_set_free(dom);
4104 return ma;
4105 error:
4106 isl_multi_pw_aff_free(tuple);
4107 vars_free(v);
4108 isl_set_free(dom);
4109 isl_multi_aff_free(ma);
4110 return NULL;
4113 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
4114 const char *str)
4116 isl_multi_aff *maff;
4117 isl_stream *s = isl_stream_new_str(ctx, str);
4118 if (!s)
4119 return NULL;
4120 maff = isl_stream_read_multi_aff(s);
4121 isl_stream_free(s);
4122 return maff;
4125 /* Read an isl_multi_pw_aff from "s".
4127 * The input format is similar to that of map, except that any conditions
4128 * on the domains should be specified inside the tuple since each
4129 * piecewise affine expression may have a different domain.
4130 * However, additional, shared conditions can also be specified.
4131 * This is especially useful for setting the explicit domain
4132 * of a zero-dimensional isl_multi_pw_aff.
4134 * Since we do not know in advance if the isl_multi_pw_aff lives
4135 * in a set or a map space, we first read the first tuple and check
4136 * if it is followed by a "->". If so, we convert the tuple into
4137 * the domain of the isl_multi_pw_aff and read in the next tuple.
4138 * This tuple (or the first tuple if it was not followed by a "->")
4139 * is then converted into the isl_multi_pw_aff through a call
4140 * to extract_mpa_from_tuple and the domain of the result
4141 * is intersected with the domain.
4143 * Note that the last tuple may introduce new identifiers,
4144 * but these cannot be referenced in the description of the domain.
4146 __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
4147 __isl_keep isl_stream *s)
4149 int n_dom;
4150 struct vars *v;
4151 isl_set *dom = NULL;
4152 isl_multi_pw_aff *tuple = NULL;
4153 isl_multi_pw_aff *mpa = NULL;
4155 v = vars_new(s->ctx);
4156 if (!v)
4157 return NULL;
4159 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4160 if (next_is_tuple(s)) {
4161 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4162 if (isl_stream_eat(s, ISL_TOKEN_TO))
4163 goto error;
4165 if (isl_stream_eat(s, '{'))
4166 goto error;
4168 n_dom = v->n;
4169 tuple = read_tuple(s, v, 0, 0);
4170 if (!tuple)
4171 goto error;
4172 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
4173 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
4174 dom = isl_map_domain(map);
4175 n_dom = v->n;
4176 tuple = read_tuple(s, v, 0, 0);
4177 if (!tuple)
4178 goto error;
4181 vars_drop(v, v->n - n_dom);
4182 if (isl_stream_eat_if_available(s, ':'))
4183 dom = read_formula(s, v, dom, 0);
4185 if (isl_stream_eat(s, '}'))
4186 goto error;
4188 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
4190 isl_multi_pw_aff_free(tuple);
4191 vars_free(v);
4192 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
4193 return mpa;
4194 error:
4195 isl_multi_pw_aff_free(tuple);
4196 vars_free(v);
4197 isl_set_free(dom);
4198 isl_multi_pw_aff_free(mpa);
4199 return NULL;
4202 /* Read an isl_multi_pw_aff from "str".
4204 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
4205 const char *str)
4207 isl_multi_pw_aff *mpa;
4208 isl_stream *s = isl_stream_new_str(ctx, str);
4209 if (!s)
4210 return NULL;
4211 mpa = isl_stream_read_multi_pw_aff(s);
4212 isl_stream_free(s);
4213 return mpa;
4216 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
4218 static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
4219 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
4221 isl_pw_aff *pa;
4222 isl_union_pw_aff *upa = NULL;
4223 isl_set *aff_dom;
4224 int n;
4226 n = v->n;
4227 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
4228 pa = read_pw_aff_with_dom(s, aff_dom, v);
4229 vars_drop(v, v->n - n);
4231 upa = isl_union_pw_aff_from_pw_aff(pa);
4233 while (isl_stream_eat_if_available(s, ';')) {
4234 isl_pw_aff *pa_i;
4235 isl_union_pw_aff *upa_i;
4237 n = v->n;
4238 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
4239 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
4240 vars_drop(v, v->n - n);
4242 upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
4243 upa = isl_union_pw_aff_union_add(upa, upa_i);
4246 isl_set_free(dom);
4247 return upa;
4250 /* Read an isl_union_pw_aff from "s".
4252 * First check if there are any paramters, then read in the opening brace
4253 * and use read_union_pw_aff_with_dom to read in the body of
4254 * the isl_union_pw_aff. Finally, read the closing brace.
4256 __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
4257 __isl_keep isl_stream *s)
4259 struct vars *v;
4260 isl_set *dom;
4261 isl_union_pw_aff *upa = NULL;
4263 v = vars_new(s->ctx);
4264 if (!v)
4265 return NULL;
4267 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4268 if (next_is_tuple(s)) {
4269 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4270 if (isl_stream_eat(s, ISL_TOKEN_TO))
4271 goto error;
4273 if (isl_stream_eat(s, '{'))
4274 goto error;
4276 upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
4278 if (isl_stream_eat(s, '}'))
4279 goto error;
4281 vars_free(v);
4282 isl_set_free(dom);
4283 return upa;
4284 error:
4285 vars_free(v);
4286 isl_set_free(dom);
4287 isl_union_pw_aff_free(upa);
4288 return NULL;
4291 /* Read an isl_union_pw_aff from "str".
4293 __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
4294 const char *str)
4296 isl_union_pw_aff *upa;
4297 isl_stream *s = isl_stream_new_str(ctx, str);
4298 if (!s)
4299 return NULL;
4300 upa = isl_stream_read_union_pw_aff(s);
4301 isl_stream_free(s);
4302 return upa;
4305 /* This function is called for each element in a tuple inside
4306 * isl_stream_read_multi_union_pw_aff.
4308 * Read a '{', the union piecewise affine expression body and a '}' and
4309 * add the isl_union_pw_aff to *list.
4311 static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
4312 struct vars *v, __isl_take isl_space *space, int rational, void *user)
4314 isl_set *dom;
4315 isl_union_pw_aff *upa;
4316 isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
4318 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
4319 if (isl_stream_eat(s, '{'))
4320 goto error;
4321 upa = read_union_pw_aff_with_dom(s, dom, v);
4322 *list = isl_union_pw_aff_list_add(*list, upa);
4323 if (isl_stream_eat(s, '}'))
4324 return isl_space_free(space);
4325 if (!*list)
4326 return isl_space_free(space);
4327 return space;
4328 error:
4329 isl_set_free(dom);
4330 return isl_space_free(space);
4333 /* Do the next tokens in "s" correspond to an empty tuple?
4334 * In particular, does the stream start with a '[', followed by a ']',
4335 * not followed by a "->"?
4337 static int next_is_empty_tuple(__isl_keep isl_stream *s)
4339 struct isl_token *tok, *tok2, *tok3;
4340 int is_empty_tuple = 0;
4342 tok = isl_stream_next_token(s);
4343 if (!tok)
4344 return 0;
4345 if (tok->type != '[') {
4346 isl_stream_push_token(s, tok);
4347 return 0;
4350 tok2 = isl_stream_next_token(s);
4351 if (tok2 && tok2->type == ']') {
4352 tok3 = isl_stream_next_token(s);
4353 is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
4354 if (tok3)
4355 isl_stream_push_token(s, tok3);
4357 if (tok2)
4358 isl_stream_push_token(s, tok2);
4359 isl_stream_push_token(s, tok);
4361 return is_empty_tuple;
4364 /* Do the next tokens in "s" correspond to a tuple of parameters?
4365 * In particular, does the stream start with a '[' that is not
4366 * followed by a '{' or a nested tuple?
4368 static int next_is_param_tuple(__isl_keep isl_stream *s)
4370 struct isl_token *tok, *tok2;
4371 int is_tuple;
4373 tok = isl_stream_next_token(s);
4374 if (!tok)
4375 return 0;
4376 if (tok->type != '[' || next_is_tuple(s)) {
4377 isl_stream_push_token(s, tok);
4378 return 0;
4381 tok2 = isl_stream_next_token(s);
4382 is_tuple = tok2 && tok2->type != '{';
4383 if (tok2)
4384 isl_stream_push_token(s, tok2);
4385 isl_stream_push_token(s, tok);
4387 return is_tuple;
4390 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4391 * i.e., everything except the parameter specification and
4392 * without shared domain constraints.
4393 * "v" contains a description of the identifiers parsed so far.
4394 * The parameters, if any, are specified by "space".
4396 * The body is of the form
4398 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4400 * Read the tuple, collecting the individual isl_union_pw_aff
4401 * elements in a list and construct the result from the tuple space and
4402 * the list.
4404 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
4405 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4407 isl_union_pw_aff_list *list;
4408 isl_multi_union_pw_aff *mupa;
4410 list = isl_union_pw_aff_list_alloc(s->ctx, 0);
4411 space = read_tuple_space(s, v, space, 1, 0,
4412 &read_union_pw_aff_el, &list);
4413 mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
4415 return mupa;
4418 /* Read the body of an isl_union_set from "s",
4419 * i.e., everything except the parameter specification.
4420 * "v" contains a description of the identifiers parsed so far.
4421 * The parameters, if any, are specified by "space".
4423 * First read a generic disjunction of object bodies and then try and extract
4424 * an isl_union_set from that.
4426 static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
4427 struct vars *v, __isl_take isl_space *space)
4429 struct isl_obj obj = { isl_obj_set, NULL };
4430 isl_map *map;
4432 map = isl_set_universe(space);
4433 if (isl_stream_eat(s, '{') < 0)
4434 goto error;
4435 obj = obj_read_disjuncts(s, v, map);
4436 if (isl_stream_eat(s, '}') < 0)
4437 goto error;
4438 isl_map_free(map);
4440 return extract_union_set(s->ctx, obj);
4441 error:
4442 obj.type->free(obj.v);
4443 isl_map_free(map);
4444 return NULL;
4447 /* Read the body of an isl_multi_union_pw_aff from "s",
4448 * i.e., everything except the parameter specification.
4449 * "v" contains a description of the identifiers parsed so far.
4450 * The parameters, if any, are specified by "space".
4452 * In particular, handle the special case with shared domain constraints.
4453 * These are specified as
4455 * ([...] : ...)
4457 * and are especially useful for setting the explicit domain
4458 * of a zero-dimensional isl_multi_union_pw_aff.
4459 * The core isl_multi_union_pw_aff body ([...]) is read by
4460 * read_multi_union_pw_aff_body_core.
4462 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
4463 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4465 isl_multi_union_pw_aff *mupa;
4467 if (!isl_stream_next_token_is(s, '('))
4468 return read_multi_union_pw_aff_body_core(s, v, space);
4470 if (isl_stream_eat(s, '(') < 0)
4471 goto error;
4472 mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
4473 if (isl_stream_eat_if_available(s, ':')) {
4474 isl_union_set *dom;
4476 dom = read_union_set_body(s, v, space);
4477 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4478 } else {
4479 isl_space_free(space);
4481 if (isl_stream_eat(s, ')') < 0)
4482 return isl_multi_union_pw_aff_free(mupa);
4484 return mupa;
4485 error:
4486 isl_space_free(space);
4487 return NULL;
4490 /* Read an isl_multi_union_pw_aff from "s".
4492 * The input has the form
4494 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4496 * or
4498 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4500 * Additionally, a shared domain may be specified as
4502 * ([..] : ...)
4504 * or
4506 * [..] -> ([..] : ...)
4508 * The first case is handled by the caller, the second case
4509 * is handled by read_multi_union_pw_aff_body.
4511 * We first check for the special case of an empty tuple "[]".
4512 * Then we check if there are any parameters.
4513 * Finally, read the tuple and construct the result.
4515 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
4516 __isl_keep isl_stream *s)
4518 struct vars *v;
4519 isl_set *dom = NULL;
4520 isl_space *space;
4521 isl_multi_union_pw_aff *mupa = NULL;
4523 if (next_is_empty_tuple(s)) {
4524 if (isl_stream_eat(s, '['))
4525 return NULL;
4526 if (isl_stream_eat(s, ']'))
4527 return NULL;
4528 space = isl_space_set_alloc(s->ctx, 0, 0);
4529 return isl_multi_union_pw_aff_zero(space);
4532 v = vars_new(s->ctx);
4533 if (!v)
4534 return NULL;
4536 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4537 if (next_is_param_tuple(s)) {
4538 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4539 if (isl_stream_eat(s, ISL_TOKEN_TO))
4540 goto error;
4542 space = isl_set_get_space(dom);
4543 isl_set_free(dom);
4544 mupa = read_multi_union_pw_aff_body(s, v, space);
4546 vars_free(v);
4548 return mupa;
4549 error:
4550 vars_free(v);
4551 isl_set_free(dom);
4552 isl_multi_union_pw_aff_free(mupa);
4553 return NULL;
4556 /* Read an isl_multi_union_pw_aff from "s".
4558 * In particular, handle the special case with shared domain constraints.
4559 * These are specified as
4561 * ([...] : ...)
4563 * and are especially useful for setting the explicit domain
4564 * of a zero-dimensional isl_multi_union_pw_aff.
4565 * The core isl_multi_union_pw_aff ([...]) is read by
4566 * read_multi_union_pw_aff_core.
4568 __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4569 __isl_keep isl_stream *s)
4571 isl_multi_union_pw_aff *mupa;
4573 if (!isl_stream_next_token_is(s, '('))
4574 return read_multi_union_pw_aff_core(s);
4576 if (isl_stream_eat(s, '(') < 0)
4577 return NULL;
4578 mupa = read_multi_union_pw_aff_core(s);
4579 if (isl_stream_eat_if_available(s, ':')) {
4580 isl_union_set *dom;
4582 dom = isl_stream_read_union_set(s);
4583 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4585 if (isl_stream_eat(s, ')') < 0)
4586 return isl_multi_union_pw_aff_free(mupa);
4587 return mupa;
4590 /* Read an isl_multi_union_pw_aff from "str".
4592 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4593 isl_ctx *ctx, const char *str)
4595 isl_multi_union_pw_aff *mupa;
4596 isl_stream *s = isl_stream_new_str(ctx, str);
4597 if (!s)
4598 return NULL;
4599 mupa = isl_stream_read_multi_union_pw_aff(s);
4600 isl_stream_free(s);
4601 return mupa;
4604 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4605 __isl_keep isl_stream *s)
4607 struct isl_obj obj;
4609 obj = obj_read(s);
4610 if (obj.type == isl_obj_pw_qpolynomial) {
4611 obj.type = isl_obj_union_pw_qpolynomial;
4612 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4614 if (obj.v)
4615 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4616 goto error);
4618 return obj.v;
4619 error:
4620 obj.type->free(obj.v);
4621 return NULL;
4624 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4625 isl_ctx *ctx, const char *str)
4627 isl_union_pw_qpolynomial *upwqp;
4628 isl_stream *s = isl_stream_new_str(ctx, str);
4629 if (!s)
4630 return NULL;
4631 upwqp = isl_stream_read_union_pw_qpolynomial(s);
4632 isl_stream_free(s);
4633 return upwqp;