extract out shared isl_schedule_node_grandchild
[isl.git] / isl_input.c
blob4c6839d15d8f16447f20a5a41c00d5c05f583faf
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2019 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 /* Construct an isl_pw_aff defined on a "space" (with v->n variables)
1098 * that is equal to the last of those variables.
1100 static __isl_give isl_pw_aff *identity_tuple_el_on_space(
1101 __isl_take isl_space *space, struct vars *v)
1103 isl_aff *aff;
1105 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1106 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1107 return isl_pw_aff_from_aff(aff);
1110 /* Construct an isl_pw_aff defined on the domain space of "pa"
1111 * that is equal to the last variable in "v".
1113 * That is, if D is the domain space of "pa", then construct
1115 * D[..., i] -> i.
1117 static __isl_give isl_pw_aff *init_range(__isl_keep isl_pw_aff *pa,
1118 struct vars *v)
1120 isl_space *space;
1122 space = isl_pw_aff_get_domain_space(pa);
1123 return identity_tuple_el_on_space(space, v);
1126 /* Impose the lower bound "lower" on the variable represented by "range_pa".
1128 * In particular, "range_pa" is of the form
1130 * D[..., i] -> i : C
1132 * with D also the domains space of "lower' and "C" some constraints.
1134 * Return the expression
1136 * D[..., i] -> i : C and i >= lower
1138 static __isl_give isl_pw_aff *set_lower(__isl_take isl_pw_aff *range_pa,
1139 __isl_take isl_pw_aff *lower)
1141 isl_set *range;
1143 range = isl_pw_aff_ge_set(isl_pw_aff_copy(range_pa), lower);
1144 return isl_pw_aff_intersect_domain(range_pa, range);
1147 /* Impose the upper bound "upper" on the variable represented by "range_pa".
1149 * In particular, "range_pa" is of the form
1151 * D[..., i] -> i : C
1153 * with D also the domains space of "upper' and "C" some constraints.
1155 * Return the expression
1157 * D[..., i] -> i : C and i <= upper
1159 static __isl_give isl_pw_aff *set_upper(__isl_take isl_pw_aff *range_pa,
1160 __isl_take isl_pw_aff *upper)
1162 isl_set *range;
1164 range = isl_pw_aff_le_set(isl_pw_aff_copy(range_pa), upper);
1165 return isl_pw_aff_intersect_domain(range_pa, range);
1168 /* Construct a piecewise affine expression corresponding
1169 * to the last variable in "v" that is greater than or equal to "pa".
1171 * In particular, if D is the domain space of "pa",
1172 * then construct the expression
1174 * D[..., i] -> i,
1176 * impose lower bound "pa" and return
1178 * D[..., i] -> i : i >= pa
1180 static __isl_give isl_pw_aff *construct_lower(__isl_take isl_pw_aff *pa,
1181 struct vars *v)
1183 return set_lower(init_range(pa, v), pa);
1186 /* Construct a piecewise affine expression corresponding
1187 * to the last variable in "v" that is smaller than or equal to "pa".
1189 * In particular, if D is the domain space of "pa",
1190 * then construct the expression
1192 * D[..., i] -> i,
1194 * impose lower bound "pa" and return
1196 * D[..., i] -> i : i <= pa
1198 static __isl_give isl_pw_aff *construct_upper(__isl_take isl_pw_aff *pa,
1199 struct vars *v)
1201 return set_upper(init_range(pa, v), pa);
1204 /* Construct a piecewise affine expression corresponding
1205 * to the last variable in "v" that ranges between "pa" and "pa2".
1207 * In particular, if D is the domain space of "pa" (and "pa2"),
1208 * then construct the expression
1210 * D[..., i] -> i,
1212 * impose lower bound "pa" and upper bound "pa2" and return
1214 * D[..., i] -> i : pa <= i <= pa2
1216 static __isl_give isl_pw_aff *construct_range(__isl_take isl_pw_aff *pa,
1217 __isl_take isl_pw_aff *pa2, struct vars *v)
1219 return set_upper(set_lower(init_range(pa, v), pa), pa2);
1222 static int resolve_paren_expr(__isl_keep isl_stream *s,
1223 struct vars *v, __isl_take isl_map *map, int rational);
1225 /* Given that the (piecewise) affine expression "pa"
1226 * has just been parsed, followed by a colon,
1227 * continue parsing as part of a piecewise affine expression.
1229 * In particular, check if the colon is followed by a condition.
1230 * If so, parse the conditions(a) on "pa" and include them in the domain.
1231 * Otherwise, if the colon is followed by another (piecewise) affine expression
1232 * then consider the two expressions as endpoints of a range of values and
1233 * return a piecewise affine expression that takes values in that range.
1234 * Note that an affine expression followed by a comparison operator
1235 * is considered to be part of a condition.
1236 * If the colon is not followed by anything (inside the tuple element),
1237 * then consider "pa" as a lower bound on a range of values without upper bound
1238 * and return a piecewise affine expression that takes values in that range.
1240 static __isl_give isl_pw_aff *update_piecewise_affine_colon(
1241 __isl_take isl_pw_aff *pa, __isl_keep isl_stream *s,
1242 struct vars *v, int rational)
1244 isl_space *dom_space;
1245 isl_map *map;
1247 dom_space = isl_pw_aff_get_domain_space(pa);
1248 map = isl_map_universe(isl_space_from_domain(dom_space));
1250 if (isl_stream_next_token_is(s, '('))
1251 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1252 goto error;
1253 if (next_is_end_tuple_element(s)) {
1254 isl_map_free(map);
1255 return construct_lower(pa, v);
1257 if (!next_is_condition_start(s)) {
1258 int line = -1, col = -1;
1259 isl_space *space;
1260 isl_pw_aff *pa2;
1262 set_current_line_col(s, &line, &col);
1263 space = isl_space_wrap(isl_map_get_space(map));
1264 pa2 = accept_affine(s, space, v);
1265 if (rational)
1266 pa2 = isl_pw_aff_set_rational(pa2);
1267 if (!next_is_comparator(s)) {
1268 isl_map_free(map);
1269 pa2 = isl_pw_aff_domain_factor_domain(pa2);
1270 return construct_range(pa, pa2, v);
1272 if (push_aff(s, line, col, pa2) < 0)
1273 goto error;
1276 map = read_formula(s, v, map, rational);
1277 pa = isl_pw_aff_intersect_domain(pa, isl_map_domain(map));
1279 return pa;
1280 error:
1281 isl_map_free(map);
1282 isl_pw_aff_free(pa);
1283 return NULL;
1286 /* Accept a piecewise affine expression.
1288 * At the outer level, the piecewise affine expression may be of the form
1290 * aff1 : condition1; aff2 : conditions2; ...
1292 * or one of
1294 * aff :
1295 * aff1 : aff2
1296 * : aff
1299 * or simply
1301 * aff
1303 * each of the affine expressions may in turn include ternary operators.
1305 * If the first token is a colon, then the expression must be
1306 * ":" or ": aff2", depending on whether anything follows the colon
1307 * inside the tuple element.
1308 * The first is considered to represent an arbitrary value.
1309 * The second is considered to represent a range of values
1310 * with the given upper bound and no lower bound.
1312 * There may be parentheses around some subexpression of "aff1"
1313 * around "aff1" itself, around "aff1 : condition1" and/or
1314 * around the entire piecewise affine expression.
1315 * We therefore remove the opening parenthesis (if any) from the stream
1316 * in case the closing parenthesis follows the colon, but if the closing
1317 * parenthesis is the first thing in the stream after the parsed affine
1318 * expression, we push the parsed expression onto the stream and parse
1319 * again in case the parentheses enclose some subexpression of "aff1".
1321 static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1322 __isl_take isl_space *space, struct vars *v, int rational)
1324 isl_pw_aff *res;
1325 isl_space *res_space;
1327 if (isl_stream_eat_if_available(s, ':')) {
1328 if (next_is_end_tuple_element(s))
1329 return identity_tuple_el_on_space(space, v);
1330 else
1331 return construct_upper(accept_affine(s, space, v), v);
1334 res_space = isl_space_from_domain(isl_space_copy(space));
1335 res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1336 res = isl_pw_aff_empty(res_space);
1337 do {
1338 isl_pw_aff *pa;
1339 int seen_paren;
1340 int line = -1, col = -1;
1342 set_current_line_col(s, &line, &col);
1343 seen_paren = isl_stream_eat_if_available(s, '(');
1344 if (seen_paren)
1345 pa = accept_piecewise_affine(s, isl_space_copy(space),
1346 v, rational);
1347 else
1348 pa = accept_extended_affine(s, isl_space_copy(space),
1349 v, rational);
1350 if (seen_paren && isl_stream_eat_if_available(s, ')')) {
1351 seen_paren = 0;
1352 if (push_aff(s, line, col, pa) < 0)
1353 goto error;
1354 pa = accept_extended_affine(s, isl_space_copy(space),
1355 v, rational);
1357 if (isl_stream_eat_if_available(s, ':'))
1358 pa = update_piecewise_affine_colon(pa, s, v, rational);
1360 res = isl_pw_aff_union_add(res, pa);
1362 if (seen_paren && isl_stream_eat(s, ')'))
1363 goto error;
1364 } while (isl_stream_eat_if_available(s, ';'));
1366 isl_space_free(space);
1368 return res;
1369 error:
1370 isl_space_free(space);
1371 return isl_pw_aff_free(res);
1374 /* Read an affine expression from "s" for use in read_tuple.
1376 * accept_extended_affine requires a wrapped space as input.
1377 * read_tuple on the other hand expects each isl_pw_aff
1378 * to have an anonymous space. We therefore adjust the space
1379 * of the isl_pw_aff before returning it.
1381 static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1382 struct vars *v, int rational)
1384 isl_space *space;
1385 isl_pw_aff *def;
1387 space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1389 def = accept_piecewise_affine(s, space, v, rational);
1390 def = isl_pw_aff_domain_factor_domain(def);
1392 return def;
1395 /* Read a list of tuple elements by calling "read_el" on each of them and
1396 * return a space with the same number of set dimensions derived from
1397 * the parameter space "space" and possibly updated by "read_el".
1398 * The elements in the list are separated by either "," or "][".
1399 * If "comma" is set then only "," is allowed.
1401 static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1402 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1403 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1404 struct vars *v, __isl_take isl_space *space, int rational,
1405 void *user),
1406 void *user)
1408 if (!space)
1409 return NULL;
1411 space = isl_space_set_from_params(space);
1413 if (isl_stream_next_token_is(s, ']'))
1414 return space;
1416 for (;;) {
1417 struct isl_token *tok;
1419 space = isl_space_add_dims(space, isl_dim_set, 1);
1421 space = read_el(s, v, space, rational, user);
1422 if (!space)
1423 return NULL;
1425 tok = isl_stream_next_token(s);
1426 if (!comma && tok && tok->type == ']' &&
1427 isl_stream_next_token_is(s, '[')) {
1428 isl_token_free(tok);
1429 tok = isl_stream_next_token(s);
1430 } else if (!tok || tok->type != ',') {
1431 if (tok)
1432 isl_stream_push_token(s, tok);
1433 break;
1436 isl_token_free(tok);
1439 return space;
1442 /* Read a tuple space from "s" derived from the parameter space "space".
1443 * Call "read_el" on each element in the tuples.
1445 static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1446 struct vars *v, __isl_take isl_space *space, int rational, int comma,
1447 __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1448 struct vars *v, __isl_take isl_space *space, int rational,
1449 void *user),
1450 void *user)
1452 struct isl_token *tok;
1453 char *name = NULL;
1454 isl_space *res = NULL;
1456 tok = isl_stream_next_token(s);
1457 if (!tok)
1458 goto error;
1459 if (tok->type == ISL_TOKEN_IDENT || tok->is_keyword) {
1460 name = strdup(tok->u.s);
1461 isl_token_free(tok);
1462 if (!name)
1463 goto error;
1464 } else
1465 isl_stream_push_token(s, tok);
1466 if (isl_stream_eat(s, '['))
1467 goto error;
1468 if (next_is_tuple(s)) {
1469 isl_space *out;
1470 res = read_tuple_space(s, v, isl_space_copy(space),
1471 rational, comma, read_el, user);
1472 if (isl_stream_eat(s, ISL_TOKEN_TO))
1473 goto error;
1474 out = read_tuple_space(s, v, isl_space_copy(space),
1475 rational, comma, read_el, user);
1476 res = isl_space_product(res, out);
1477 } else
1478 res = read_tuple_list(s, v, isl_space_copy(space),
1479 rational, comma, read_el, user);
1480 if (isl_stream_eat(s, ']'))
1481 goto error;
1483 if (name) {
1484 res = isl_space_set_tuple_name(res, isl_dim_set, name);
1485 free(name);
1488 isl_space_free(space);
1489 return res;
1490 error:
1491 free(name);
1492 isl_space_free(res);
1493 isl_space_free(space);
1494 return NULL;
1497 /* Construct an isl_pw_aff defined on a space with v->n variables
1498 * that is equal to the last of those variables.
1500 static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1502 isl_space *space;
1504 space = isl_space_set_alloc(v->ctx, 0, v->n);
1505 return identity_tuple_el_on_space(space, v);
1508 /* This function is called for each element in a tuple inside read_tuple.
1509 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1510 * over a space containing all variables in "v" defined so far.
1511 * The isl_pw_aff expresses the new variable in terms of earlier variables
1512 * if a definition is provided. Otherwise, it is represented as being
1513 * equal to itself.
1514 * Add the isl_pw_aff to *list.
1515 * If the new variable was named, then adjust "space" accordingly and
1516 * return the updated space.
1518 static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1519 struct vars *v, __isl_take isl_space *space, int rational, void *user)
1521 isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1522 isl_pw_aff *pa;
1523 struct isl_token *tok;
1524 int new_name = 0;
1526 tok = next_token(s);
1527 if (!tok) {
1528 isl_stream_error(s, NULL, "unexpected EOF");
1529 return isl_space_free(space);
1532 if (tok->type == ISL_TOKEN_IDENT) {
1533 int n = v->n;
1534 int p = vars_pos(v, tok->u.s, -1);
1535 if (p < 0)
1536 goto error;
1537 new_name = p >= n;
1540 if (tok->type == '*') {
1541 if (vars_add_anon(v) < 0)
1542 goto error;
1543 isl_token_free(tok);
1544 pa = identity_tuple_el(v);
1545 } else if (new_name) {
1546 isl_size pos = isl_space_dim(space, isl_dim_out);
1547 if (pos < 0)
1548 goto error;
1549 pos -= 1;
1550 space = space_set_dim_name(space, pos, v->v->name);
1551 isl_token_free(tok);
1552 if (isl_stream_eat_if_available(s, '='))
1553 pa = read_tuple_var_def(s, v, rational);
1554 else
1555 pa = identity_tuple_el(v);
1556 } else {
1557 isl_stream_push_token(s, tok);
1558 tok = NULL;
1559 if (vars_add_anon(v) < 0)
1560 goto error;
1561 pa = read_tuple_var_def(s, v, rational);
1564 *list = isl_pw_aff_list_add(*list, pa);
1565 if (!*list)
1566 return isl_space_free(space);
1568 return space;
1569 error:
1570 isl_token_free(tok);
1571 return isl_space_free(space);
1574 /* Read a tuple and represent it as an isl_multi_pw_aff.
1575 * The range space of the isl_multi_pw_aff is the space of the tuple.
1576 * The domain space is an anonymous space
1577 * with a dimension for each variable in the set of variables in "v",
1578 * including the variables in the range.
1579 * If a given dimension is not defined in terms of earlier dimensions in
1580 * the input, then the corresponding isl_pw_aff is set equal to one time
1581 * the variable corresponding to the dimension being defined.
1583 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1584 * Each element in this list is defined over a space representing
1585 * the variables defined so far. We need to adjust the earlier
1586 * elements to have as many variables in the domain as the final
1587 * element in the list.
1589 static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1590 struct vars *v, int rational, int comma)
1592 int i;
1593 isl_size n;
1594 isl_space *space;
1595 isl_pw_aff_list *list;
1597 space = isl_space_params_alloc(v->ctx, 0);
1598 list = isl_pw_aff_list_alloc(s->ctx, 0);
1599 space = read_tuple_space(s, v, space, rational, comma,
1600 &read_tuple_pw_aff_el, &list);
1601 n = isl_space_dim(space, isl_dim_set);
1602 if (n < 0)
1603 space = isl_space_free(space);
1604 for (i = 0; i + 1 < n; ++i) {
1605 isl_pw_aff *pa;
1607 pa = isl_pw_aff_list_get_pw_aff(list, i);
1608 pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1609 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1612 space = isl_space_from_range(space);
1613 space = isl_space_add_dims(space, isl_dim_in, v->n);
1614 return isl_multi_pw_aff_from_pw_aff_list(space, list);
1617 /* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1618 * We first create the appropriate space in "map" based on the range
1619 * space of this isl_multi_pw_aff. Then, we add equalities based
1620 * on the affine expressions. These live in an anonymous space,
1621 * however, so we first need to reset the space to that of "map".
1623 static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1624 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1625 int rational)
1627 int i;
1628 isl_size n;
1629 isl_ctx *ctx;
1630 isl_space *space = NULL;
1632 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1633 if (!map || n < 0)
1634 goto error;
1635 ctx = isl_multi_pw_aff_get_ctx(tuple);
1636 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1637 if (!space)
1638 goto error;
1640 if (type == isl_dim_param) {
1641 if (isl_space_has_tuple_name(space, isl_dim_set) ||
1642 isl_space_is_wrapping(space)) {
1643 isl_die(ctx, isl_error_invalid,
1644 "parameter tuples cannot be named or nested",
1645 goto error);
1647 map = isl_map_add_dims(map, type, n);
1648 for (i = 0; i < n; ++i) {
1649 isl_id *id;
1650 if (!isl_space_has_dim_name(space, isl_dim_set, i))
1651 isl_die(ctx, isl_error_invalid,
1652 "parameters must be named",
1653 goto error);
1654 id = isl_space_get_dim_id(space, isl_dim_set, i);
1655 map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1657 } else if (type == isl_dim_in) {
1658 isl_set *set;
1660 set = isl_set_universe(isl_space_copy(space));
1661 if (rational)
1662 set = isl_set_set_rational(set);
1663 set = isl_set_intersect_params(set, isl_map_params(map));
1664 map = isl_map_from_domain(set);
1665 } else {
1666 isl_set *set;
1668 set = isl_set_universe(isl_space_copy(space));
1669 if (rational)
1670 set = isl_set_set_rational(set);
1671 map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1674 for (i = 0; i < n; ++i) {
1675 isl_pw_aff *pa;
1676 isl_space *space;
1677 isl_aff *aff;
1678 isl_set *set;
1679 isl_map *map_i;
1681 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1682 space = isl_pw_aff_get_domain_space(pa);
1683 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1684 aff = isl_aff_add_coefficient_si(aff,
1685 isl_dim_in, v->n - n + i, -1);
1686 pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1687 if (rational)
1688 pa = isl_pw_aff_set_rational(pa);
1689 set = isl_pw_aff_zero_set(pa);
1690 map_i = isl_map_from_range(set);
1691 map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1692 map = isl_map_intersect(map, map_i);
1695 isl_space_free(space);
1696 isl_multi_pw_aff_free(tuple);
1697 return map;
1698 error:
1699 isl_space_free(space);
1700 isl_multi_pw_aff_free(tuple);
1701 isl_map_free(map);
1702 return NULL;
1705 /* Read a tuple from "s" and add it to "map".
1706 * The tuple is initially represented as an isl_multi_pw_aff and
1707 * then added to "map".
1709 static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1710 __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1711 int rational, int comma)
1713 isl_multi_pw_aff *tuple;
1715 tuple = read_tuple(s, v, rational, comma);
1716 if (!tuple)
1717 return isl_map_free(map);
1719 return map_from_tuple(tuple, map, type, v, rational);
1722 /* Given two equal-length lists of piecewise affine expression with the space
1723 * of "set" as domain, construct a set in the same space that expresses
1724 * that "left" and "right" satisfy the comparison "type".
1726 * A space is constructed of the same dimension as the number of elements
1727 * in the two lists. The comparison is then expressed in a map from
1728 * this space to itself and wrapped into a set. Finally the two lists
1729 * of piecewise affine expressions are plugged into this set.
1731 * Let S be the space of "set" and T the constructed space.
1732 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1733 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1734 * while the comparison is first expressed in T -> T, then [T -> T]
1735 * and finally in S.
1737 static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1738 __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1740 isl_space *space;
1741 isl_size n;
1742 isl_multi_pw_aff *mpa1, *mpa2;
1744 n = isl_pw_aff_list_n_pw_aff(left);
1745 if (!set || n < 0 || !right)
1746 goto error;
1748 space = isl_set_get_space(set);
1749 space = isl_space_from_domain(space);
1750 space = isl_space_add_dims(space, isl_dim_out, n);
1751 mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1752 mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1753 mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1755 space = isl_space_range(space);
1756 switch (type) {
1757 case ISL_TOKEN_LEX_LT:
1758 set = isl_map_wrap(isl_map_lex_lt(space));
1759 break;
1760 case ISL_TOKEN_LEX_GT:
1761 set = isl_map_wrap(isl_map_lex_gt(space));
1762 break;
1763 case ISL_TOKEN_LEX_LE:
1764 set = isl_map_wrap(isl_map_lex_le(space));
1765 break;
1766 case ISL_TOKEN_LEX_GE:
1767 set = isl_map_wrap(isl_map_lex_ge(space));
1768 break;
1769 default:
1770 isl_multi_pw_aff_free(mpa1);
1771 isl_space_free(space);
1772 isl_die(isl_set_get_ctx(set), isl_error_internal,
1773 "unhandled list comparison type", return NULL);
1775 set = isl_set_preimage_multi_pw_aff(set, mpa1);
1776 return set;
1777 error:
1778 isl_pw_aff_list_free(left);
1779 isl_pw_aff_list_free(right);
1780 return NULL;
1783 /* Construct constraints of the form
1785 * a op b
1787 * where a is an element in "left", op is an operator of type "type" and
1788 * b is an element in "right", add the constraints to "set" and return
1789 * the result.
1790 * "rational" is set if the constraints should be treated as
1791 * a rational constraints.
1793 * If "type" is the type of a comparison operator between lists
1794 * of affine expressions, then a single (compound) constraint
1795 * is constructed by list_cmp instead.
1797 static __isl_give isl_set *construct_constraints(
1798 __isl_take isl_set *set, int type,
1799 __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1800 int rational)
1802 isl_set *cond;
1804 left = isl_pw_aff_list_copy(left);
1805 right = isl_pw_aff_list_copy(right);
1806 if (rational) {
1807 left = isl_pw_aff_list_set_rational(left);
1808 right = isl_pw_aff_list_set_rational(right);
1810 if (is_list_comparator_type(type))
1811 cond = list_cmp(set, type, left, right);
1812 else if (type == ISL_TOKEN_LE)
1813 cond = isl_pw_aff_list_le_set(left, right);
1814 else if (type == ISL_TOKEN_GE)
1815 cond = isl_pw_aff_list_ge_set(left, right);
1816 else if (type == ISL_TOKEN_LT)
1817 cond = isl_pw_aff_list_lt_set(left, right);
1818 else if (type == ISL_TOKEN_GT)
1819 cond = isl_pw_aff_list_gt_set(left, right);
1820 else if (type == ISL_TOKEN_NE)
1821 cond = isl_pw_aff_list_ne_set(left, right);
1822 else
1823 cond = isl_pw_aff_list_eq_set(left, right);
1825 return isl_set_intersect(set, cond);
1828 /* Read a constraint from "s", add it to "map" and return the result.
1829 * "v" contains a description of the identifiers parsed so far.
1830 * "rational" is set if the constraint should be treated as
1831 * a rational constraint.
1832 * The constraint read from "s" may be applied to multiple pairs
1833 * of affine expressions and may be chained.
1834 * In particular, a list of affine expressions is read, followed
1835 * by a comparison operator and another list of affine expressions.
1836 * The comparison operator is then applied to each pair of elements
1837 * in the two lists and the results are added to "map".
1838 * However, if the operator expects two lists of affine expressions,
1839 * then it is applied directly to those lists and the two lists
1840 * are required to have the same length.
1841 * If the next token is another comparison operator, then another
1842 * list of affine expressions is read and the process repeats.
1844 * The processing is performed on a wrapped copy of "map" because
1845 * an affine expression cannot have a binary relation as domain.
1847 static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1848 struct vars *v, __isl_take isl_map *map, int rational)
1850 struct isl_token *tok;
1851 int type;
1852 isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1853 isl_size n1, n2;
1854 isl_set *set;
1856 set = isl_map_wrap(map);
1857 list1 = accept_affine_list(s, isl_set_get_space(set), v);
1858 if (!list1)
1859 goto error;
1860 tok = isl_stream_next_token(s);
1861 if (!is_comparator(tok)) {
1862 isl_stream_error(s, tok, "missing operator");
1863 if (tok)
1864 isl_stream_push_token(s, tok);
1865 goto error;
1867 type = tok->type;
1868 isl_token_free(tok);
1869 for (;;) {
1870 list2 = accept_affine_list(s, isl_set_get_space(set), v);
1871 n1 = isl_pw_aff_list_n_pw_aff(list1);
1872 n2 = isl_pw_aff_list_n_pw_aff(list2);
1873 if (n1 < 0 || n2 < 0)
1874 goto error;
1875 if (is_list_comparator_type(type) && n1 != n2) {
1876 isl_stream_error(s, NULL,
1877 "list arguments not of same size");
1878 goto error;
1881 set = construct_constraints(set, type, list1, list2, rational);
1882 isl_pw_aff_list_free(list1);
1883 list1 = list2;
1885 if (!next_is_comparator(s))
1886 break;
1887 tok = isl_stream_next_token(s);
1888 type = tok->type;
1889 isl_token_free(tok);
1891 isl_pw_aff_list_free(list1);
1893 return isl_set_unwrap(set);
1894 error:
1895 isl_pw_aff_list_free(list1);
1896 isl_pw_aff_list_free(list2);
1897 isl_set_free(set);
1898 return NULL;
1901 static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
1902 struct vars *v, __isl_take isl_map *map, int rational)
1904 int n = v->n;
1905 int seen_paren = isl_stream_eat_if_available(s, '(');
1907 map = isl_map_from_domain(isl_map_wrap(map));
1908 map = read_defined_var_list(s, v, map, rational);
1910 if (isl_stream_eat(s, ':'))
1911 goto error;
1913 map = read_formula(s, v, map, rational);
1914 map = isl_set_unwrap(isl_map_domain(map));
1916 vars_drop(v, v->n - n);
1917 if (seen_paren && isl_stream_eat(s, ')'))
1918 goto error;
1920 return map;
1921 error:
1922 isl_map_free(map);
1923 return NULL;
1926 /* Parse an expression between parentheses and push the result
1927 * back on the stream.
1929 * The parsed expression may be either an affine expression
1930 * or a condition. The first type is pushed onto the stream
1931 * as an isl_pw_aff, while the second is pushed as an isl_map.
1933 * If the initial token indicates the start of a condition,
1934 * we parse it as such.
1935 * Otherwise, we first parse an affine expression and push
1936 * that onto the stream. If the affine expression covers the
1937 * entire expression between parentheses, we return.
1938 * Otherwise, we assume that the affine expression is the
1939 * start of a condition and continue parsing.
1941 static int resolve_paren_expr(__isl_keep isl_stream *s,
1942 struct vars *v, __isl_take isl_map *map, int rational)
1944 struct isl_token *tok, *tok2;
1945 int has_paren;
1946 int line, col;
1947 isl_pw_aff *pwaff;
1949 tok = isl_stream_next_token(s);
1950 if (!tok || tok->type != '(')
1951 goto error;
1953 if (isl_stream_next_token_is(s, '('))
1954 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1955 goto error;
1957 if (next_is_condition_start(s)) {
1958 map = read_formula(s, v, map, rational);
1959 if (isl_stream_eat(s, ')'))
1960 goto error;
1961 tok->type = ISL_TOKEN_MAP;
1962 tok->u.map = map;
1963 isl_stream_push_token(s, tok);
1964 return 0;
1967 tok2 = isl_stream_next_token(s);
1968 if (!tok2)
1969 goto error;
1970 line = tok2->line;
1971 col = tok2->col;
1972 isl_stream_push_token(s, tok2);
1974 pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1975 if (!pwaff)
1976 goto error;
1978 has_paren = isl_stream_eat_if_available(s, ')');
1980 if (push_aff(s, line, col, pwaff) < 0)
1981 goto error;
1983 if (has_paren) {
1984 isl_token_free(tok);
1985 isl_map_free(map);
1986 return 0;
1989 map = read_formula(s, v, map, rational);
1990 if (isl_stream_eat(s, ')'))
1991 goto error;
1993 tok->type = ISL_TOKEN_MAP;
1994 tok->u.map = map;
1995 isl_stream_push_token(s, tok);
1997 return 0;
1998 error:
1999 isl_token_free(tok);
2000 isl_map_free(map);
2001 return -1;
2004 static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
2005 struct vars *v, __isl_take isl_map *map, int rational)
2007 if (isl_stream_next_token_is(s, '('))
2008 if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
2009 goto error;
2011 if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
2012 struct isl_token *tok;
2013 tok = isl_stream_next_token(s);
2014 if (!tok)
2015 goto error;
2016 isl_map_free(map);
2017 map = isl_map_copy(tok->u.map);
2018 isl_token_free(tok);
2019 return map;
2022 if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
2023 return read_exists(s, v, map, rational);
2025 if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
2026 return map;
2028 if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
2029 isl_space *space = isl_map_get_space(map);
2030 isl_map_free(map);
2031 return isl_map_empty(space);
2034 return add_constraint(s, v, map, rational);
2035 error:
2036 isl_map_free(map);
2037 return NULL;
2040 static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
2041 struct vars *v, __isl_take isl_map *map, int rational)
2043 isl_map *res;
2044 int negate;
2046 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
2047 res = read_conjunct(s, v, isl_map_copy(map), rational);
2048 if (negate)
2049 res = isl_map_subtract(isl_map_copy(map), res);
2051 while (res && isl_stream_eat_if_available(s, ISL_TOKEN_AND)) {
2052 isl_map *res_i;
2054 negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
2055 res_i = read_conjunct(s, v, isl_map_copy(map), rational);
2056 if (negate)
2057 res = isl_map_subtract(res, res_i);
2058 else
2059 res = isl_map_intersect(res, res_i);
2062 isl_map_free(map);
2063 return res;
2066 static __isl_give isl_map *read_disjuncts(__isl_keep isl_stream *s,
2067 struct vars *v, __isl_take isl_map *map, int rational)
2069 isl_map *res;
2071 if (isl_stream_next_token_is(s, '}'))
2072 return map;
2074 res = read_conjuncts(s, v, isl_map_copy(map), rational);
2075 while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
2076 isl_map *res_i;
2078 res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
2079 res = isl_map_union(res, res_i);
2082 isl_map_free(map);
2083 return res;
2086 /* Read a first order formula from "s", add the corresponding
2087 * constraints to "map" and return the result.
2089 * In particular, read a formula of the form
2093 * or
2095 * a implies b
2097 * where a and b are disjunctions.
2099 * In the first case, map is replaced by
2101 * map \cap { [..] : a }
2103 * In the second case, it is replaced by
2105 * (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
2107 static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
2108 struct vars *v, __isl_take isl_map *map, int rational)
2110 isl_map *res;
2112 res = read_disjuncts(s, v, isl_map_copy(map), rational);
2114 if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
2115 isl_map *res2;
2117 res = isl_map_subtract(isl_map_copy(map), res);
2118 res2 = read_disjuncts(s, v, map, rational);
2119 res = isl_map_union(res, res2);
2120 } else
2121 isl_map_free(map);
2123 return res;
2126 static isl_size polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
2128 isl_size n_out, n_in, n_param, n_div;
2130 n_param = isl_basic_map_dim(bmap, isl_dim_param);
2131 n_in = isl_basic_map_dim(bmap, isl_dim_in);
2132 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2133 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2134 if (n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
2135 return isl_size_error;
2137 if (pos < n_out)
2138 return 1 + n_param + n_in + pos;
2139 pos -= n_out;
2141 if (pos < n_in)
2142 return 1 + n_param + pos;
2143 pos -= n_in;
2145 if (pos < n_div)
2146 return 1 + n_param + n_in + n_out + pos;
2147 pos -= n_div;
2149 if (pos < n_param)
2150 return 1 + pos;
2152 return 0;
2155 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
2156 __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
2158 int j;
2159 struct isl_token *tok;
2160 int type;
2161 int k;
2162 isl_int *c;
2163 isl_size total;
2165 if (!bmap)
2166 return NULL;
2168 tok = isl_stream_next_token(s);
2169 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2170 isl_stream_error(s, tok, "expecting coefficient");
2171 if (tok)
2172 isl_stream_push_token(s, tok);
2173 goto error;
2175 if (!tok->on_new_line) {
2176 isl_stream_error(s, tok, "coefficient should appear on new line");
2177 isl_stream_push_token(s, tok);
2178 goto error;
2181 type = isl_int_get_si(tok->u.v);
2182 isl_token_free(tok);
2184 isl_assert(s->ctx, type == 0 || type == 1, goto error);
2185 if (type == 0) {
2186 k = isl_basic_map_alloc_equality(bmap);
2187 c = bmap->eq[k];
2188 } else {
2189 k = isl_basic_map_alloc_inequality(bmap);
2190 c = bmap->ineq[k];
2192 if (k < 0)
2193 goto error;
2195 total = isl_basic_map_dim(bmap, isl_dim_all);
2196 if (total < 0)
2197 return isl_basic_map_free(bmap);
2198 for (j = 0; j < 1 + total; ++j) {
2199 isl_size pos;
2200 tok = isl_stream_next_token(s);
2201 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2202 isl_stream_error(s, tok, "expecting coefficient");
2203 if (tok)
2204 isl_stream_push_token(s, tok);
2205 goto error;
2207 if (tok->on_new_line) {
2208 isl_stream_error(s, tok,
2209 "coefficient should not appear on new line");
2210 isl_stream_push_token(s, tok);
2211 goto error;
2213 pos = polylib_pos_to_isl_pos(bmap, j);
2214 if (pos >= 0)
2215 isl_int_set(c[pos], tok->u.v);
2216 isl_token_free(tok);
2217 if (pos < 0)
2218 return isl_basic_map_free(bmap);
2221 return bmap;
2222 error:
2223 isl_basic_map_free(bmap);
2224 return NULL;
2227 static __isl_give isl_basic_map *basic_map_read_polylib(
2228 __isl_keep isl_stream *s)
2230 int i;
2231 struct isl_token *tok;
2232 struct isl_token *tok2;
2233 int n_row, n_col;
2234 int on_new_line;
2235 unsigned in = 0, out, local = 0;
2236 struct isl_basic_map *bmap = NULL;
2237 int nparam = 0;
2239 tok = isl_stream_next_token(s);
2240 if (!tok) {
2241 isl_stream_error(s, NULL, "unexpected EOF");
2242 return NULL;
2244 tok2 = isl_stream_next_token(s);
2245 if (!tok2) {
2246 isl_token_free(tok);
2247 isl_stream_error(s, NULL, "unexpected EOF");
2248 return NULL;
2250 if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
2251 isl_stream_push_token(s, tok2);
2252 isl_stream_push_token(s, tok);
2253 isl_stream_error(s, NULL,
2254 "expecting constraint matrix dimensions");
2255 return NULL;
2257 n_row = isl_int_get_si(tok->u.v);
2258 n_col = isl_int_get_si(tok2->u.v);
2259 on_new_line = tok2->on_new_line;
2260 isl_token_free(tok2);
2261 isl_token_free(tok);
2262 isl_assert(s->ctx, !on_new_line, return NULL);
2263 isl_assert(s->ctx, n_row >= 0, return NULL);
2264 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
2265 tok = isl_stream_next_token_on_same_line(s);
2266 if (tok) {
2267 if (tok->type != ISL_TOKEN_VALUE) {
2268 isl_stream_error(s, tok,
2269 "expecting number of output dimensions");
2270 isl_stream_push_token(s, tok);
2271 goto error;
2273 out = isl_int_get_si(tok->u.v);
2274 isl_token_free(tok);
2276 tok = isl_stream_next_token_on_same_line(s);
2277 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2278 isl_stream_error(s, tok,
2279 "expecting number of input dimensions");
2280 if (tok)
2281 isl_stream_push_token(s, tok);
2282 goto error;
2284 in = isl_int_get_si(tok->u.v);
2285 isl_token_free(tok);
2287 tok = isl_stream_next_token_on_same_line(s);
2288 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2289 isl_stream_error(s, tok,
2290 "expecting number of existentials");
2291 if (tok)
2292 isl_stream_push_token(s, tok);
2293 goto error;
2295 local = isl_int_get_si(tok->u.v);
2296 isl_token_free(tok);
2298 tok = isl_stream_next_token_on_same_line(s);
2299 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2300 isl_stream_error(s, tok,
2301 "expecting number of parameters");
2302 if (tok)
2303 isl_stream_push_token(s, tok);
2304 goto error;
2306 nparam = isl_int_get_si(tok->u.v);
2307 isl_token_free(tok);
2308 if (n_col != 1 + out + in + local + nparam + 1) {
2309 isl_stream_error(s, NULL,
2310 "dimensions don't match");
2311 goto error;
2313 } else
2314 out = n_col - 2 - nparam;
2315 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2316 if (!bmap)
2317 return NULL;
2319 for (i = 0; i < local; ++i) {
2320 int k = isl_basic_map_alloc_div(bmap);
2321 if (k < 0)
2322 goto error;
2323 isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2326 for (i = 0; i < n_row; ++i)
2327 bmap = basic_map_read_polylib_constraint(s, bmap);
2329 tok = isl_stream_next_token_on_same_line(s);
2330 if (tok) {
2331 isl_stream_error(s, tok, "unexpected extra token on line");
2332 isl_stream_push_token(s, tok);
2333 goto error;
2336 bmap = isl_basic_map_simplify(bmap);
2337 bmap = isl_basic_map_finalize(bmap);
2338 return bmap;
2339 error:
2340 isl_basic_map_free(bmap);
2341 return NULL;
2344 static __isl_give isl_map *map_read_polylib(__isl_keep isl_stream *s)
2346 struct isl_token *tok;
2347 struct isl_token *tok2;
2348 int i, n;
2349 struct isl_map *map;
2351 tok = isl_stream_next_token(s);
2352 if (!tok) {
2353 isl_stream_error(s, NULL, "unexpected EOF");
2354 return NULL;
2356 tok2 = isl_stream_next_token_on_same_line(s);
2357 if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2358 isl_stream_push_token(s, tok2);
2359 isl_stream_push_token(s, tok);
2360 return isl_map_from_basic_map(basic_map_read_polylib(s));
2362 if (tok2) {
2363 isl_stream_error(s, tok2, "unexpected token");
2364 isl_stream_push_token(s, tok2);
2365 isl_stream_push_token(s, tok);
2366 return NULL;
2368 n = isl_int_get_si(tok->u.v);
2369 isl_token_free(tok);
2371 isl_assert(s->ctx, n >= 1, return NULL);
2373 map = isl_map_from_basic_map(basic_map_read_polylib(s));
2375 for (i = 1; map && i < n; ++i)
2376 map = isl_map_union(map,
2377 isl_map_from_basic_map(basic_map_read_polylib(s)));
2379 return map;
2382 static int optional_power(__isl_keep isl_stream *s)
2384 int pow;
2385 struct isl_token *tok;
2387 tok = isl_stream_next_token(s);
2388 if (!tok)
2389 return 1;
2390 if (tok->type != '^') {
2391 isl_stream_push_token(s, tok);
2392 return 1;
2394 isl_token_free(tok);
2395 tok = isl_stream_next_token(s);
2396 if (!tok || tok->type != ISL_TOKEN_VALUE) {
2397 isl_stream_error(s, tok, "expecting exponent");
2398 if (tok)
2399 isl_stream_push_token(s, tok);
2400 return 1;
2402 pow = isl_int_get_si(tok->u.v);
2403 isl_token_free(tok);
2404 return pow;
2407 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2408 __isl_keep isl_map *map, struct vars *v);
2410 static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2411 __isl_keep isl_map *map, struct vars *v)
2413 isl_pw_qpolynomial *pwqp;
2414 struct isl_token *tok;
2416 tok = next_token(s);
2417 if (!tok) {
2418 isl_stream_error(s, NULL, "unexpected EOF");
2419 return NULL;
2421 if (tok->type == '(') {
2422 int pow;
2424 isl_token_free(tok);
2425 pwqp = read_term(s, map, v);
2426 if (!pwqp)
2427 return NULL;
2428 if (isl_stream_eat(s, ')'))
2429 goto error;
2430 pow = optional_power(s);
2431 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2432 } else if (tok->type == ISL_TOKEN_VALUE) {
2433 struct isl_token *tok2;
2434 isl_qpolynomial *qp;
2436 tok2 = isl_stream_next_token(s);
2437 if (tok2 && tok2->type == '/') {
2438 isl_token_free(tok2);
2439 tok2 = next_token(s);
2440 if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2441 isl_stream_error(s, tok2, "expected denominator");
2442 isl_token_free(tok);
2443 isl_token_free(tok2);
2444 return NULL;
2446 qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2447 tok->u.v, tok2->u.v);
2448 isl_token_free(tok2);
2449 } else {
2450 isl_stream_push_token(s, tok2);
2451 qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2452 tok->u.v);
2454 isl_token_free(tok);
2455 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2456 } else if (tok->type == ISL_TOKEN_INFTY) {
2457 isl_qpolynomial *qp;
2458 isl_token_free(tok);
2459 qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2460 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2461 } else if (tok->type == ISL_TOKEN_NAN) {
2462 isl_qpolynomial *qp;
2463 isl_token_free(tok);
2464 qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2465 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2466 } else if (tok->type == ISL_TOKEN_IDENT) {
2467 int n = v->n;
2468 int pos = vars_pos(v, tok->u.s, -1);
2469 int pow;
2470 isl_qpolynomial *qp;
2471 if (pos < 0) {
2472 isl_token_free(tok);
2473 return NULL;
2475 if (pos >= n) {
2476 vars_drop(v, v->n - n);
2477 isl_stream_error(s, tok, "unknown identifier");
2478 isl_token_free(tok);
2479 return NULL;
2481 isl_token_free(tok);
2482 pow = optional_power(s);
2483 qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2484 pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2485 } else if (is_start_of_div(tok)) {
2486 isl_pw_aff *pwaff;
2487 int pow;
2489 isl_stream_push_token(s, tok);
2490 pwaff = accept_div(s, isl_map_get_space(map), v);
2491 pow = optional_power(s);
2492 pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2493 pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2494 } else if (tok->type == '-') {
2495 isl_token_free(tok);
2496 pwqp = read_factor(s, map, v);
2497 pwqp = isl_pw_qpolynomial_neg(pwqp);
2498 } else {
2499 isl_stream_error(s, tok, "unexpected isl_token");
2500 isl_stream_push_token(s, tok);
2501 return NULL;
2504 if (isl_stream_eat_if_available(s, '*') ||
2505 isl_stream_next_token_is(s, ISL_TOKEN_IDENT)) {
2506 isl_pw_qpolynomial *pwqp2;
2508 pwqp2 = read_factor(s, map, v);
2509 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2512 return pwqp;
2513 error:
2514 isl_pw_qpolynomial_free(pwqp);
2515 return NULL;
2518 static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2519 __isl_keep isl_map *map, struct vars *v)
2521 struct isl_token *tok;
2522 isl_pw_qpolynomial *pwqp;
2524 pwqp = read_factor(s, map, v);
2526 for (;;) {
2527 tok = next_token(s);
2528 if (!tok)
2529 return pwqp;
2531 if (tok->type == '+') {
2532 isl_pw_qpolynomial *pwqp2;
2534 isl_token_free(tok);
2535 pwqp2 = read_factor(s, map, v);
2536 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2537 } else if (tok->type == '-') {
2538 isl_pw_qpolynomial *pwqp2;
2540 isl_token_free(tok);
2541 pwqp2 = read_factor(s, map, v);
2542 pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2543 } else if (tok->type == ISL_TOKEN_VALUE &&
2544 isl_int_is_neg(tok->u.v)) {
2545 isl_pw_qpolynomial *pwqp2;
2547 isl_stream_push_token(s, tok);
2548 pwqp2 = read_factor(s, map, v);
2549 pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2550 } else {
2551 isl_stream_push_token(s, tok);
2552 break;
2556 return pwqp;
2559 static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2560 __isl_take isl_map *map, struct vars *v, int rational)
2562 struct isl_token *tok;
2564 tok = isl_stream_next_token(s);
2565 if (!tok) {
2566 isl_stream_error(s, NULL, "unexpected EOF");
2567 goto error;
2569 if (tok->type == ':' ||
2570 (tok->type == ISL_TOKEN_OR && !strcmp(tok->u.s, "|"))) {
2571 isl_token_free(tok);
2572 map = read_formula(s, v, map, rational);
2573 } else
2574 isl_stream_push_token(s, tok);
2576 return map;
2577 error:
2578 isl_map_free(map);
2579 return NULL;
2582 static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2583 __isl_take isl_map *map, struct vars *v, int n)
2585 struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2586 isl_pw_qpolynomial *pwqp;
2587 struct isl_set *set;
2589 pwqp = read_term(s, map, v);
2590 map = read_optional_formula(s, map, v, 0);
2591 set = isl_map_range(map);
2593 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2595 vars_drop(v, v->n - n);
2597 obj.v = pwqp;
2598 return obj;
2601 static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2602 __isl_take isl_set *set, struct vars *v, int n)
2604 int min, max;
2605 struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2606 isl_pw_qpolynomial *pwqp;
2607 isl_pw_qpolynomial_fold *pwf = NULL;
2608 enum isl_fold fold;
2610 max = isl_stream_eat_if_available(s, ISL_TOKEN_MAX);
2611 min = !max && isl_stream_eat_if_available(s, ISL_TOKEN_MIN);
2612 if (!min && !max)
2613 return obj_read_poly(s, set, v, n);
2614 fold = max ? isl_fold_max : isl_fold_min;
2616 if (isl_stream_eat(s, '('))
2617 goto error;
2619 pwqp = read_term(s, set, v);
2620 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
2622 while (isl_stream_eat_if_available(s, ',')) {
2623 isl_pw_qpolynomial_fold *pwf_i;
2624 pwqp = read_term(s, set, v);
2625 pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(fold, pwqp);
2626 pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2629 if (isl_stream_eat(s, ')'))
2630 goto error;
2632 set = read_optional_formula(s, set, v, 0);
2633 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2635 vars_drop(v, v->n - n);
2637 obj.v = pwf;
2638 return obj;
2639 error:
2640 isl_set_free(set);
2641 isl_pw_qpolynomial_fold_free(pwf);
2642 obj.type = isl_obj_none;
2643 return obj;
2646 static int is_rational(__isl_keep isl_stream *s)
2648 struct isl_token *tok;
2650 tok = isl_stream_next_token(s);
2651 if (!tok)
2652 return 0;
2653 if (tok->type == ISL_TOKEN_RAT && isl_stream_next_token_is(s, ':')) {
2654 isl_token_free(tok);
2655 isl_stream_eat(s, ':');
2656 return 1;
2659 isl_stream_push_token(s, tok);
2661 return 0;
2664 static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2665 __isl_take isl_map *map, struct vars *v)
2667 struct isl_token *tok;
2668 struct isl_obj obj = { isl_obj_set, NULL };
2669 int n = v->n;
2670 int rational;
2672 rational = is_rational(s);
2673 if (rational)
2674 map = isl_map_set_rational(map);
2676 if (isl_stream_next_token_is(s, ':')) {
2677 obj.type = isl_obj_set;
2678 obj.v = read_optional_formula(s, map, v, rational);
2679 return obj;
2682 if (!next_is_tuple(s))
2683 return obj_read_poly_or_fold(s, map, v, n);
2685 map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2686 if (!map)
2687 goto error;
2688 tok = isl_stream_next_token(s);
2689 if (!tok)
2690 goto error;
2691 if (tok->type == ISL_TOKEN_TO) {
2692 obj.type = isl_obj_map;
2693 isl_token_free(tok);
2694 if (!next_is_tuple(s)) {
2695 isl_set *set = isl_map_domain(map);
2696 return obj_read_poly_or_fold(s, set, v, n);
2698 map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2699 if (!map)
2700 goto error;
2701 } else {
2702 map = isl_map_domain(map);
2703 isl_stream_push_token(s, tok);
2706 map = read_optional_formula(s, map, v, rational);
2708 vars_drop(v, v->n - n);
2710 obj.v = map;
2711 return obj;
2712 error:
2713 isl_map_free(map);
2714 obj.type = isl_obj_none;
2715 return obj;
2718 static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2720 if (obj.type == isl_obj_map) {
2721 obj.v = isl_union_map_from_map(obj.v);
2722 obj.type = isl_obj_union_map;
2723 } else if (obj.type == isl_obj_set) {
2724 obj.v = isl_union_set_from_set(obj.v);
2725 obj.type = isl_obj_union_set;
2726 } else if (obj.type == isl_obj_pw_qpolynomial) {
2727 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2728 obj.type = isl_obj_union_pw_qpolynomial;
2729 } else if (obj.type == isl_obj_pw_qpolynomial_fold) {
2730 obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2731 obj.type = isl_obj_union_pw_qpolynomial_fold;
2732 } else
2733 isl_assert(ctx, 0, goto error);
2734 return obj;
2735 error:
2736 obj.type->free(obj.v);
2737 obj.type = isl_obj_none;
2738 return obj;
2741 static struct isl_obj obj_add(__isl_keep isl_stream *s,
2742 struct isl_obj obj1, struct isl_obj obj2)
2744 if (obj2.type == isl_obj_none || !obj2.v)
2745 goto error;
2746 if (obj1.type == isl_obj_set && obj2.type == isl_obj_union_set)
2747 obj1 = to_union(s->ctx, obj1);
2748 if (obj1.type == isl_obj_union_set && obj2.type == isl_obj_set)
2749 obj2 = to_union(s->ctx, obj2);
2750 if (obj1.type == isl_obj_map && obj2.type == isl_obj_union_map)
2751 obj1 = to_union(s->ctx, obj1);
2752 if (obj1.type == isl_obj_union_map && obj2.type == isl_obj_map)
2753 obj2 = to_union(s->ctx, obj2);
2754 if (obj1.type == isl_obj_pw_qpolynomial &&
2755 obj2.type == isl_obj_union_pw_qpolynomial)
2756 obj1 = to_union(s->ctx, obj1);
2757 if (obj1.type == isl_obj_union_pw_qpolynomial &&
2758 obj2.type == isl_obj_pw_qpolynomial)
2759 obj2 = to_union(s->ctx, obj2);
2760 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2761 obj2.type == isl_obj_union_pw_qpolynomial_fold)
2762 obj1 = to_union(s->ctx, obj1);
2763 if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2764 obj2.type == isl_obj_pw_qpolynomial_fold)
2765 obj2 = to_union(s->ctx, obj2);
2766 if (obj1.type != obj2.type) {
2767 isl_stream_error(s, NULL,
2768 "attempt to combine incompatible objects");
2769 goto error;
2771 if (!obj1.type->add)
2772 isl_die(s->ctx, isl_error_internal,
2773 "combination not supported on object type", goto error);
2774 if (obj1.type == isl_obj_map && !isl_map_has_equal_space(obj1.v, obj2.v)) {
2775 obj1 = to_union(s->ctx, obj1);
2776 obj2 = to_union(s->ctx, obj2);
2778 if (obj1.type == isl_obj_set && !isl_set_has_equal_space(obj1.v, obj2.v)) {
2779 obj1 = to_union(s->ctx, obj1);
2780 obj2 = to_union(s->ctx, obj2);
2782 if (obj1.type == isl_obj_pw_qpolynomial &&
2783 !isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)) {
2784 obj1 = to_union(s->ctx, obj1);
2785 obj2 = to_union(s->ctx, obj2);
2787 if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2788 !isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)) {
2789 obj1 = to_union(s->ctx, obj1);
2790 obj2 = to_union(s->ctx, obj2);
2792 obj1.v = obj1.type->add(obj1.v, obj2.v);
2793 return obj1;
2794 error:
2795 obj1.type->free(obj1.v);
2796 obj2.type->free(obj2.v);
2797 obj1.type = isl_obj_none;
2798 obj1.v = NULL;
2799 return obj1;
2802 /* Are the first two tokens on "s", "domain" (either as a string
2803 * or as an identifier) followed by ":"?
2805 static int next_is_domain_colon(__isl_keep isl_stream *s)
2807 struct isl_token *tok;
2808 char *name;
2809 int res;
2811 tok = isl_stream_next_token(s);
2812 if (!tok)
2813 return 0;
2814 if (tok->type != ISL_TOKEN_IDENT && tok->type != ISL_TOKEN_STRING) {
2815 isl_stream_push_token(s, tok);
2816 return 0;
2819 name = isl_token_get_str(s->ctx, tok);
2820 res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2821 free(name);
2823 isl_stream_push_token(s, tok);
2825 return res;
2828 /* Do the first tokens on "s" look like a schedule?
2830 * The root of a schedule is always a domain node, so the first thing
2831 * we expect in the stream is a domain key, i.e., "domain" followed
2832 * by ":". If the schedule was printed in YAML flow style, then
2833 * we additionally expect a "{" to open the outer mapping.
2835 static int next_is_schedule(__isl_keep isl_stream *s)
2837 struct isl_token *tok;
2838 int is_schedule;
2840 tok = isl_stream_next_token(s);
2841 if (!tok)
2842 return 0;
2843 if (tok->type != '{') {
2844 isl_stream_push_token(s, tok);
2845 return next_is_domain_colon(s);
2848 is_schedule = next_is_domain_colon(s);
2849 isl_stream_push_token(s, tok);
2851 return is_schedule;
2854 /* Read an isl_schedule from "s" and store it in an isl_obj.
2856 static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2858 struct isl_obj obj;
2860 obj.type = isl_obj_schedule;
2861 obj.v = isl_stream_read_schedule(s);
2863 return obj;
2866 /* Read a disjunction of object bodies from "s".
2867 * That is, read the inside of the braces, but not the braces themselves.
2868 * "v" contains a description of the identifiers parsed so far.
2869 * "map" contains information about the parameters.
2871 static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
2872 struct vars *v, __isl_keep isl_map *map)
2874 struct isl_obj obj = { isl_obj_set, NULL };
2876 if (isl_stream_next_token_is(s, '}')) {
2877 obj.type = isl_obj_union_set;
2878 obj.v = isl_union_set_empty(isl_map_get_space(map));
2879 return obj;
2882 for (;;) {
2883 struct isl_obj o;
2884 o = obj_read_body(s, isl_map_copy(map), v);
2885 if (!obj.v)
2886 obj = o;
2887 else
2888 obj = obj_add(s, obj, o);
2889 if (obj.type == isl_obj_none || !obj.v)
2890 return obj;
2891 if (!isl_stream_eat_if_available(s, ';'))
2892 break;
2893 if (isl_stream_next_token_is(s, '}'))
2894 break;
2897 return obj;
2900 static struct isl_obj obj_read(__isl_keep isl_stream *s)
2902 isl_map *map = NULL;
2903 struct isl_token *tok;
2904 struct vars *v = NULL;
2905 struct isl_obj obj = { isl_obj_set, NULL };
2907 if (next_is_schedule(s))
2908 return schedule_read(s);
2910 tok = next_token(s);
2911 if (!tok) {
2912 isl_stream_error(s, NULL, "unexpected EOF");
2913 goto error;
2915 if (tok->type == ISL_TOKEN_VALUE) {
2916 struct isl_token *tok2;
2917 struct isl_map *map;
2919 tok2 = isl_stream_next_token(s);
2920 if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2921 isl_int_is_neg(tok2->u.v)) {
2922 if (tok2)
2923 isl_stream_push_token(s, tok2);
2924 obj.type = isl_obj_val;
2925 obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2926 isl_token_free(tok);
2927 return obj;
2929 isl_stream_push_token(s, tok2);
2930 isl_stream_push_token(s, tok);
2931 map = map_read_polylib(s);
2932 if (!map)
2933 goto error;
2934 if (isl_map_may_be_set(map))
2935 obj.v = isl_map_range(map);
2936 else {
2937 obj.type = isl_obj_map;
2938 obj.v = map;
2940 return obj;
2942 v = vars_new(s->ctx);
2943 if (!v) {
2944 isl_stream_push_token(s, tok);
2945 goto error;
2947 map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2948 if (tok->type == '[') {
2949 isl_stream_push_token(s, tok);
2950 map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2951 if (!map)
2952 goto error;
2953 tok = isl_stream_next_token(s);
2954 if (!tok || tok->type != ISL_TOKEN_TO) {
2955 isl_stream_error(s, tok, "expecting '->'");
2956 if (tok)
2957 isl_stream_push_token(s, tok);
2958 goto error;
2960 isl_token_free(tok);
2961 tok = isl_stream_next_token(s);
2963 if (!tok || tok->type != '{') {
2964 isl_stream_error(s, tok, "expecting '{'");
2965 if (tok)
2966 isl_stream_push_token(s, tok);
2967 goto error;
2969 isl_token_free(tok);
2971 tok = isl_stream_next_token(s);
2972 if (!tok)
2974 else if (tok->type == ISL_TOKEN_IDENT && !strcmp(tok->u.s, "Sym")) {
2975 isl_token_free(tok);
2976 if (isl_stream_eat(s, '='))
2977 goto error;
2978 map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2979 if (!map)
2980 goto error;
2981 } else
2982 isl_stream_push_token(s, tok);
2984 obj = obj_read_disjuncts(s, v, map);
2985 if (obj.type == isl_obj_none || !obj.v)
2986 goto error;
2988 tok = isl_stream_next_token(s);
2989 if (tok && tok->type == '}') {
2990 isl_token_free(tok);
2991 } else {
2992 isl_stream_error(s, tok, "unexpected isl_token");
2993 if (tok)
2994 isl_token_free(tok);
2995 goto error;
2998 vars_free(v);
2999 isl_map_free(map);
3001 return obj;
3002 error:
3003 isl_map_free(map);
3004 obj.type->free(obj.v);
3005 if (v)
3006 vars_free(v);
3007 obj.v = NULL;
3008 return obj;
3011 struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
3013 return obj_read(s);
3016 __isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
3018 struct isl_obj obj;
3020 obj = obj_read(s);
3021 if (obj.v)
3022 isl_assert(s->ctx, obj.type == isl_obj_map ||
3023 obj.type == isl_obj_set, goto error);
3025 if (obj.type == isl_obj_set)
3026 obj.v = isl_map_from_range(obj.v);
3028 return obj.v;
3029 error:
3030 obj.type->free(obj.v);
3031 return NULL;
3034 __isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
3036 struct isl_obj obj;
3038 obj = obj_read(s);
3039 if (obj.v) {
3040 if (obj.type == isl_obj_map && isl_map_may_be_set(obj.v)) {
3041 obj.v = isl_map_range(obj.v);
3042 obj.type = isl_obj_set;
3044 isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
3047 return obj.v;
3048 error:
3049 obj.type->free(obj.v);
3050 return NULL;
3053 __isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
3055 struct isl_obj obj;
3057 obj = obj_read(s);
3058 if (obj.type == isl_obj_map) {
3059 obj.type = isl_obj_union_map;
3060 obj.v = isl_union_map_from_map(obj.v);
3062 if (obj.type == isl_obj_set) {
3063 obj.type = isl_obj_union_set;
3064 obj.v = isl_union_set_from_set(obj.v);
3066 if (obj.v && obj.type == isl_obj_union_set &&
3067 isl_union_set_is_empty(obj.v))
3068 obj.type = isl_obj_union_map;
3069 if (obj.v && obj.type != isl_obj_union_map)
3070 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
3072 return obj.v;
3073 error:
3074 obj.type->free(obj.v);
3075 return NULL;
3078 /* Extract an isl_union_set from "obj".
3079 * This only works if the object was detected as either a set
3080 * (in which case it is converted to a union set) or a union set.
3082 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
3083 struct isl_obj obj)
3085 if (obj.type == isl_obj_set) {
3086 obj.type = isl_obj_union_set;
3087 obj.v = isl_union_set_from_set(obj.v);
3089 if (obj.v)
3090 isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
3092 return obj.v;
3093 error:
3094 obj.type->free(obj.v);
3095 return NULL;
3098 /* Read an isl_union_set from "s".
3099 * First read a generic object and then try and extract
3100 * an isl_union_set from that.
3102 __isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
3104 struct isl_obj obj;
3106 obj = obj_read(s);
3107 return extract_union_set(s->ctx, obj);
3110 static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
3112 struct isl_obj obj;
3113 struct isl_map *map;
3114 struct isl_basic_map *bmap;
3116 obj = obj_read(s);
3117 if (obj.v && (obj.type != isl_obj_map && obj.type != isl_obj_set))
3118 isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
3119 goto error);
3120 map = obj.v;
3121 if (!map)
3122 return NULL;
3124 if (map->n > 1)
3125 isl_die(s->ctx, isl_error_invalid,
3126 "set or map description involves "
3127 "more than one disjunct", goto error);
3129 if (map->n == 0)
3130 bmap = isl_basic_map_empty(isl_map_get_space(map));
3131 else
3132 bmap = isl_basic_map_copy(map->p[0]);
3134 isl_map_free(map);
3136 return bmap;
3137 error:
3138 obj.type->free(obj.v);
3139 return NULL;
3142 static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
3144 isl_basic_map *bmap;
3145 bmap = basic_map_read(s);
3146 if (!bmap)
3147 return NULL;
3148 if (!isl_basic_map_may_be_set(bmap))
3149 isl_die(s->ctx, isl_error_invalid,
3150 "input is not a set", goto error);
3151 return isl_basic_map_range(bmap);
3152 error:
3153 isl_basic_map_free(bmap);
3154 return NULL;
3157 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
3158 FILE *input)
3160 struct isl_basic_map *bmap;
3161 isl_stream *s = isl_stream_new_file(ctx, input);
3162 if (!s)
3163 return NULL;
3164 bmap = basic_map_read(s);
3165 isl_stream_free(s);
3166 return bmap;
3169 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
3170 FILE *input)
3172 isl_basic_set *bset;
3173 isl_stream *s = isl_stream_new_file(ctx, input);
3174 if (!s)
3175 return NULL;
3176 bset = basic_set_read(s);
3177 isl_stream_free(s);
3178 return bset;
3181 __isl_give isl_basic_map *isl_basic_map_read_from_str(isl_ctx *ctx,
3182 const char *str)
3184 struct isl_basic_map *bmap;
3185 isl_stream *s = isl_stream_new_str(ctx, str);
3186 if (!s)
3187 return NULL;
3188 bmap = basic_map_read(s);
3189 isl_stream_free(s);
3190 return bmap;
3193 __isl_give isl_basic_set *isl_basic_set_read_from_str(isl_ctx *ctx,
3194 const char *str)
3196 isl_basic_set *bset;
3197 isl_stream *s = isl_stream_new_str(ctx, str);
3198 if (!s)
3199 return NULL;
3200 bset = basic_set_read(s);
3201 isl_stream_free(s);
3202 return bset;
3205 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
3206 FILE *input)
3208 struct isl_map *map;
3209 isl_stream *s = isl_stream_new_file(ctx, input);
3210 if (!s)
3211 return NULL;
3212 map = isl_stream_read_map(s);
3213 isl_stream_free(s);
3214 return map;
3217 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
3218 const char *str)
3220 struct isl_map *map;
3221 isl_stream *s = isl_stream_new_str(ctx, str);
3222 if (!s)
3223 return NULL;
3224 map = isl_stream_read_map(s);
3225 isl_stream_free(s);
3226 return map;
3229 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
3230 FILE *input)
3232 isl_set *set;
3233 isl_stream *s = isl_stream_new_file(ctx, input);
3234 if (!s)
3235 return NULL;
3236 set = isl_stream_read_set(s);
3237 isl_stream_free(s);
3238 return set;
3241 __isl_give isl_set *isl_set_read_from_str(isl_ctx *ctx, const char *str)
3243 isl_set *set;
3244 isl_stream *s = isl_stream_new_str(ctx, str);
3245 if (!s)
3246 return NULL;
3247 set = isl_stream_read_set(s);
3248 isl_stream_free(s);
3249 return set;
3252 __isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
3253 FILE *input)
3255 isl_union_map *umap;
3256 isl_stream *s = isl_stream_new_file(ctx, input);
3257 if (!s)
3258 return NULL;
3259 umap = isl_stream_read_union_map(s);
3260 isl_stream_free(s);
3261 return umap;
3264 __isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
3265 const char *str)
3267 isl_union_map *umap;
3268 isl_stream *s = isl_stream_new_str(ctx, str);
3269 if (!s)
3270 return NULL;
3271 umap = isl_stream_read_union_map(s);
3272 isl_stream_free(s);
3273 return umap;
3276 __isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
3277 FILE *input)
3279 isl_union_set *uset;
3280 isl_stream *s = isl_stream_new_file(ctx, input);
3281 if (!s)
3282 return NULL;
3283 uset = isl_stream_read_union_set(s);
3284 isl_stream_free(s);
3285 return uset;
3288 __isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3289 const char *str)
3291 isl_union_set *uset;
3292 isl_stream *s = isl_stream_new_str(ctx, str);
3293 if (!s)
3294 return NULL;
3295 uset = isl_stream_read_union_set(s);
3296 isl_stream_free(s);
3297 return uset;
3300 static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3302 struct isl_vec *vec = NULL;
3303 struct isl_token *tok;
3304 unsigned size;
3305 int j;
3307 tok = isl_stream_next_token(s);
3308 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3309 isl_stream_error(s, tok, "expecting vector length");
3310 goto error;
3313 size = isl_int_get_si(tok->u.v);
3314 isl_token_free(tok);
3316 vec = isl_vec_alloc(s->ctx, size);
3318 for (j = 0; j < size; ++j) {
3319 tok = isl_stream_next_token(s);
3320 if (!tok || tok->type != ISL_TOKEN_VALUE) {
3321 isl_stream_error(s, tok, "expecting constant value");
3322 goto error;
3324 isl_int_set(vec->el[j], tok->u.v);
3325 isl_token_free(tok);
3328 return vec;
3329 error:
3330 isl_token_free(tok);
3331 isl_vec_free(vec);
3332 return NULL;
3335 static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3337 return isl_vec_read_polylib(s);
3340 __isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3342 isl_vec *v;
3343 isl_stream *s = isl_stream_new_file(ctx, input);
3344 if (!s)
3345 return NULL;
3346 v = vec_read(s);
3347 isl_stream_free(s);
3348 return v;
3351 __isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3352 __isl_keep isl_stream *s)
3354 struct isl_obj obj;
3356 obj = obj_read(s);
3357 if (obj.v)
3358 isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3359 goto error);
3361 return obj.v;
3362 error:
3363 obj.type->free(obj.v);
3364 return NULL;
3367 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3368 const char *str)
3370 isl_pw_qpolynomial *pwqp;
3371 isl_stream *s = isl_stream_new_str(ctx, str);
3372 if (!s)
3373 return NULL;
3374 pwqp = isl_stream_read_pw_qpolynomial(s);
3375 isl_stream_free(s);
3376 return pwqp;
3379 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3380 FILE *input)
3382 isl_pw_qpolynomial *pwqp;
3383 isl_stream *s = isl_stream_new_file(ctx, input);
3384 if (!s)
3385 return NULL;
3386 pwqp = isl_stream_read_pw_qpolynomial(s);
3387 isl_stream_free(s);
3388 return pwqp;
3391 /* Read an isl_pw_qpolynomial_fold from "s".
3392 * First read a generic object and
3393 * then check that it is an isl_pw_qpolynomial_fold.
3395 __isl_give isl_pw_qpolynomial_fold *isl_stream_read_pw_qpolynomial_fold(
3396 __isl_keep isl_stream *s)
3398 struct isl_obj obj;
3400 obj = obj_read(s);
3401 if (obj.v && obj.type != isl_obj_pw_qpolynomial_fold)
3402 isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
3404 return obj.v;
3405 error:
3406 obj.type->free(obj.v);
3407 return NULL;
3410 /* Read an isl_pw_qpolynomial_fold from "str".
3412 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_read_from_str(
3413 isl_ctx *ctx, const char *str)
3415 isl_pw_qpolynomial_fold *pwqp;
3416 isl_stream *s;
3418 s = isl_stream_new_str(ctx, str);
3419 if (!s)
3420 return NULL;
3421 pwqp = isl_stream_read_pw_qpolynomial_fold(s);
3422 isl_stream_free(s);
3424 return pwqp;
3427 /* Is the next token an identifier not in "v"?
3429 static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3431 int n = v->n;
3432 int fresh;
3433 struct isl_token *tok;
3435 tok = isl_stream_next_token(s);
3436 if (!tok)
3437 return 0;
3438 fresh = tok->type == ISL_TOKEN_IDENT && vars_pos(v, tok->u.s, -1) >= n;
3439 isl_stream_push_token(s, tok);
3441 vars_drop(v, v->n - n);
3443 return fresh;
3446 /* First read the domain of the affine expression, which may be
3447 * a parameter space or a set.
3448 * The tricky part is that we don't know if the domain is a set or not,
3449 * so when we are trying to read the domain, we may actually be reading
3450 * the affine expression itself (defined on a parameter domains)
3451 * If the tuple we are reading is named, we assume it's the domain.
3452 * Also, if inside the tuple, the first thing we find is a nested tuple
3453 * or a new identifier, we again assume it's the domain.
3454 * Finally, if the tuple is empty, then it must be the domain
3455 * since it does not contain an affine expression.
3456 * Otherwise, we assume we are reading an affine expression.
3458 static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3459 __isl_take isl_set *dom, struct vars *v)
3461 struct isl_token *tok, *tok2;
3462 int is_empty;
3464 tok = isl_stream_next_token(s);
3465 if (tok && (tok->type == ISL_TOKEN_IDENT || tok->is_keyword)) {
3466 isl_stream_push_token(s, tok);
3467 return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3469 if (!tok || tok->type != '[') {
3470 isl_stream_error(s, tok, "expecting '['");
3471 goto error;
3473 tok2 = isl_stream_next_token(s);
3474 is_empty = tok2 && tok2->type == ']';
3475 if (tok2)
3476 isl_stream_push_token(s, tok2);
3477 if (is_empty || next_is_tuple(s) || next_is_fresh_ident(s, v)) {
3478 isl_stream_push_token(s, tok);
3479 dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3480 } else
3481 isl_stream_push_token(s, tok);
3483 return dom;
3484 error:
3485 if (tok)
3486 isl_stream_push_token(s, tok);
3487 isl_set_free(dom);
3488 return NULL;
3491 /* Read an affine expression from "s".
3493 __isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3495 isl_aff *aff;
3496 isl_multi_aff *ma;
3497 isl_size dim;
3499 ma = isl_stream_read_multi_aff(s);
3500 dim = isl_multi_aff_dim(ma, isl_dim_out);
3501 if (dim < 0)
3502 goto error;
3503 if (dim != 1)
3504 isl_die(s->ctx, isl_error_invalid,
3505 "expecting single affine expression",
3506 goto error);
3508 aff = isl_multi_aff_get_aff(ma, 0);
3509 isl_multi_aff_free(ma);
3510 return aff;
3511 error:
3512 isl_multi_aff_free(ma);
3513 return NULL;
3516 /* Read a piecewise affine expression from "s" with domain (space) "dom".
3518 static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3519 __isl_take isl_set *dom, struct vars *v)
3521 isl_pw_aff *pwaff = NULL;
3523 if (!isl_set_is_params(dom) && isl_stream_eat(s, ISL_TOKEN_TO))
3524 goto error;
3526 if (isl_stream_eat(s, '['))
3527 goto error;
3529 pwaff = accept_affine(s, isl_set_get_space(dom), v);
3531 if (isl_stream_eat(s, ']'))
3532 goto error;
3534 dom = read_optional_formula(s, dom, v, 0);
3535 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3537 return pwaff;
3538 error:
3539 isl_set_free(dom);
3540 isl_pw_aff_free(pwaff);
3541 return NULL;
3544 __isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3546 struct vars *v;
3547 isl_set *dom = NULL;
3548 isl_set *aff_dom;
3549 isl_pw_aff *pa = NULL;
3550 int n;
3552 v = vars_new(s->ctx);
3553 if (!v)
3554 return NULL;
3556 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3557 if (next_is_tuple(s)) {
3558 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3559 if (isl_stream_eat(s, ISL_TOKEN_TO))
3560 goto error;
3562 if (isl_stream_eat(s, '{'))
3563 goto error;
3565 n = v->n;
3566 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3567 pa = read_pw_aff_with_dom(s, aff_dom, v);
3568 vars_drop(v, v->n - n);
3570 while (isl_stream_eat_if_available(s, ';')) {
3571 isl_pw_aff *pa_i;
3573 n = v->n;
3574 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3575 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3576 vars_drop(v, v->n - n);
3578 pa = isl_pw_aff_union_add(pa, pa_i);
3581 if (isl_stream_eat(s, '}'))
3582 goto error;
3584 vars_free(v);
3585 isl_set_free(dom);
3586 return pa;
3587 error:
3588 vars_free(v);
3589 isl_set_free(dom);
3590 isl_pw_aff_free(pa);
3591 return NULL;
3594 __isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3596 isl_aff *aff;
3597 isl_stream *s = isl_stream_new_str(ctx, str);
3598 if (!s)
3599 return NULL;
3600 aff = isl_stream_read_aff(s);
3601 isl_stream_free(s);
3602 return aff;
3605 __isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3607 isl_pw_aff *pa;
3608 isl_stream *s = isl_stream_new_str(ctx, str);
3609 if (!s)
3610 return NULL;
3611 pa = isl_stream_read_pw_aff(s);
3612 isl_stream_free(s);
3613 return pa;
3616 /* Extract an isl_multi_pw_aff with domain space "dom_space"
3617 * from a tuple "tuple" read by read_tuple.
3619 * Note that the function read_tuple accepts tuples where some output or
3620 * set dimensions are defined in terms of other output or set dimensions
3621 * since this function is also used to read maps. As a special case,
3622 * read_tuple also accept dimensions that are defined in terms of themselves
3623 * (i.e., that are not defined).
3624 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3625 * that the definitions of the output/set dimensions do not involve any
3626 * output/set dimensions.
3627 * Finally, drop the output dimensions from the domain of the result
3628 * of read_tuple (which is of the form [input, output] -> [output],
3629 * with anonymous domain) and reset the space.
3631 static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3632 __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3634 int i;
3635 isl_size dim, n;
3636 isl_space *space;
3637 isl_multi_pw_aff *mpa;
3639 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3640 dim = isl_space_dim(dom_space, isl_dim_all);
3641 if (n < 0 || dim < 0)
3642 dom_space = isl_space_free(dom_space);
3643 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3644 space = isl_space_align_params(space, isl_space_copy(dom_space));
3645 if (!isl_space_is_params(dom_space))
3646 space = isl_space_map_from_domain_and_range(
3647 isl_space_copy(dom_space), space);
3648 isl_space_free(dom_space);
3649 mpa = isl_multi_pw_aff_alloc(space);
3651 for (i = 0; i < n; ++i) {
3652 isl_pw_aff *pa;
3653 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3654 if (!pa)
3655 return isl_multi_pw_aff_free(mpa);
3656 if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3657 isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3658 isl_pw_aff_free(pa);
3659 isl_die(ctx, isl_error_invalid,
3660 "not an affine expression",
3661 return isl_multi_pw_aff_free(mpa));
3663 pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3664 space = isl_multi_pw_aff_get_domain_space(mpa);
3665 pa = isl_pw_aff_reset_domain_space(pa, space);
3666 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3669 return mpa;
3672 /* Read a tuple of affine expressions, together with optional constraints
3673 * on the domain from "s". "dom" represents the initial constraints
3674 * on the domain.
3676 * The isl_multi_aff may live in either a set or a map space.
3677 * First read the first tuple and check if it is followed by a "->".
3678 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3679 * read in the next tuple. This tuple (or the first tuple if it was
3680 * not followed by a "->") is then converted into an isl_multi_pw_aff
3681 * through a call to extract_mpa_from_tuple.
3682 * The result is converted to an isl_pw_multi_aff and
3683 * its domain is intersected with the domain.
3685 * Note that the last tuple may introduce new identifiers,
3686 * but these cannot be referenced in the description of the domain.
3688 static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3689 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3691 isl_multi_pw_aff *tuple;
3692 isl_multi_pw_aff *mpa;
3693 isl_pw_multi_aff *pma;
3694 int n = v->n;
3695 int n_dom;
3697 n_dom = v->n;
3698 tuple = read_tuple(s, v, 0, 0);
3699 if (!tuple)
3700 goto error;
3701 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3702 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3703 dom = isl_map_domain(map);
3704 n_dom = v->n;
3705 tuple = read_tuple(s, v, 0, 0);
3706 if (!tuple)
3707 goto error;
3709 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3710 isl_multi_pw_aff_free(tuple);
3711 if (!mpa)
3712 dom = isl_set_free(dom);
3714 vars_drop(v, v->n - n_dom);
3715 dom = read_optional_formula(s, dom, v, 0);
3717 vars_drop(v, v->n - n);
3719 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3720 pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3722 return pma;
3723 error:
3724 isl_set_free(dom);
3725 return NULL;
3728 /* Read an isl_union_pw_multi_aff from "s".
3730 * In particular, first read the parameters and then read a sequence
3731 * of zero or more tuples of affine expressions with optional conditions and
3732 * add them up.
3734 __isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3735 __isl_keep isl_stream *s)
3737 struct vars *v;
3738 isl_set *dom;
3739 isl_union_pw_multi_aff *upma = NULL;
3741 v = vars_new(s->ctx);
3742 if (!v)
3743 return NULL;
3745 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3746 if (next_is_tuple(s)) {
3747 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3748 if (isl_stream_eat(s, ISL_TOKEN_TO))
3749 goto error;
3751 if (isl_stream_eat(s, '{'))
3752 goto error;
3754 upma = isl_union_pw_multi_aff_empty(isl_set_get_space(dom));
3756 do {
3757 isl_pw_multi_aff *pma;
3758 isl_union_pw_multi_aff *upma2;
3760 if (isl_stream_next_token_is(s, '}'))
3761 break;
3763 pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3764 upma2 = isl_union_pw_multi_aff_from_pw_multi_aff(pma);
3765 upma = isl_union_pw_multi_aff_union_add(upma, upma2);
3766 if (!upma)
3767 goto error;
3768 } while (isl_stream_eat_if_available(s, ';'));
3770 if (isl_stream_eat(s, '}'))
3771 goto error;
3773 isl_set_free(dom);
3774 vars_free(v);
3775 return upma;
3776 error:
3777 isl_union_pw_multi_aff_free(upma);
3778 isl_set_free(dom);
3779 vars_free(v);
3780 return NULL;
3783 /* Read an isl_pw_multi_aff from "s".
3785 * Read a more generic isl_union_pw_multi_aff first and
3786 * then check that the result lives in a single space.
3788 __isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3789 __isl_keep isl_stream *s)
3791 isl_bool single_space;
3792 isl_union_pw_multi_aff *upma;
3794 upma = isl_stream_read_union_pw_multi_aff(s);
3795 single_space = isl_union_pw_multi_aff_isa_pw_multi_aff(upma);
3796 if (single_space < 0)
3797 upma = isl_union_pw_multi_aff_free(upma);
3798 else if (!single_space)
3799 isl_die(s->ctx, isl_error_invalid,
3800 "expecting expression in single space",
3801 upma = isl_union_pw_multi_aff_free(upma));
3802 return isl_union_pw_multi_aff_as_pw_multi_aff(upma);
3805 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3806 const char *str)
3808 isl_pw_multi_aff *pma;
3809 isl_stream *s = isl_stream_new_str(ctx, str);
3810 if (!s)
3811 return NULL;
3812 pma = isl_stream_read_pw_multi_aff(s);
3813 isl_stream_free(s);
3814 return pma;
3817 /* Read an isl_union_pw_multi_aff from "str".
3819 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3820 isl_ctx *ctx, const char *str)
3822 isl_union_pw_multi_aff *upma;
3823 isl_stream *s = isl_stream_new_str(ctx, str);
3824 if (!s)
3825 return NULL;
3826 upma = isl_stream_read_union_pw_multi_aff(s);
3827 isl_stream_free(s);
3828 return upma;
3831 /* Assuming "pa" represents a single affine expression defined on a universe
3832 * domain, extract this affine expression.
3834 static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3836 isl_aff *aff;
3838 if (!pa)
3839 return NULL;
3840 if (pa->n != 1)
3841 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3842 "expecting single affine expression",
3843 goto error);
3844 if (!isl_set_plain_is_universe(pa->p[0].set))
3845 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3846 "expecting universe domain",
3847 goto error);
3849 aff = isl_aff_copy(pa->p[0].aff);
3850 isl_pw_aff_free(pa);
3851 return aff;
3852 error:
3853 isl_pw_aff_free(pa);
3854 return NULL;
3857 #undef BASE
3858 #define BASE val
3860 #include <isl_multi_read_no_explicit_domain_templ.c>
3862 #undef BASE
3863 #define BASE id
3865 #include <isl_multi_read_no_explicit_domain_templ.c>
3867 /* Read a multi-affine expression from "s".
3868 * If the multi-affine expression has a domain, then the tuple
3869 * representing this domain cannot involve any affine expressions.
3870 * The tuple representing the actual expressions needs to consist
3871 * of only affine expressions. Moreover, these expressions can
3872 * only depend on parameters and input dimensions and not on other
3873 * output dimensions.
3875 __isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3877 struct vars *v;
3878 isl_set *dom = NULL;
3879 isl_multi_pw_aff *tuple = NULL;
3880 int i;
3881 isl_size dim, n;
3882 isl_space *space, *dom_space;
3883 isl_multi_aff *ma = NULL;
3885 v = vars_new(s->ctx);
3886 if (!v)
3887 return NULL;
3889 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3890 if (next_is_tuple(s)) {
3891 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3892 if (isl_stream_eat(s, ISL_TOKEN_TO))
3893 goto error;
3895 if (!isl_set_plain_is_universe(dom))
3896 isl_die(s->ctx, isl_error_invalid,
3897 "expecting universe parameter domain", goto error);
3898 if (isl_stream_eat(s, '{'))
3899 goto error;
3901 tuple = read_tuple(s, v, 0, 0);
3902 if (!tuple)
3903 goto error;
3904 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3905 isl_set *set;
3906 isl_space *space;
3907 isl_bool has_expr;
3909 has_expr = tuple_has_expr(tuple);
3910 if (has_expr < 0)
3911 goto error;
3912 if (has_expr)
3913 isl_die(s->ctx, isl_error_invalid,
3914 "expecting universe domain", goto error);
3915 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3916 set = isl_set_universe(space);
3917 dom = isl_set_intersect_params(set, dom);
3918 isl_multi_pw_aff_free(tuple);
3919 tuple = read_tuple(s, v, 0, 0);
3920 if (!tuple)
3921 goto error;
3924 if (isl_stream_eat(s, '}'))
3925 goto error;
3927 n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3928 dim = isl_set_dim(dom, isl_dim_all);
3929 if (n < 0 || dim < 0)
3930 goto error;
3931 dom_space = isl_set_get_space(dom);
3932 space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3933 space = isl_space_align_params(space, isl_space_copy(dom_space));
3934 if (!isl_space_is_params(dom_space))
3935 space = isl_space_map_from_domain_and_range(
3936 isl_space_copy(dom_space), space);
3937 isl_space_free(dom_space);
3938 ma = isl_multi_aff_alloc(space);
3940 for (i = 0; i < n; ++i) {
3941 isl_pw_aff *pa;
3942 isl_aff *aff;
3943 pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3944 aff = aff_from_pw_aff(pa);
3945 if (!aff)
3946 goto error;
3947 if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3948 isl_aff_free(aff);
3949 isl_die(s->ctx, isl_error_invalid,
3950 "not an affine expression", goto error);
3952 aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3953 space = isl_multi_aff_get_domain_space(ma);
3954 aff = isl_aff_reset_domain_space(aff, space);
3955 ma = isl_multi_aff_set_aff(ma, i, aff);
3958 isl_multi_pw_aff_free(tuple);
3959 vars_free(v);
3960 isl_set_free(dom);
3961 return ma;
3962 error:
3963 isl_multi_pw_aff_free(tuple);
3964 vars_free(v);
3965 isl_set_free(dom);
3966 isl_multi_aff_free(ma);
3967 return NULL;
3970 __isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3971 const char *str)
3973 isl_multi_aff *maff;
3974 isl_stream *s = isl_stream_new_str(ctx, str);
3975 if (!s)
3976 return NULL;
3977 maff = isl_stream_read_multi_aff(s);
3978 isl_stream_free(s);
3979 return maff;
3982 /* Read an isl_multi_pw_aff from "s".
3984 * The input format is similar to that of map, except that any conditions
3985 * on the domains should be specified inside the tuple since each
3986 * piecewise affine expression may have a different domain.
3987 * However, additional, shared conditions can also be specified.
3988 * This is especially useful for setting the explicit domain
3989 * of a zero-dimensional isl_multi_pw_aff.
3991 * Since we do not know in advance if the isl_multi_pw_aff lives
3992 * in a set or a map space, we first read the first tuple and check
3993 * if it is followed by a "->". If so, we convert the tuple into
3994 * the domain of the isl_multi_pw_aff and read in the next tuple.
3995 * This tuple (or the first tuple if it was not followed by a "->")
3996 * is then converted into the isl_multi_pw_aff through a call
3997 * to extract_mpa_from_tuple and the domain of the result
3998 * is intersected with the domain.
4000 * Note that the last tuple may introduce new identifiers,
4001 * but these cannot be referenced in the description of the domain.
4003 __isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
4004 __isl_keep isl_stream *s)
4006 int n_dom;
4007 struct vars *v;
4008 isl_set *dom = NULL;
4009 isl_multi_pw_aff *tuple = NULL;
4010 isl_multi_pw_aff *mpa = NULL;
4012 v = vars_new(s->ctx);
4013 if (!v)
4014 return NULL;
4016 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4017 if (next_is_tuple(s)) {
4018 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4019 if (isl_stream_eat(s, ISL_TOKEN_TO))
4020 goto error;
4022 if (isl_stream_eat(s, '{'))
4023 goto error;
4025 n_dom = v->n;
4026 tuple = read_tuple(s, v, 0, 0);
4027 if (!tuple)
4028 goto error;
4029 if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
4030 isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
4031 dom = isl_map_domain(map);
4032 n_dom = v->n;
4033 tuple = read_tuple(s, v, 0, 0);
4034 if (!tuple)
4035 goto error;
4038 vars_drop(v, v->n - n_dom);
4039 if (isl_stream_eat_if_available(s, ':'))
4040 dom = read_formula(s, v, dom, 0);
4042 if (isl_stream_eat(s, '}'))
4043 goto error;
4045 mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
4047 isl_multi_pw_aff_free(tuple);
4048 vars_free(v);
4049 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
4050 return mpa;
4051 error:
4052 isl_multi_pw_aff_free(tuple);
4053 vars_free(v);
4054 isl_set_free(dom);
4055 isl_multi_pw_aff_free(mpa);
4056 return NULL;
4059 /* Read an isl_multi_pw_aff from "str".
4061 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
4062 const char *str)
4064 isl_multi_pw_aff *mpa;
4065 isl_stream *s = isl_stream_new_str(ctx, str);
4066 if (!s)
4067 return NULL;
4068 mpa = isl_stream_read_multi_pw_aff(s);
4069 isl_stream_free(s);
4070 return mpa;
4073 /* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
4075 static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
4076 __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
4078 isl_pw_aff *pa;
4079 isl_union_pw_aff *upa = NULL;
4080 isl_set *aff_dom;
4081 int n;
4083 n = v->n;
4084 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
4085 pa = read_pw_aff_with_dom(s, aff_dom, v);
4086 vars_drop(v, v->n - n);
4088 upa = isl_union_pw_aff_from_pw_aff(pa);
4090 while (isl_stream_eat_if_available(s, ';')) {
4091 isl_pw_aff *pa_i;
4092 isl_union_pw_aff *upa_i;
4094 n = v->n;
4095 aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
4096 pa_i = read_pw_aff_with_dom(s, aff_dom, v);
4097 vars_drop(v, v->n - n);
4099 upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
4100 upa = isl_union_pw_aff_union_add(upa, upa_i);
4103 isl_set_free(dom);
4104 return upa;
4107 /* Read an isl_union_pw_aff from "s".
4109 * First check if there are any paramters, then read in the opening brace
4110 * and use read_union_pw_aff_with_dom to read in the body of
4111 * the isl_union_pw_aff. Finally, read the closing brace.
4113 __isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
4114 __isl_keep isl_stream *s)
4116 struct vars *v;
4117 isl_set *dom;
4118 isl_union_pw_aff *upa = NULL;
4120 v = vars_new(s->ctx);
4121 if (!v)
4122 return NULL;
4124 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4125 if (next_is_tuple(s)) {
4126 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4127 if (isl_stream_eat(s, ISL_TOKEN_TO))
4128 goto error;
4130 if (isl_stream_eat(s, '{'))
4131 goto error;
4133 upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
4135 if (isl_stream_eat(s, '}'))
4136 goto error;
4138 vars_free(v);
4139 isl_set_free(dom);
4140 return upa;
4141 error:
4142 vars_free(v);
4143 isl_set_free(dom);
4144 isl_union_pw_aff_free(upa);
4145 return NULL;
4148 /* Read an isl_union_pw_aff from "str".
4150 __isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
4151 const char *str)
4153 isl_union_pw_aff *upa;
4154 isl_stream *s = isl_stream_new_str(ctx, str);
4155 if (!s)
4156 return NULL;
4157 upa = isl_stream_read_union_pw_aff(s);
4158 isl_stream_free(s);
4159 return upa;
4162 /* This function is called for each element in a tuple inside
4163 * isl_stream_read_multi_union_pw_aff.
4165 * Read a '{', the union piecewise affine expression body and a '}' and
4166 * add the isl_union_pw_aff to *list.
4168 static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
4169 struct vars *v, __isl_take isl_space *space, int rational, void *user)
4171 isl_set *dom;
4172 isl_union_pw_aff *upa;
4173 isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
4175 dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
4176 if (isl_stream_eat(s, '{'))
4177 goto error;
4178 upa = read_union_pw_aff_with_dom(s, dom, v);
4179 *list = isl_union_pw_aff_list_add(*list, upa);
4180 if (isl_stream_eat(s, '}'))
4181 return isl_space_free(space);
4182 if (!*list)
4183 return isl_space_free(space);
4184 return space;
4185 error:
4186 isl_set_free(dom);
4187 return isl_space_free(space);
4190 /* Do the next tokens in "s" correspond to an empty tuple?
4191 * In particular, does the stream start with a '[', followed by a ']',
4192 * not followed by a "->"?
4194 static int next_is_empty_tuple(__isl_keep isl_stream *s)
4196 struct isl_token *tok, *tok2, *tok3;
4197 int is_empty_tuple = 0;
4199 tok = isl_stream_next_token(s);
4200 if (!tok)
4201 return 0;
4202 if (tok->type != '[') {
4203 isl_stream_push_token(s, tok);
4204 return 0;
4207 tok2 = isl_stream_next_token(s);
4208 if (tok2 && tok2->type == ']') {
4209 tok3 = isl_stream_next_token(s);
4210 is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
4211 if (tok3)
4212 isl_stream_push_token(s, tok3);
4214 if (tok2)
4215 isl_stream_push_token(s, tok2);
4216 isl_stream_push_token(s, tok);
4218 return is_empty_tuple;
4221 /* Do the next tokens in "s" correspond to a tuple of parameters?
4222 * In particular, does the stream start with a '[' that is not
4223 * followed by a '{' or a nested tuple?
4225 static int next_is_param_tuple(__isl_keep isl_stream *s)
4227 struct isl_token *tok, *tok2;
4228 int is_tuple;
4230 tok = isl_stream_next_token(s);
4231 if (!tok)
4232 return 0;
4233 if (tok->type != '[' || next_is_tuple(s)) {
4234 isl_stream_push_token(s, tok);
4235 return 0;
4238 tok2 = isl_stream_next_token(s);
4239 is_tuple = tok2 && tok2->type != '{';
4240 if (tok2)
4241 isl_stream_push_token(s, tok2);
4242 isl_stream_push_token(s, tok);
4244 return is_tuple;
4247 /* Read the core of a body of an isl_multi_union_pw_aff from "s",
4248 * i.e., everything except the parameter specification and
4249 * without shared domain constraints.
4250 * "v" contains a description of the identifiers parsed so far.
4251 * The parameters, if any, are specified by "space".
4253 * The body is of the form
4255 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4257 * Read the tuple, collecting the individual isl_union_pw_aff
4258 * elements in a list and construct the result from the tuple space and
4259 * the list.
4261 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
4262 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4264 isl_union_pw_aff_list *list;
4265 isl_multi_union_pw_aff *mupa;
4267 list = isl_union_pw_aff_list_alloc(s->ctx, 0);
4268 space = read_tuple_space(s, v, space, 1, 0,
4269 &read_union_pw_aff_el, &list);
4270 mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
4272 return mupa;
4275 /* Read the body of an isl_union_set from "s",
4276 * i.e., everything except the parameter specification.
4277 * "v" contains a description of the identifiers parsed so far.
4278 * The parameters, if any, are specified by "space".
4280 * First read a generic disjunction of object bodies and then try and extract
4281 * an isl_union_set from that.
4283 static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
4284 struct vars *v, __isl_take isl_space *space)
4286 struct isl_obj obj = { isl_obj_set, NULL };
4287 isl_map *map;
4289 map = isl_set_universe(space);
4290 if (isl_stream_eat(s, '{') < 0)
4291 goto error;
4292 obj = obj_read_disjuncts(s, v, map);
4293 if (isl_stream_eat(s, '}') < 0)
4294 goto error;
4295 isl_map_free(map);
4297 return extract_union_set(s->ctx, obj);
4298 error:
4299 obj.type->free(obj.v);
4300 isl_map_free(map);
4301 return NULL;
4304 /* Read the body of an isl_multi_union_pw_aff from "s",
4305 * i.e., everything except the parameter specification.
4306 * "v" contains a description of the identifiers parsed so far.
4307 * The parameters, if any, are specified by "space".
4309 * In particular, handle the special case with shared domain constraints.
4310 * These are specified as
4312 * ([...] : ...)
4314 * and are especially useful for setting the explicit domain
4315 * of a zero-dimensional isl_multi_union_pw_aff.
4316 * The core isl_multi_union_pw_aff body ([...]) is read by
4317 * read_multi_union_pw_aff_body_core.
4319 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
4320 __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4322 isl_multi_union_pw_aff *mupa;
4324 if (!isl_stream_next_token_is(s, '('))
4325 return read_multi_union_pw_aff_body_core(s, v, space);
4327 if (isl_stream_eat(s, '(') < 0)
4328 goto error;
4329 mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
4330 if (isl_stream_eat_if_available(s, ':')) {
4331 isl_union_set *dom;
4333 dom = read_union_set_body(s, v, space);
4334 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4335 } else {
4336 isl_space_free(space);
4338 if (isl_stream_eat(s, ')') < 0)
4339 return isl_multi_union_pw_aff_free(mupa);
4341 return mupa;
4342 error:
4343 isl_space_free(space);
4344 return NULL;
4347 /* Read an isl_multi_union_pw_aff from "s".
4349 * The input has the form
4351 * [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4353 * or
4355 * [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4357 * Additionally, a shared domain may be specified as
4359 * ([..] : ...)
4361 * or
4363 * [..] -> ([..] : ...)
4365 * The first case is handled by the caller, the second case
4366 * is handled by read_multi_union_pw_aff_body.
4368 * We first check for the special case of an empty tuple "[]".
4369 * Then we check if there are any parameters.
4370 * Finally, read the tuple and construct the result.
4372 static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
4373 __isl_keep isl_stream *s)
4375 struct vars *v;
4376 isl_set *dom = NULL;
4377 isl_space *space;
4378 isl_multi_union_pw_aff *mupa = NULL;
4380 if (next_is_empty_tuple(s)) {
4381 if (isl_stream_eat(s, '['))
4382 return NULL;
4383 if (isl_stream_eat(s, ']'))
4384 return NULL;
4385 space = isl_space_set_alloc(s->ctx, 0, 0);
4386 return isl_multi_union_pw_aff_zero(space);
4389 v = vars_new(s->ctx);
4390 if (!v)
4391 return NULL;
4393 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4394 if (next_is_param_tuple(s)) {
4395 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4396 if (isl_stream_eat(s, ISL_TOKEN_TO))
4397 goto error;
4399 space = isl_set_get_space(dom);
4400 isl_set_free(dom);
4401 mupa = read_multi_union_pw_aff_body(s, v, space);
4403 vars_free(v);
4405 return mupa;
4406 error:
4407 vars_free(v);
4408 isl_set_free(dom);
4409 isl_multi_union_pw_aff_free(mupa);
4410 return NULL;
4413 /* Read an isl_multi_union_pw_aff from "s".
4415 * In particular, handle the special case with shared domain constraints.
4416 * These are specified as
4418 * ([...] : ...)
4420 * and are especially useful for setting the explicit domain
4421 * of a zero-dimensional isl_multi_union_pw_aff.
4422 * The core isl_multi_union_pw_aff ([...]) is read by
4423 * read_multi_union_pw_aff_core.
4425 __isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4426 __isl_keep isl_stream *s)
4428 isl_multi_union_pw_aff *mupa;
4430 if (!isl_stream_next_token_is(s, '('))
4431 return read_multi_union_pw_aff_core(s);
4433 if (isl_stream_eat(s, '(') < 0)
4434 return NULL;
4435 mupa = read_multi_union_pw_aff_core(s);
4436 if (isl_stream_eat_if_available(s, ':')) {
4437 isl_union_set *dom;
4439 dom = isl_stream_read_union_set(s);
4440 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4442 if (isl_stream_eat(s, ')') < 0)
4443 return isl_multi_union_pw_aff_free(mupa);
4444 return mupa;
4447 /* Read an isl_multi_union_pw_aff from "str".
4449 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4450 isl_ctx *ctx, const char *str)
4452 isl_multi_union_pw_aff *mupa;
4453 isl_stream *s = isl_stream_new_str(ctx, str);
4454 if (!s)
4455 return NULL;
4456 mupa = isl_stream_read_multi_union_pw_aff(s);
4457 isl_stream_free(s);
4458 return mupa;
4461 __isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4462 __isl_keep isl_stream *s)
4464 struct isl_obj obj;
4466 obj = obj_read(s);
4467 if (obj.type == isl_obj_pw_qpolynomial) {
4468 obj.type = isl_obj_union_pw_qpolynomial;
4469 obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4471 if (obj.v)
4472 isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4473 goto error);
4475 return obj.v;
4476 error:
4477 obj.type->free(obj.v);
4478 return NULL;
4481 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4482 isl_ctx *ctx, const char *str)
4484 isl_union_pw_qpolynomial *upwqp;
4485 isl_stream *s = isl_stream_new_str(ctx, str);
4486 if (!s)
4487 return NULL;
4488 upwqp = isl_stream_read_union_pw_qpolynomial(s);
4489 isl_stream_free(s);
4490 return upwqp;