isl_range.c qpolynomial_bound_on_domain_range: avoid invalid access on error path
[isl.git] / isl_multi_templ.c
blobd0b62cbc3274f8460e0688c51b61263fc73b68cb
1 /*
2 * Copyright 2011 Sven Verdoolaege
4 * Use of this software is governed by the MIT license
5 */
7 #define xCAT(A,B) A ## B
8 #define CAT(A,B) xCAT(A,B)
9 #undef EL
10 #define EL CAT(isl_,BASE)
11 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 #define xMULTI(BASE) isl_multi_ ## BASE
14 #define MULTI(BASE) xMULTI(BASE)
15 #define MULTI_NAME(BASE) "isl_multi_" #BASE
16 #define xLIST(EL) EL ## _list
17 #define LIST(EL) xLIST(EL)
19 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
21 return multi ? isl_space_get_ctx(multi->space) : NULL;
24 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
26 return multi ? isl_space_copy(multi->space) : NULL;
29 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
30 __isl_keep MULTI(BASE) *multi)
32 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
35 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
37 isl_ctx *ctx;
38 int n;
39 MULTI(BASE) *multi;
41 if (!space)
42 return NULL;
44 ctx = isl_space_get_ctx(space);
45 n = isl_space_dim(space, isl_dim_out);
46 multi = isl_calloc(ctx, MULTI(BASE),
47 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
48 if (!multi)
49 goto error;
51 multi->space = space;
52 multi->n = n;
53 multi->ref = 1;
54 return multi;
55 error:
56 isl_space_free(space);
57 return NULL;
60 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
62 int i;
63 MULTI(BASE) *dup;
65 if (!multi)
66 return NULL;
68 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
69 if (!dup)
70 return NULL;
72 for (i = 0; i < multi->n; ++i)
73 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
74 FN(EL,copy)(multi->p[i]));
76 return dup;
79 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
81 if (!multi)
82 return NULL;
84 if (multi->ref == 1)
85 return multi;
87 multi->ref--;
88 return FN(MULTI(BASE),dup)(multi);
91 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
93 if (!multi)
94 return NULL;
96 multi->ref++;
97 return multi;
100 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
102 int i;
104 if (!multi)
105 return NULL;
107 if (--multi->ref > 0)
108 return NULL;
110 isl_space_free(multi->space);
111 for (i = 0; i < multi->n; ++i)
112 FN(EL,free)(multi->p[i]);
113 free(multi);
115 return NULL;
118 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
119 enum isl_dim_type type)
121 return multi ? isl_space_dim(multi->space, type) : 0;
124 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
125 enum isl_dim_type type)
127 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
130 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
131 int pos)
133 isl_ctx *ctx;
135 if (!multi)
136 return NULL;
137 ctx = FN(MULTI(BASE),get_ctx)(multi);
138 if (pos < 0 || pos >= multi->n)
139 isl_die(ctx, isl_error_invalid,
140 "index out of bounds", return NULL);
141 return FN(EL,copy)(multi->p[pos]);
144 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
145 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
147 isl_space *multi_space = NULL;
148 isl_space *el_space = NULL;
150 multi = FN(MULTI(BASE),cow)(multi);
151 if (!multi || !el)
152 goto error;
154 multi_space = FN(MULTI(BASE),get_space)(multi);
155 el_space = FN(EL,get_space)(el);
157 if (!isl_space_match(multi_space, isl_dim_param,
158 el_space, isl_dim_param))
159 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
160 "parameters don't match", goto error);
161 if (!isl_space_tuple_match(multi_space, isl_dim_in,
162 el_space, isl_dim_in))
163 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
164 "domains don't match", goto error);
166 if (pos < 0 || pos >= multi->n)
167 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
168 "index out of bounds", goto error);
170 FN(EL,free)(multi->p[pos]);
171 multi->p[pos] = el;
173 isl_space_free(multi_space);
174 isl_space_free(el_space);
176 return multi;
177 error:
178 FN(MULTI(BASE),free)(multi);
179 FN(EL,free)(el);
180 isl_space_free(multi_space);
181 isl_space_free(el_space);
182 return NULL;
185 /* Reset the space of "multi". This function is called from isl_pw_templ.c
186 * and doesn't know if the space of an element object is represented
187 * directly or through its domain. It therefore passes along both,
188 * which we pass along to the element function since we don't how
189 * that is represented either.
191 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
192 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
193 __isl_take isl_space *domain)
195 int i;
197 multi = FN(MULTI(BASE),cow)(multi);
198 if (!multi || !space || !domain)
199 goto error;
201 for (i = 0; i < multi->n; ++i) {
202 multi->p[i] = FN(EL,reset_space_and_domain)(multi->p[i],
203 isl_space_copy(space), isl_space_copy(domain));
204 if (!multi->p[i])
205 goto error;
207 isl_space_free(domain);
208 isl_space_free(multi->space);
209 multi->space = space;
211 return multi;
212 error:
213 isl_space_free(domain);
214 isl_space_free(space);
215 FN(MULTI(BASE),free)(multi);
216 return NULL;
219 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
220 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
222 isl_space *space;
224 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
225 isl_space_copy(multi->space));
226 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
229 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
230 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
232 isl_space *domain;
234 domain = isl_space_domain(isl_space_copy(space));
235 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
238 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
239 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
240 __isl_take isl_id *id)
242 isl_space *space;
244 multi = FN(MULTI(BASE),cow)(multi);
245 if (!multi)
246 return isl_id_free(id);
248 space = FN(MULTI(BASE),get_space)(multi);
249 space = isl_space_set_tuple_id(space, type, id);
251 return FN(MULTI(BASE),reset_space)(multi, space);
254 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
255 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
257 int i;
259 multi = FN(MULTI(BASE),cow)(multi);
260 if (!multi || !exp)
261 return NULL;
263 for (i = 0; i < multi->n; ++i) {
264 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
265 isl_reordering_copy(exp));
266 if (!multi->p[i])
267 goto error;
270 multi = FN(MULTI(BASE),reset_domain_space)(multi,
271 isl_space_copy(exp->dim));
273 isl_reordering_free(exp);
274 return multi;
275 error:
276 isl_reordering_free(exp);
277 FN(MULTI(BASE),free)(multi);
278 return NULL;
281 /* Align the parameters of "multi" to those of "model".
283 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
284 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
286 isl_ctx *ctx;
288 if (!multi || !model)
289 goto error;
291 ctx = isl_space_get_ctx(model);
292 if (!isl_space_has_named_params(model))
293 isl_die(ctx, isl_error_invalid,
294 "model has unnamed parameters", goto error);
295 if (!isl_space_has_named_params(multi->space))
296 isl_die(ctx, isl_error_invalid,
297 "input has unnamed parameters", goto error);
298 if (!isl_space_match(multi->space, isl_dim_param,
299 model, isl_dim_param)) {
300 isl_reordering *exp;
302 model = isl_space_params(model);
303 exp = isl_parameter_alignment_reordering(multi->space, model);
304 exp = isl_reordering_extend_space(exp,
305 FN(MULTI(BASE),get_domain_space)(multi));
306 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
309 isl_space_free(model);
310 return multi;
311 error:
312 isl_space_free(model);
313 FN(MULTI(BASE),free)(multi);
314 return NULL;
317 static __isl_give MULTI(BASE) *align_params_multi_set_and(
318 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
319 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
320 __isl_take isl_set *set))
322 isl_ctx *ctx;
324 if (!multi || !set)
325 goto error;
326 if (isl_space_match(multi->space, isl_dim_param,
327 set->dim, isl_dim_param))
328 return fn(multi, set);
329 ctx = FN(MULTI(BASE),get_ctx)(multi);
330 if (!isl_space_has_named_params(multi->space) ||
331 !isl_space_has_named_params(set->dim))
332 isl_die(ctx, isl_error_invalid,
333 "unaligned unnamed parameters", goto error);
334 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
335 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
336 return fn(multi, set);
337 error:
338 FN(MULTI(BASE),free)(multi);
339 isl_set_free(set);
340 return NULL;
343 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
344 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
346 int i;
348 if (!multi || !context)
349 goto error;
351 for (i = 0; i < multi->n; ++i) {
352 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
353 if (!multi->p[i])
354 goto error;
357 isl_set_free(context);
358 return multi;
359 error:
360 isl_set_free(context);
361 FN(MULTI(BASE),free)(multi);
362 return NULL;
365 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
366 __isl_take isl_set *context)
368 return align_params_multi_set_and(multi, context,
369 &FN(MULTI(BASE),gist_aligned));
372 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
373 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
375 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
376 isl_set *dom_context = isl_set_universe(space);
377 dom_context = isl_set_intersect_params(dom_context, context);
378 return FN(MULTI(BASE),gist)(multi, dom_context);
381 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
382 __isl_take isl_space *space, __isl_take LIST(EL) *list)
384 int i;
385 int n;
386 isl_ctx *ctx;
387 MULTI(BASE) *multi;
389 if (!space || !list)
390 goto error;
392 ctx = isl_space_get_ctx(space);
393 n = FN(FN(LIST(EL),n),BASE)(list);
394 if (n != isl_space_dim(space, isl_dim_out))
395 isl_die(ctx, isl_error_invalid,
396 "invalid number of elements in list", goto error);
398 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
399 for (i = 0; i < n; ++i) {
400 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
401 FN(FN(LIST(EL),get),BASE)(list, i));
404 isl_space_free(space);
405 FN(LIST(EL),free)(list);
406 return multi;
407 error:
408 isl_space_free(space);
409 FN(LIST(EL),free)(list);
410 return NULL;