1 /*
2 * Copyright 2013 Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
9
10 #include <isl/space.h>
11 #include <isl/set.h>
12
13 #include <isl_multi_macro.h>
14
15 /* This function is called for each element in a tuple inside
16 * isl_stream_read_multi_*.
17 * Read an EL from "s" and add it to *list.
18 */
FN(read_el,BASE)19 static __isl_give isl_space *FN(read_el,BASE)(__isl_keep isl_stream *s,
20 struct vars *v, __isl_take isl_space *space, int rational, void *user)
21 {
22 LIST(EL) **list = (LIST(EL) **) user;
23 EL *el;
24
25 el = FN(isl_stream_read,BASE)(s);
26 *list = FN(LIST(EL),add)(*list, el);
27 if (!*list)
28 return isl_space_free(space);
29
30 return space;
31 }
32
33 /* Read a multi expression from "s".
34 *
35 * We first read a tuple space, collecting the element values in a list.
36 * Then we create an isl_multi_* from the space and the isl_*_list.
37 */
MULTI(BASE)38 __isl_give MULTI(BASE) *FN(isl_stream_read_multi,BASE)(
39 __isl_keep isl_stream *s)
40 {
41 struct vars *v;
42 isl_set *dom = NULL;
43 isl_space *space;
44 MULTI(BASE) *multi = NULL;
45 LIST(EL) *list;
46
47 v = vars_new(s->ctx);
48 if (!v)
49 return NULL;
50
51 dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
52 if (next_is_tuple(s)) {
53 dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
54 if (isl_stream_eat(s, ISL_TOKEN_TO))
55 goto error;
56 }
57 if (!isl_set_plain_is_universe(dom))
58 isl_die(s->ctx, isl_error_invalid,
59 "expecting universe parameter domain", goto error);
60 if (isl_stream_eat(s, '{'))
61 goto error;
62
63 space = isl_set_get_space(dom);
64
65 list = FN(LIST(EL),alloc)(s->ctx, 0);
66 space = read_tuple_space(s, v, space, 1, 0, &FN(read_el,BASE), &list);
67 multi = FN(FN(MULTI(BASE),from),LIST(BASE))(space, list);
68
69 if (isl_stream_eat(s, '}'))
70 goto error;
71
72 vars_free(v);
73 isl_set_free(dom);
74 return multi;
75 error:
76 vars_free(v);
77 isl_set_free(dom);
78 FN(MULTI(BASE),free)(multi);
79 return NULL;
80 }
81
82 /* Read a multi expression from "str".
83 */
MULTI(BASE)84 __isl_give MULTI(BASE) *FN(MULTI(BASE),read_from_str)(isl_ctx *ctx,
85 const char *str)
86 {
87 MULTI(BASE) *multi;
88 isl_stream *s = isl_stream_new_str(ctx, str);
89 if (!s)
90 return NULL;
91 multi = FN(isl_stream_read_multi,BASE)(s);
92 isl_stream_free(s);
93 return multi;
94 }
95