Coverage Report

Created: 2017-11-21 03:47

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_input.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 * Copyright 2010      INRIA Saclay
4
 * Copyright 2012-2013 Ecole Normale Superieure
5
 *
6
 * Use of this software is governed by the MIT license
7
 *
8
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
12
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13
 */
14
15
#include <ctype.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <isl_ctx_private.h>
19
#include <isl_map_private.h>
20
#include <isl/set.h>
21
#include <isl_seq.h>
22
#include <isl_stream_private.h>
23
#include <isl/obj.h>
24
#include "isl_polynomial_private.h"
25
#include <isl/union_set.h>
26
#include <isl/union_map.h>
27
#include <isl_mat_private.h>
28
#include <isl_aff_private.h>
29
#include <isl_vec_private.h>
30
#include <isl/list.h>
31
#include <isl_val_private.h>
32
33
struct variable {
34
  char            *name;
35
  int          pos;
36
  struct variable   *next;
37
};
38
39
struct vars {
40
  struct isl_ctx  *ctx;
41
  int    n;
42
  struct variable *v;
43
};
44
45
static struct vars *vars_new(struct isl_ctx *ctx)
46
2.46k
{
47
2.46k
  struct vars *v;
48
2.46k
  v = isl_alloc_type(ctx, struct vars);
49
2.46k
  if (!v)
50
0
    return NULL;
51
2.46k
  v->ctx = ctx;
52
2.46k
  v->n = 0;
53
2.46k
  v->v = NULL;
54
2.46k
  return v;
55
2.46k
}
56
57
static void variable_free(struct variable *var)
58
2.46k
{
59
3.52k
  while (var) {
60
1.06k
    struct variable *next = var->next;
61
1.06k
    free(var->name);
62
1.06k
    free(var);
63
1.06k
    var = next;
64
1.06k
  }
65
2.46k
}
66
67
static void vars_free(struct vars *v)
68
2.46k
{
69
2.46k
  if (!v)
70
0
    return;
71
2.46k
  variable_free(v->v);
72
2.46k
  free(v);
73
2.46k
}
74
75
static void vars_drop(struct vars *v, int n)
76
2.87k
{
77
2.87k
  struct variable *var;
78
2.87k
79
2.87k
  if (!v || !v->v)
80
163
    return;
81
2.71k
82
2.71k
  v->n -= n;
83
2.71k
84
2.71k
  var = v->v;
85
9.17k
  while (--n >= 0) {
86
6.46k
    struct variable *next = var->next;
87
6.46k
    free(var->name);
88
6.46k
    free(var);
89
6.46k
    var = next;
90
6.46k
  }
91
2.87k
  v->v = var;
92
2.87k
}
93
94
static struct variable *variable_new(struct vars *v, const char *name, int len,
95
        int pos)
96
7.53k
{
97
7.53k
  struct variable *var;
98
7.53k
  var = isl_calloc_type(v->ctx, struct variable);
99
7.53k
  if (!var)
100
0
    goto error;
101
7.53k
  var->name = strdup(name);
102
7.53k
  var->name[len] = '\0';
103
7.53k
  var->pos = pos;
104
7.53k
  var->next = v->v;
105
7.53k
  return var;
106
0
error:
107
0
  variable_free(v->v);
108
0
  return NULL;
109
7.53k
}
110
111
static int vars_pos(struct vars *v, const char *s, int len)
112
13.4k
{
113
13.4k
  int pos;
114
13.4k
  struct variable *q;
115
13.4k
116
13.4k
  if (len == -1)
117
13.4k
    len = strlen(s);
118
43.5k
  for (q = v->v; q; 
q = q->next30.1k
) {
119
39.0k
    if (strncmp(q->name, s, len) == 0 && 
q->name[len] == '\0'9.18k
)
120
8.86k
      break;
121
39.0k
  }
122
13.4k
  if (q)
123
8.86k
    pos = q->pos;
124
4.55k
  else {
125
4.55k
    pos = v->n;
126
4.55k
    v->v = variable_new(v, s, len, v->n);
127
4.55k
    if (!v->v)
128
0
      return -1;
129
4.55k
    v->n++;
130
4.55k
  }
131
13.4k
  return pos;
132
13.4k
}
133
134
static int vars_add_anon(struct vars *v)
135
2.97k
{
136
2.97k
  v->v = variable_new(v, "", 0, v->n);
137
2.97k
138
2.97k
  if (!v->v)
139
0
    return -1;
140
2.97k
  v->n++;
141
2.97k
142
2.97k
  return 0;
143
2.97k
}
144
145
/* Obtain next token, with some preprocessing.
146
 * In particular, evaluate expressions of the form x^y,
147
 * with x and y values.
148
 */
149
static struct isl_token *next_token(__isl_keep isl_stream *s)
150
46.8k
{
151
46.8k
  struct isl_token *tok, *tok2;
152
46.8k
153
46.8k
  tok = isl_stream_next_token(s);
154
46.8k
  if (!tok || tok->type != ISL_TOKEN_VALUE)
155
37.5k
    return tok;
156
9.34k
  if (!isl_stream_eat_if_available(s, '^'))
157
9.34k
    return tok;
158
0
  tok2 = isl_stream_next_token(s);
159
0
  if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
160
0
    isl_stream_error(s, tok2, "expecting constant value");
161
0
    goto error;
162
0
  }
163
0
164
0
  isl_int_pow_ui(tok->u.v, tok->u.v, isl_int_get_ui(tok2->u.v));
165
0
166
0
  isl_token_free(tok2);
167
0
  return tok;
168
0
error:
169
0
  isl_token_free(tok);
170
0
  isl_token_free(tok2);
171
0
  return NULL;
172
46.8k
}
173
174
/* Read an isl_val from "s".
175
 *
176
 * The following token sequences are recognized
177
 *
178
 *  "infty"   ->  infty
179
 *  "-" "infty" ->  -infty
180
 *  "NaN"   ->  NaN
181
 *  n "/" d   ->  n/d
182
 *  v   ->  v
183
 *
184
 * where n, d and v are integer constants.
185
 */
186
__isl_give isl_val *isl_stream_read_val(__isl_keep isl_stream *s)
187
342
{
188
342
  struct isl_token *tok = NULL;
189
342
  struct isl_token *tok2 = NULL;
190
342
  isl_val *val;
191
342
192
342
  tok = next_token(s);
193
342
  if (!tok) {
194
0
    isl_stream_error(s, NULL, "unexpected EOF");
195
0
    goto error;
196
0
  }
197
342
  if (tok->type == ISL_TOKEN_INFTY) {
198
50
    isl_token_free(tok);
199
50
    return isl_val_infty(s->ctx);
200
50
  }
201
292
  if (tok->type == '-' &&
202
292
      
isl_stream_eat_if_available(s, ISL_TOKEN_INFTY)32
) {
203
32
    isl_token_free(tok);
204
32
    return isl_val_neginfty(s->ctx);
205
32
  }
206
260
  if (tok->type == ISL_TOKEN_NAN) {
207
48
    isl_token_free(tok);
208
48
    return isl_val_nan(s->ctx);
209
48
  }
210
212
  if (tok->type != ISL_TOKEN_VALUE) {
211
0
    isl_stream_error(s, tok, "expecting value");
212
0
    goto error;
213
0
  }
214
212
215
212
  if (isl_stream_eat_if_available(s, '/')) {
216
53
    tok2 = next_token(s);
217
53
    if (!tok2) {
218
0
      isl_stream_error(s, NULL, "unexpected EOF");
219
0
      goto error;
220
0
    }
221
53
    if (tok2->type != ISL_TOKEN_VALUE) {
222
0
      isl_stream_error(s, tok2, "expecting value");
223
0
      goto error;
224
0
    }
225
53
    val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
226
53
    val = isl_val_normalize(val);
227
159
  } else {
228
159
    val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
229
159
  }
230
212
231
212
  isl_token_free(tok);
232
212
  isl_token_free(tok2);
233
212
  return val;
234
0
error:
235
0
  isl_token_free(tok);
236
0
  isl_token_free(tok2);
237
0
  return NULL;
238
342
}
239
240
/* Read an isl_val from "str".
241
 */
242
struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
243
  const char *str)
244
325
{
245
325
  isl_val *val;
246
325
  isl_stream *s = isl_stream_new_str(ctx, str);
247
325
  if (!s)
248
0
    return NULL;
249
325
  val = isl_stream_read_val(s);
250
325
  isl_stream_free(s);
251
325
  return val;
252
325
}
253
254
static int accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
255
325
{
256
325
  struct isl_token *tok;
257
325
258
325
  tok = next_token(s);
259
325
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
260
0
    isl_stream_error(s, tok, "expecting constant value");
261
0
    goto error;
262
0
  }
263
325
264
325
  isl_int_mul(*f, *f, tok->u.v);
265
325
266
325
  isl_token_free(tok);
267
325
268
325
  if (isl_stream_eat_if_available(s, '*'))
269
0
    return accept_cst_factor(s, f);
270
325
271
325
  return 0;
272
0
error:
273
0
  isl_token_free(tok);
274
0
  return -1;
275
325
}
276
277
/* Given an affine expression aff, return an affine expression
278
 * for aff % d, with d the next token on the stream, which is
279
 * assumed to be a constant.
280
 *
281
 * We introduce an integer division q = [aff/d] and the result
282
 * is set to aff - d q.
283
 */
284
static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
285
  struct vars *v, __isl_take isl_pw_aff *aff)
286
97
{
287
97
  struct isl_token *tok;
288
97
  isl_pw_aff *q;
289
97
290
97
  tok = next_token(s);
291
97
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
292
0
    isl_stream_error(s, tok, "expecting constant value");
293
0
    goto error;
294
0
  }
295
97
296
97
  q = isl_pw_aff_copy(aff);
297
97
  q = isl_pw_aff_scale_down(q, tok->u.v);
298
97
  q = isl_pw_aff_floor(q);
299
97
  q = isl_pw_aff_scale(q, tok->u.v);
300
97
301
97
  aff = isl_pw_aff_sub(aff, q);
302
97
303
97
  isl_token_free(tok);
304
97
  return aff;
305
0
error:
306
0
  isl_pw_aff_free(aff);
307
0
  isl_token_free(tok);
308
0
  return NULL;
309
97
}
310
311
static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
312
  __isl_take isl_space *space, struct vars *v);
313
static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
314
  __isl_take isl_space *dim, struct vars *v);
315
316
static __isl_give isl_pw_aff *accept_minmax(__isl_keep isl_stream *s,
317
  __isl_take isl_space *dim, struct vars *v)
318
6
{
319
6
  struct isl_token *tok;
320
6
  isl_pw_aff_list *list = NULL;
321
6
  int min;
322
6
323
6
  tok = isl_stream_next_token(s);
324
6
  if (!tok)
325
0
    goto error;
326
6
  min = tok->type == ISL_TOKEN_MIN;
327
6
  isl_token_free(tok);
328
6
329
6
  if (isl_stream_eat(s, '('))
330
0
    goto error;
331
6
332
6
  list = accept_affine_list(s, isl_space_copy(dim), v);
333
6
  if (!list)
334
0
    goto error;
335
6
336
6
  if (isl_stream_eat(s, ')'))
337
0
    goto error;
338
6
339
6
  isl_space_free(dim);
340
6
  return min ? 
isl_pw_aff_list_min(list)5
:
isl_pw_aff_list_max(list)1
;
341
0
error:
342
0
  isl_space_free(dim);
343
0
  isl_pw_aff_list_free(list);
344
0
  return NULL;
345
6
}
346
347
/* Is "tok" the start of an integer division?
348
 */
349
static int is_start_of_div(struct isl_token *tok)
350
14.0k
{
351
14.0k
  if (!tok)
352
0
    return 0;
353
14.0k
  if (tok->type == '[')
354
266
    return 1;
355
13.7k
  if (tok->type == ISL_TOKEN_FLOOR)
356
216
    return 1;
357
13.5k
  if (tok->type == ISL_TOKEN_CEIL)
358
0
    return 1;
359
13.5k
  if (tok->type == ISL_TOKEN_FLOORD)
360
2
    return 1;
361
13.5k
  if (tok->type == ISL_TOKEN_CEILD)
362
0
    return 1;
363
13.5k
  return 0;
364
13.5k
}
365
366
/* Read an integer division from "s" and return it as an isl_pw_aff.
367
 *
368
 * The integer division can be of the form
369
 *
370
 *  [<affine expression>]
371
 *  floor(<affine expression>)
372
 *  ceil(<affine expression>)
373
 *  floord(<affine expression>,<denominator>)
374
 *  ceild(<affine expression>,<denominator>)
375
 */
376
static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
377
  __isl_take isl_space *dim, struct vars *v)
378
299
{
379
299
  struct isl_token *tok;
380
299
  int f = 0;
381
299
  int c = 0;
382
299
  int extra = 0;
383
299
  isl_pw_aff *pwaff = NULL;
384
299
385
299
  if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
386
1
    extra = f = 1;
387
298
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
388
0
    extra = c = 1;
389
298
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
390
142
    f = 1;
391
156
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
392
0
    c = 1;
393
299
  if (f || 
c156
) {
394
143
    if (isl_stream_eat(s, '('))
395
0
      goto error;
396
156
  } else {
397
156
    if (isl_stream_eat(s, '['))
398
0
      goto error;
399
299
  }
400
299
401
299
  pwaff = accept_affine(s, isl_space_copy(dim), v);
402
299
403
299
  if (extra) {
404
1
    if (isl_stream_eat(s, ','))
405
0
      goto error;
406
1
407
1
    tok = next_token(s);
408
1
    if (!tok)
409
0
      goto error;
410
1
    if (tok->type != ISL_TOKEN_VALUE) {
411
0
      isl_stream_error(s, tok, "expected denominator");
412
0
      isl_stream_push_token(s, tok);
413
0
      goto error;
414
0
    }
415
1
    pwaff = isl_pw_aff_scale_down(pwaff,  tok->u.v);
416
1
    isl_token_free(tok);
417
1
  }
418
299
419
299
  if (c)
420
0
    pwaff = isl_pw_aff_ceil(pwaff);
421
299
  else
422
299
    pwaff = isl_pw_aff_floor(pwaff);
423
299
424
299
  if (f || 
c156
) {
425
143
    if (isl_stream_eat(s, ')'))
426
0
      goto error;
427
156
  } else {
428
156
    if (isl_stream_eat(s, ']'))
429
0
      goto error;
430
299
  }
431
299
432
299
  isl_space_free(dim);
433
299
  return pwaff;
434
0
error:
435
0
  isl_space_free(dim);
436
0
  isl_pw_aff_free(pwaff);
437
0
  return NULL;
438
299
}
439
440
static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
441
  __isl_take isl_space *dim, struct vars *v)
442
8.37k
{
443
8.37k
  struct isl_token *tok = NULL;
444
8.37k
  isl_pw_aff *res = NULL;
445
8.37k
446
8.37k
  tok = next_token(s);
447
8.37k
  if (!tok) {
448
0
    isl_stream_error(s, NULL, "unexpected EOF");
449
0
    goto error;
450
0
  }
451
8.37k
452
8.37k
  if (tok->type == ISL_TOKEN_AFF) {
453
179
    res = isl_pw_aff_copy(tok->u.pwaff);
454
179
    isl_token_free(tok);
455
8.19k
  } else if (tok->type == ISL_TOKEN_IDENT) {
456
7.70k
    int n = v->n;
457
7.70k
    int pos = vars_pos(v, tok->u.s, -1);
458
7.70k
    isl_aff *aff;
459
7.70k
460
7.70k
    if (pos < 0)
461
0
      goto error;
462
7.70k
    if (pos >= n) {
463
0
      vars_drop(v, v->n - n);
464
0
      isl_stream_error(s, tok, "unknown identifier");
465
0
      goto error;
466
0
    }
467
7.70k
468
7.70k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
469
7.70k
    if (!aff)
470
0
      goto error;
471
7.70k
    isl_int_set_si(aff->v->el[2 + pos], 1);
472
7.70k
    res = isl_pw_aff_from_aff(aff);
473
7.70k
    isl_token_free(tok);
474
7.70k
  } else 
if (487
tok->type == ISL_TOKEN_VALUE487
) {
475
0
    if (isl_stream_eat_if_available(s, '*')) {
476
0
      res = accept_affine_factor(s, isl_space_copy(dim), v);
477
0
      res = isl_pw_aff_scale(res, tok->u.v);
478
0
    } else {
479
0
      isl_local_space *ls;
480
0
      isl_aff *aff;
481
0
      ls = isl_local_space_from_space(isl_space_copy(dim));
482
0
      aff = isl_aff_zero_on_domain(ls);
483
0
      aff = isl_aff_add_constant(aff, tok->u.v);
484
0
      res = isl_pw_aff_from_aff(aff);
485
0
    }
486
0
    isl_token_free(tok);
487
487
  } else if (tok->type == '(') {
488
211
    isl_token_free(tok);
489
211
    tok = NULL;
490
211
    res = accept_affine(s, isl_space_copy(dim), v);
491
211
    if (!res)
492
0
      goto error;
493
211
    if (isl_stream_eat(s, ')'))
494
0
      goto error;
495
276
  } else if (is_start_of_div(tok)) {
496
270
    isl_stream_push_token(s, tok);
497
270
    tok = NULL;
498
270
    res = accept_div(s, isl_space_copy(dim), v);
499
270
  } else 
if (6
tok->type == ISL_TOKEN_MIN6
||
tok->type == ISL_TOKEN_MAX1
) {
500
6
    isl_stream_push_token(s, tok);
501
6
    tok = NULL;
502
6
    res = accept_minmax(s, isl_space_copy(dim), v);
503
6
  } else {
504
0
    isl_stream_error(s, tok, "expecting factor");
505
0
    goto error;
506
0
  }
507
8.37k
  if (isl_stream_eat_if_available(s, '%') ||
508
8.37k
      
isl_stream_eat_if_available(s, ISL_TOKEN_MOD)8.33k
) {
509
97
    isl_space_free(dim);
510
97
    return affine_mod(s, v, res);
511
97
  }
512
8.27k
  if (isl_stream_eat_if_available(s, '*')) {
513
7
    isl_int f;
514
7
    isl_int_init(f);
515
7
    isl_int_set_si(f, 1);
516
7
    if (accept_cst_factor(s, &f) < 0) {
517
0
      isl_int_clear(f);
518
0
      goto error2;
519
0
    }
520
7
    res = isl_pw_aff_scale(res, f);
521
7
    isl_int_clear(f);
522
7
  }
523
8.27k
  if (isl_stream_eat_if_available(s, '/')) {
524
318
    isl_int f;
525
318
    isl_int_init(f);
526
318
    isl_int_set_si(f, 1);
527
318
    if (accept_cst_factor(s, &f) < 0) {
528
0
      isl_int_clear(f);
529
0
      goto error2;
530
0
    }
531
318
    res = isl_pw_aff_scale_down(res, f);
532
318
    isl_int_clear(f);
533
318
  }
534
8.27k
535
8.27k
  isl_space_free(dim);
536
8.27k
  return res;
537
0
error:
538
0
  isl_token_free(tok);
539
0
error2:
540
0
  isl_pw_aff_free(res);
541
0
  isl_space_free(dim);
542
0
  return NULL;
543
8.37k
}
544
545
static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
546
5.50k
{
547
5.50k
  isl_aff *aff;
548
5.50k
  isl_space *space;
549
5.50k
550
5.50k
  space = isl_pw_aff_get_domain_space(pwaff);
551
5.50k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
552
5.50k
  aff = isl_aff_add_constant(aff, v);
553
5.50k
554
5.50k
  return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
555
5.50k
}
556
557
/* Return a piecewise affine expression defined on the specified domain
558
 * that represents NaN.
559
 */
560
static __isl_give isl_pw_aff *nan_on_domain(__isl_keep isl_space *space)
561
30
{
562
30
  isl_local_space *ls;
563
30
564
30
  ls = isl_local_space_from_space(isl_space_copy(space));
565
30
  return isl_pw_aff_nan_on_domain(ls);
566
30
}
567
568
static __isl_give isl_pw_aff *accept_affine(__isl_keep isl_stream *s,
569
  __isl_take isl_space *space, struct vars *v)
570
11.8k
{
571
11.8k
  struct isl_token *tok = NULL;
572
11.8k
  isl_local_space *ls;
573
11.8k
  isl_pw_aff *res;
574
11.8k
  int sign = 1;
575
11.8k
576
11.8k
  ls = isl_local_space_from_space(isl_space_copy(space));
577
11.8k
  res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
578
11.8k
  if (!res)
579
0
    goto error;
580
11.8k
581
13.9k
  
for (;;) 11.8k
{
582
13.9k
    tok = next_token(s);
583
13.9k
    if (!tok) {
584
0
      isl_stream_error(s, NULL, "unexpected EOF");
585
0
      goto error;
586
0
    }
587
13.9k
    if (tok->type == '-') {
588
73
      sign = -sign;
589
73
      isl_token_free(tok);
590
73
      continue;
591
73
    }
592
13.9k
    if (tok->type == '(' || 
is_start_of_div(tok)13.6k
||
593
13.9k
        
tok->type == ISL_TOKEN_MIN13.5k
||
tok->type == ISL_TOKEN_MAX13.5k
||
594
13.9k
        
tok->type == ISL_TOKEN_IDENT13.5k
||
595
13.9k
        
tok->type == ISL_TOKEN_AFF6.97k
) {
596
7.11k
      isl_pw_aff *term;
597
7.11k
      isl_stream_push_token(s, tok);
598
7.11k
      tok = NULL;
599
7.11k
      term = accept_affine_factor(s,
600
7.11k
                isl_space_copy(space), v);
601
7.11k
      if (sign < 0)
602
400
        res = isl_pw_aff_sub(res, term);
603
6.71k
      else
604
6.71k
        res = isl_pw_aff_add(res, term);
605
7.11k
      if (!res)
606
0
        goto error;
607
7.11k
      sign = 1;
608
7.11k
    } else 
if (6.79k
tok->type == ISL_TOKEN_VALUE6.79k
) {
609
6.76k
      if (sign < 0)
610
6.76k
        
isl_int_neg234
(tok->u.v, tok->u.v);
611
6.76k
      if (isl_stream_eat_if_available(s, '*') ||
612
6.76k
          
isl_stream_next_token_is(s, ISL_TOKEN_IDENT)6.66k
) {
613
1.25k
        isl_pw_aff *term;
614
1.25k
        term = accept_affine_factor(s,
615
1.25k
                isl_space_copy(space), v);
616
1.25k
        term = isl_pw_aff_scale(term, tok->u.v);
617
1.25k
        res = isl_pw_aff_add(res, term);
618
1.25k
        if (!res)
619
0
          goto error;
620
5.50k
      } else {
621
5.50k
        res = add_cst(res, tok->u.v);
622
5.50k
      }
623
6.76k
      sign = 1;
624
6.76k
    } else 
if (31
tok->type == ISL_TOKEN_NAN31
) {
625
30
      res = isl_pw_aff_add(res, nan_on_domain(space));
626
30
    } else {
627
1
      isl_stream_error(s, tok, "unexpected isl_token");
628
1
      isl_stream_push_token(s, tok);
629
1
      isl_pw_aff_free(res);
630
1
      isl_space_free(space);
631
1
      return NULL;
632
1
    }
633
13.9k
    isl_token_free(tok);
634
13.9k
635
13.9k
    tok = next_token(s);
636
13.9k
    if (tok && tok->type == '-') {
637
561
      sign = -sign;
638
561
      isl_token_free(tok);
639
13.3k
    } else if (tok && tok->type == '+') {
640
1.41k
      /* nothing */
641
1.41k
      isl_token_free(tok);
642
11.9k
    } else if (tok && tok->type == ISL_TOKEN_VALUE &&
643
11.9k
         
isl_int_is_neg37
(tok->u.v)) {
644
37
      isl_stream_push_token(s, tok);
645
11.8k
    } else {
646
11.8k
      if (tok)
647
11.8k
        isl_stream_push_token(s, tok);
648
13.3k
      break;
649
13.3k
    }
650
11.8k
  }
651
11.8k
652
11.8k
  isl_space_free(space);
653
11.8k
  return res;
654
0
error:
655
0
  isl_space_free(space);
656
0
  isl_token_free(tok);
657
0
  isl_pw_aff_free(res);
658
0
  return NULL;
659
11.8k
}
660
661
/* Is "type" the type of a comparison operator between lists
662
 * of affine expressions?
663
 */
664
static int is_list_comparator_type(int type)
665
19.6k
{
666
19.6k
  switch (type) {
667
19.6k
  case ISL_TOKEN_LEX_LT:
668
12
  case ISL_TOKEN_LEX_GT:
669
12
  case ISL_TOKEN_LEX_LE:
670
12
  case ISL_TOKEN_LEX_GE:
671
12
    return 1;
672
19.5k
  default:
673
19.5k
    return 0;
674
0
  }
675
0
}
676
677
static int is_comparator(struct isl_token *tok)
678
11.0k
{
679
11.0k
  if (!tok)
680
0
    return 0;
681
11.0k
  if (is_list_comparator_type(tok->type))
682
4
    return 1;
683
11.0k
684
11.0k
  switch (tok->type) {
685
11.0k
  case ISL_TOKEN_LT:
686
4.28k
  case ISL_TOKEN_GT:
687
4.28k
  case ISL_TOKEN_LE:
688
4.28k
  case ISL_TOKEN_GE:
689
4.28k
  case ISL_TOKEN_NE:
690
4.28k
  case '=':
691
4.28k
    return 1;
692
6.75k
  default:
693
6.75k
    return 0;
694
0
  }
695
0
}
696
697
static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
698
  struct vars *v, __isl_take isl_map *map, int rational);
699
static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
700
  __isl_take isl_space *dim, struct vars *v, int rational);
701
702
/* Accept a ternary operator, given the first argument.
703
 */
704
static __isl_give isl_pw_aff *accept_ternary(__isl_keep isl_stream *s,
705
  __isl_take isl_map *cond, struct vars *v, int rational)
706
1
{
707
1
  isl_space *dim;
708
1
  isl_pw_aff *pwaff1 = NULL, *pwaff2 = NULL, *pa_cond;
709
1
710
1
  if (!cond)
711
0
    return NULL;
712
1
713
1
  if (isl_stream_eat(s, '?'))
714
0
    goto error;
715
1
716
1
  dim = isl_space_wrap(isl_map_get_space(cond));
717
1
  pwaff1 = accept_extended_affine(s, dim, v, rational);
718
1
  if (!pwaff1)
719
0
    goto error;
720
1
721
1
  if (isl_stream_eat(s, ':'))
722
0
    goto error;
723
1
724
1
  dim = isl_pw_aff_get_domain_space(pwaff1);
725
1
  pwaff2 = accept_extended_affine(s, dim, v, rational);
726
1
  if (!pwaff1)
727
0
    goto error;
728
1
729
1
  pa_cond = isl_set_indicator_function(isl_map_wrap(cond));
730
1
  return isl_pw_aff_cond(pa_cond, pwaff1, pwaff2);
731
0
error:
732
0
  isl_map_free(cond);
733
0
  isl_pw_aff_free(pwaff1);
734
0
  isl_pw_aff_free(pwaff2);
735
0
  return NULL;
736
1
}
737
738
/* Set *line and *col to those of the next token, if any.
739
 */
740
static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
741
6.18k
{
742
6.18k
  struct isl_token *tok;
743
6.18k
744
6.18k
  tok = isl_stream_next_token(s);
745
6.18k
  if (!tok)
746
0
    return;
747
6.18k
748
6.18k
  *line = tok->line;
749
6.18k
  *col = tok->col;
750
6.18k
  isl_stream_push_token(s, tok);
751
6.18k
}
752
753
/* Push a token encapsulating "pa" onto "s", with the given
754
 * line and column.
755
 */
756
static int push_aff(__isl_keep isl_stream *s, int line, int col,
757
  __isl_take isl_pw_aff *pa)
758
43
{
759
43
  struct isl_token *tok;
760
43
761
43
  tok = isl_token_new(s->ctx, line, col, 0);
762
43
  if (!tok)
763
0
    goto error;
764
43
  tok->type = ISL_TOKEN_AFF;
765
43
  tok->u.pwaff = pa;
766
43
  isl_stream_push_token(s, tok);
767
43
768
43
  return 0;
769
0
error:
770
0
  isl_pw_aff_free(pa);
771
0
  return -1;
772
43
}
773
774
/* Accept an affine expression that may involve ternary operators.
775
 * We first read an affine expression.
776
 * If it is not followed by a comparison operator, we simply return it.
777
 * Otherwise, we assume the affine expression is part of the first
778
 * argument of a ternary operator and try to parse that.
779
 */
780
static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
781
  __isl_take isl_space *dim, struct vars *v, int rational)
782
3.15k
{
783
3.15k
  isl_space *space;
784
3.15k
  isl_map *cond;
785
3.15k
  isl_pw_aff *pwaff;
786
3.15k
  struct isl_token *tok;
787
3.15k
  int line = -1, col = -1;
788
3.15k
  int is_comp;
789
3.15k
790
3.15k
  set_current_line_col(s, &line, &col);
791
3.15k
792
3.15k
  pwaff = accept_affine(s, dim, v);
793
3.15k
  if (rational)
794
4
    pwaff = isl_pw_aff_set_rational(pwaff);
795
3.15k
  if (!pwaff)
796
0
    return NULL;
797
3.15k
798
3.15k
  tok = isl_stream_next_token(s);
799
3.15k
  if (!tok)
800
0
    return isl_pw_aff_free(pwaff);
801
3.15k
802
3.15k
  is_comp = is_comparator(tok);
803
3.15k
  isl_stream_push_token(s, tok);
804
3.15k
  if (!is_comp)
805
3.15k
    return pwaff;
806
1
807
1
  space = isl_pw_aff_get_domain_space(pwaff);
808
1
  cond = isl_map_universe(isl_space_unwrap(space));
809
1
810
1
  if (push_aff(s, line, col, pwaff) < 0)
811
0
    cond = isl_map_free(cond);
812
1
  if (!cond)
813
0
    return NULL;
814
1
815
1
  cond = read_formula(s, v, cond, rational);
816
1
817
1
  return accept_ternary(s, cond, v, rational);
818
1
}
819
820
static __isl_give isl_map *read_var_def(__isl_keep isl_stream *s,
821
  __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
822
  int rational)
823
126
{
824
126
  isl_pw_aff *def;
825
126
  int pos;
826
126
  isl_map *def_map;
827
126
828
126
  if (type == isl_dim_param)
829
0
    pos = isl_map_dim(map, isl_dim_param);
830
126
  else {
831
126
    pos = isl_map_dim(map, isl_dim_in);
832
126
    if (type == isl_dim_out)
833
126
      pos += isl_map_dim(map, isl_dim_out);
834
126
    type = isl_dim_in;
835
126
  }
836
126
  --pos;
837
126
838
126
  def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
839
126
          v, rational);
840
126
  def_map = isl_map_from_pw_aff(def);
841
126
  def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
842
126
  def_map = isl_set_unwrap(isl_map_domain(def_map));
843
126
844
126
  map = isl_map_intersect(map, def_map);
845
126
846
126
  return map;
847
126
}
848
849
static __isl_give isl_pw_aff_list *accept_affine_list(__isl_keep isl_stream *s,
850
  __isl_take isl_space *dim, struct vars *v)
851
7.89k
{
852
7.89k
  isl_pw_aff *pwaff;
853
7.89k
  isl_pw_aff_list *list;
854
7.89k
  struct isl_token *tok = NULL;
855
7.89k
856
7.89k
  pwaff = accept_affine(s, isl_space_copy(dim), v);
857
7.89k
  list = isl_pw_aff_list_from_pw_aff(pwaff);
858
7.89k
  if (!list)
859
1
    goto error;
860
7.89k
861
8.02k
  
for (;;) 7.89k
{
862
8.02k
    tok = isl_stream_next_token(s);
863
8.02k
    if (!tok) {
864
0
      isl_stream_error(s, NULL, "unexpected EOF");
865
0
      goto error;
866
0
    }
867
8.02k
    if (tok->type != ',') {
868
7.89k
      isl_stream_push_token(s, tok);
869
7.89k
      break;
870
7.89k
    }
871
137
    isl_token_free(tok);
872
137
873
137
    pwaff = accept_affine(s, isl_space_copy(dim), v);
874
137
    list = isl_pw_aff_list_concat(list,
875
137
        isl_pw_aff_list_from_pw_aff(pwaff));
876
137
    if (!list)
877
0
      goto error;
878
7.89k
  }
879
7.89k
880
7.89k
  isl_space_free(dim);
881
7.89k
  return list;
882
1
error:
883
1
  isl_space_free(dim);
884
1
  isl_pw_aff_list_free(list);
885
1
  return NULL;
886
7.89k
}
887
888
static __isl_give isl_map *read_defined_var_list(__isl_keep isl_stream *s,
889
  struct vars *v, __isl_take isl_map *map, int rational)
890
131
{
891
131
  struct isl_token *tok;
892
131
893
215
  while ((tok = isl_stream_next_token(s)) != NULL) {
894
215
    int p;
895
215
    int n = v->n;
896
215
897
215
    if (tok->type != ISL_TOKEN_IDENT)
898
0
      break;
899
215
900
215
    p = vars_pos(v, tok->u.s, -1);
901
215
    if (p < 0)
902
0
      goto error;
903
215
    if (p < n) {
904
0
      isl_stream_error(s, tok, "expecting unique identifier");
905
0
      goto error;
906
0
    }
907
215
908
215
    map = isl_map_add_dims(map, isl_dim_out, 1);
909
215
910
215
    isl_token_free(tok);
911
215
    tok = isl_stream_next_token(s);
912
215
    if (tok && tok->type == '=') {
913
126
      isl_token_free(tok);
914
126
      map = read_var_def(s, map, isl_dim_out, v, rational);
915
126
      tok = isl_stream_next_token(s);
916
126
    }
917
215
918
215
    if (!tok || tok->type != ',')
919
131
      break;
920
84
921
84
    isl_token_free(tok);
922
84
  }
923
131
  if (tok)
924
131
    isl_stream_push_token(s, tok);
925
131
926
131
  return map;
927
0
error:
928
0
  isl_token_free(tok);
929
0
  isl_map_free(map);
930
0
  return NULL;
931
131
}
932
933
static int next_is_tuple(__isl_keep isl_stream *s)
934
10.3k
{
935
10.3k
  struct isl_token *tok;
936
10.3k
  int is_tuple;
937
10.3k
938
10.3k
  tok = isl_stream_next_token(s);
939
10.3k
  if (!tok)
940
0
    return 0;
941
10.3k
  if (tok->type == '[') {
942
2.00k
    isl_stream_push_token(s, tok);
943
2.00k
    return 1;
944
2.00k
  }
945
8.30k
  if (tok->type != ISL_TOKEN_IDENT && 
!tok->is_keyword2.29k
) {
946
2.25k
    isl_stream_push_token(s, tok);
947
2.25k
    return 0;
948
2.25k
  }
949
6.04k
950
6.04k
  is_tuple = isl_stream_next_token_is(s, '[');
951
6.04k
952
6.04k
  isl_stream_push_token(s, tok);
953
6.04k
954
6.04k
  return is_tuple;
955
6.04k
}
956
957
/* Is "pa" an expression in term of earlier dimensions?
958
 * The alternative is that the dimension is defined to be equal to itself,
959
 * meaning that it has a universe domain and an expression that depends
960
 * on itself.  "i" is the position of the expression in a sequence
961
 * of "n" expressions.  The final dimensions of "pa" correspond to
962
 * these "n" expressions.
963
 */
964
static int pw_aff_is_expr(__isl_keep isl_pw_aff *pa, int i, int n)
965
123
{
966
123
  isl_aff *aff;
967
123
968
123
  if (!pa)
969
0
    return -1;
970
123
  if (pa->n != 1)
971
0
    return 1;
972
123
  if (!isl_set_plain_is_universe(pa->p[0].set))
973
0
    return 1;
974
123
975
123
  aff = pa->p[0].aff;
976
123
  if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
977
123
    
return 10
;
978
123
  return 0;
979
123
}
980
981
/* Does the tuple contain any dimensions that are defined
982
 * in terms of earlier dimensions?
983
 */
984
static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
985
107
{
986
107
  int i, n;
987
107
  int has_expr = 0;
988
107
  isl_pw_aff *pa;
989
107
990
107
  if (!tuple)
991
0
    return -1;
992
107
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
993
230
  for (i = 0; i < n; 
++i123
) {
994
123
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
995
123
    has_expr = pw_aff_is_expr(pa, i, n);
996
123
    isl_pw_aff_free(pa);
997
123
    if (has_expr < 0 || has_expr)
998
0
      break;
999
123
  }
1000
107
1001
107
  return has_expr;
1002
107
}
1003
1004
/* Set the name of dimension "pos" in "space" to "name".
1005
 * During printing, we add primes if the same name appears more than once
1006
 * to distinguish the occurrences.  Here, we remove those primes from "name"
1007
 * before setting the name of the dimension.
1008
 */
1009
static __isl_give isl_space *space_set_dim_name(__isl_take isl_space *space,
1010
  int pos, char *name)
1011
4.32k
{
1012
4.32k
  char *prime;
1013
4.32k
1014
4.32k
  if (!name)
1015
0
    return space;
1016
4.32k
1017
4.32k
  prime = strchr(name, '\'');
1018
4.32k
  if (prime)
1019
32
    *prime = '\0';
1020
4.32k
  space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1021
4.32k
  if (prime)
1022
32
    *prime = '\'';
1023
4.32k
1024
4.32k
  return space;
1025
4.32k
}
1026
1027
/* Accept a piecewise affine expression.
1028
 *
1029
 * At the outer level, the piecewise affine expression may be of the form
1030
 *
1031
 *  aff1 : condition1; aff2 : conditions2; ...
1032
 *
1033
 * or simply
1034
 *
1035
 *  aff
1036
 *
1037
 * each of the affine expressions may in turn include ternary operators.
1038
 *
1039
 * There may be parentheses around some subexpression of "aff1"
1040
 * around "aff1" itself, around "aff1 : condition1" and/or
1041
 * around the entire piecewise affine expression.
1042
 * We therefore remove the opening parenthesis (if any) from the stream
1043
 * in case the closing parenthesis follows the colon, but if the closing
1044
 * parenthesis is the first thing in the stream after the parsed affine
1045
 * expression, we push the parsed expression onto the stream and parse
1046
 * again in case the parentheses enclose some subexpression of "aff1".
1047
 */
1048
static __isl_give isl_pw_aff *accept_piecewise_affine(__isl_keep isl_stream *s,
1049
  __isl_take isl_space *space, struct vars *v, int rational)
1050
3.02k
{
1051
3.02k
  isl_pw_aff *res;
1052
3.02k
  isl_space *res_space;
1053
3.02k
1054
3.02k
  res_space = isl_space_from_domain(isl_space_copy(space));
1055
3.02k
  res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1056
3.02k
  res = isl_pw_aff_empty(res_space);
1057
3.02k
  do {
1058
3.02k
    isl_pw_aff *pa;
1059
3.02k
    int seen_paren;
1060
3.02k
    int line = -1, col = -1;
1061
3.02k
1062
3.02k
    set_current_line_col(s, &line, &col);
1063
3.02k
    seen_paren = isl_stream_eat_if_available(s, '(');
1064
3.02k
    if (seen_paren)
1065
42
      pa = accept_piecewise_affine(s, isl_space_copy(space),
1066
42
              v, rational);
1067
2.98k
    else
1068
2.98k
      pa = accept_extended_affine(s, isl_space_copy(space),
1069
2.98k
              v, rational);
1070
3.02k
    if (seen_paren && 
isl_stream_eat_if_available(s, ')')42
) {
1071
42
      seen_paren = 0;
1072
42
      if (push_aff(s, line, col, pa) < 0)
1073
0
        goto error;
1074
42
      pa = accept_extended_affine(s, isl_space_copy(space),
1075
42
              v, rational);
1076
42
    }
1077
3.02k
    if (isl_stream_eat_if_available(s, ':')) {
1078
12
      isl_space *dom_space;
1079
12
      isl_set *dom;
1080
12
1081
12
      dom_space = isl_pw_aff_get_domain_space(pa);
1082
12
      dom = isl_set_universe(dom_space);
1083
12
      dom = read_formula(s, v, dom, rational);
1084
12
      pa = isl_pw_aff_intersect_domain(pa, dom);
1085
12
    }
1086
3.02k
1087
3.02k
    res = isl_pw_aff_union_add(res, pa);
1088
3.02k
1089
3.02k
    if (seen_paren && 
isl_stream_eat(s, ')')0
)
1090
0
      goto error;
1091
3.02k
  } while (isl_stream_eat_if_available(s, ';'));
1092
3.02k
1093
3.02k
  isl_space_free(space);
1094
3.02k
1095
3.02k
  return res;
1096
0
error:
1097
0
  isl_space_free(space);
1098
0
  return isl_pw_aff_free(res);
1099
3.02k
}
1100
1101
/* Read an affine expression from "s" for use in read_tuple.
1102
 *
1103
 * accept_extended_affine requires a wrapped space as input.
1104
 * read_tuple on the other hand expects each isl_pw_aff
1105
 * to have an anonymous space.  We therefore adjust the space
1106
 * of the isl_pw_aff before returning it.
1107
 */
1108
static __isl_give isl_pw_aff *read_tuple_var_def(__isl_keep isl_stream *s,
1109
  struct vars *v, int rational)
1110
2.98k
{
1111
2.98k
  isl_space *space;
1112
2.98k
  isl_pw_aff *def;
1113
2.98k
1114
2.98k
  space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1115
2.98k
1116
2.98k
  def = accept_piecewise_affine(s, space, v, rational);
1117
2.98k
1118
2.98k
  space = isl_space_set_alloc(s->ctx, 0, v->n);
1119
2.98k
  def = isl_pw_aff_reset_domain_space(def, space);
1120
2.98k
1121
2.98k
  return def;
1122
2.98k
}
1123
1124
/* Read a list of tuple elements by calling "read_el" on each of them and
1125
 * return a space with the same number of set dimensions derived from
1126
 * the parameter space "space" and possibly updated by "read_el".
1127
 * The elements in the list are separated by either "," or "][".
1128
 * If "comma" is set then only "," is allowed.
1129
 */
1130
static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1131
  struct vars *v, __isl_take isl_space *space, int rational, int comma,
1132
  __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1133
    struct vars *v, __isl_take isl_space *space, int rational,
1134
    void *user),
1135
  void *user)
1136
5.50k
{
1137
5.50k
  if (!space)
1138
0
    return NULL;
1139
5.50k
1140
5.50k
  space = isl_space_set_from_params(space);
1141
5.50k
1142
5.50k
  if (isl_stream_next_token_is(s, ']'))
1143
954
    return space;
1144
4.54k
1145
7.33k
  
for (;;) 4.54k
{
1146
7.33k
    struct isl_token *tok;
1147
7.33k
1148
7.33k
    space = isl_space_add_dims(space, isl_dim_set, 1);
1149
7.33k
1150
7.33k
    space = read_el(s, v, space, rational, user);
1151
7.33k
    if (!space)
1152
0
      return NULL;
1153
7.33k
1154
7.33k
    tok = isl_stream_next_token(s);
1155
7.33k
    if (!comma && 
tok7.33k
&&
tok->type == ']'7.33k
&&
1156
7.33k
        
isl_stream_next_token_is(s, '[')4.54k
) {
1157
0
      isl_token_free(tok);
1158
0
      tok = isl_stream_next_token(s);
1159
7.33k
    } else if (!tok || tok->type != ',') {
1160
4.54k
      if (tok)
1161
4.54k
        isl_stream_push_token(s, tok);
1162
7.33k
      break;
1163
7.33k
    }
1164
2.78k
1165
2.78k
    isl_token_free(tok);
1166
2.78k
  }
1167
4.54k
1168
4.54k
  return space;
1169
5.50k
}
1170
1171
/* Read a tuple space from "s" derived from the parameter space "space".
1172
 * Call "read_el" on each element in the tuples.
1173
 */
1174
static __isl_give isl_space *read_tuple_space(__isl_keep isl_stream *s,
1175
  struct vars *v, __isl_take isl_space *space, int rational, int comma,
1176
  __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1177
    struct vars *v, __isl_take isl_space *space, int rational,
1178
    void *user),
1179
  void *user)
1180
5.72k
{
1181
5.72k
  struct isl_token *tok;
1182
5.72k
  char *name = NULL;
1183
5.72k
  isl_space *res = NULL;
1184
5.72k
1185
5.72k
  tok = isl_stream_next_token(s);
1186
5.72k
  if (!tok)
1187
0
    goto error;
1188
5.72k
  if (tok->type == ISL_TOKEN_IDENT || 
tok->is_keyword2.85k
) {
1189
2.86k
    name = strdup(tok->u.s);
1190
2.86k
    isl_token_free(tok);
1191
2.86k
    if (!name)
1192
0
      goto error;
1193
2.85k
  } else
1194
2.85k
    isl_stream_push_token(s, tok);
1195
5.72k
  if (isl_stream_eat(s, '['))
1196
0
    goto error;
1197
5.72k
  if (next_is_tuple(s)) {
1198
220
    isl_space *out;
1199
220
    res = read_tuple_space(s, v, isl_space_copy(space),
1200
220
          rational, comma, read_el, user);
1201
220
    if (isl_stream_eat(s, ISL_TOKEN_TO))
1202
0
      goto error;
1203
220
    out = read_tuple_space(s, v, isl_space_copy(space),
1204
220
          rational, comma, read_el, user);
1205
220
    res = isl_space_product(res, out);
1206
220
  } else
1207
5.50k
    res = read_tuple_list(s, v, isl_space_copy(space),
1208
5.50k
          rational, comma, read_el, user);
1209
5.72k
  if (isl_stream_eat(s, ']'))
1210
2
    goto error;
1211
5.72k
1212
5.72k
  if (name) {
1213
2.86k
    res = isl_space_set_tuple_name(res, isl_dim_set, name);
1214
2.86k
    free(name);
1215
2.86k
  }
1216
5.72k
1217
5.72k
  isl_space_free(space);
1218
5.72k
  return res;
1219
2
error:
1220
2
  free(name);
1221
2
  isl_space_free(res);
1222
2
  isl_space_free(space);
1223
2
  return NULL;
1224
5.72k
}
1225
1226
/* Construct an isl_pw_aff defined on a space with v->n variables
1227
 * that is equal to the last of those variables.
1228
 */
1229
static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1230
4.31k
{
1231
4.31k
  isl_space *space;
1232
4.31k
  isl_aff *aff;
1233
4.31k
1234
4.31k
  space = isl_space_set_alloc(v->ctx, 0, v->n);
1235
4.31k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1236
4.31k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1237
4.31k
  return isl_pw_aff_from_aff(aff);
1238
4.31k
}
1239
1240
/* This function is called for each element in a tuple inside read_tuple.
1241
 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1242
 * over a space containing all variables in "v" defined so far.
1243
 * The isl_pw_aff expresses the new variable in terms of earlier variables
1244
 * if a definition is provided.  Otherwise, it is represented as being
1245
 * equal to itself.
1246
 * Add the isl_pw_aff to *list.
1247
 * If the new variable was named, then adjust "space" accordingly and
1248
 * return the updated space.
1249
 */
1250
static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1251
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
1252
7.29k
{
1253
7.29k
  isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1254
7.29k
  isl_pw_aff *pa;
1255
7.29k
  struct isl_token *tok;
1256
7.29k
  int new_name = 0;
1257
7.29k
1258
7.29k
  tok = next_token(s);
1259
7.29k
  if (!tok) {
1260
0
    isl_stream_error(s, NULL, "unexpected EOF");
1261
0
    return isl_space_free(space);
1262
0
  }
1263
7.29k
1264
7.29k
  if (tok->type == ISL_TOKEN_IDENT) {
1265
5.46k
    int n = v->n;
1266
5.46k
    int p = vars_pos(v, tok->u.s, -1);
1267
5.46k
    if (p < 0)
1268
0
      goto error;
1269
5.46k
    new_name = p >= n;
1270
5.46k
  }
1271
7.29k
1272
7.29k
  if (tok->type == '*') {
1273
1
    if (vars_add_anon(v) < 0)
1274
0
      goto error;
1275
1
    isl_token_free(tok);
1276
1
    pa = identity_tuple_el(v);
1277
7.29k
  } else if (new_name) {
1278
4.32k
    int pos = isl_space_dim(space, isl_dim_out) - 1;
1279
4.32k
    space = space_set_dim_name(space, pos, v->v->name);
1280
4.32k
    isl_token_free(tok);
1281
4.32k
    if (isl_stream_eat_if_available(s, '='))
1282
10
      pa = read_tuple_var_def(s, v, rational);
1283
4.31k
    else
1284
4.31k
      pa = identity_tuple_el(v);
1285
4.32k
  } else {
1286
2.97k
    isl_stream_push_token(s, tok);
1287
2.97k
    tok = NULL;
1288
2.97k
    if (vars_add_anon(v) < 0)
1289
0
      goto error;
1290
2.97k
    pa = read_tuple_var_def(s, v, rational);
1291
2.97k
  }
1292
7.29k
1293
7.29k
  *list = isl_pw_aff_list_add(*list, pa);
1294
7.29k
  if (!*list)
1295
0
    return isl_space_free(space);
1296
7.29k
1297
7.29k
  return space;
1298
0
error:
1299
0
  isl_token_free(tok);
1300
0
  return isl_space_free(space);
1301
7.29k
}
1302
1303
/* Read a tuple and represent it as an isl_multi_pw_aff.
1304
 * The range space of the isl_multi_pw_aff is the space of the tuple.
1305
 * The domain space is an anonymous space
1306
 * with a dimension for each variable in the set of variables in "v",
1307
 * including the variables in the range.
1308
 * If a given dimension is not defined in terms of earlier dimensions in
1309
 * the input, then the corresponding isl_pw_aff is set equal to one time
1310
 * the variable corresponding to the dimension being defined.
1311
 *
1312
 * The elements in the tuple are collected in a list by read_tuple_pw_aff_el.
1313
 * Each element in this list is defined over a space representing
1314
 * the variables defined so far.  We need to adjust the earlier
1315
 * elements to have as many variables in the domain as the final
1316
 * element in the list.
1317
 */
1318
static __isl_give isl_multi_pw_aff *read_tuple(__isl_keep isl_stream *s,
1319
  struct vars *v, int rational, int comma)
1320
5.26k
{
1321
5.26k
  int i, n;
1322
5.26k
  isl_space *space;
1323
5.26k
  isl_pw_aff_list *list;
1324
5.26k
1325
5.26k
  space = isl_space_params_alloc(v->ctx, 0);
1326
5.26k
  list = isl_pw_aff_list_alloc(s->ctx, 0);
1327
5.26k
  space = read_tuple_space(s, v, space, rational, comma,
1328
5.26k
        &read_tuple_pw_aff_el, &list);
1329
5.26k
  n = isl_space_dim(space, isl_dim_set);
1330
8.07k
  for (i = 0; i + 1 < n; 
++i2.81k
) {
1331
2.81k
    isl_pw_aff *pa;
1332
2.81k
1333
2.81k
    pa = isl_pw_aff_list_get_pw_aff(list, i);
1334
2.81k
    pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1335
2.81k
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1336
2.81k
  }
1337
5.26k
1338
5.26k
  space = isl_space_from_range(space);
1339
5.26k
  space = isl_space_add_dims(space, isl_dim_in, v->n);
1340
5.26k
  return isl_multi_pw_aff_from_pw_aff_list(space, list);
1341
5.26k
}
1342
1343
/* Add the tuple represented by the isl_multi_pw_aff "tuple" to "map".
1344
 * We first create the appropriate space in "map" based on the range
1345
 * space of this isl_multi_pw_aff.  Then, we add equalities based
1346
 * on the affine expressions.  These live in an anonymous space,
1347
 * however, so we first need to reset the space to that of "map".
1348
 */
1349
static __isl_give isl_map *map_from_tuple(__isl_take isl_multi_pw_aff *tuple,
1350
  __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1351
  int rational)
1352
4.99k
{
1353
4.99k
  int i, n;
1354
4.99k
  isl_ctx *ctx;
1355
4.99k
  isl_space *space = NULL;
1356
4.99k
1357
4.99k
  if (!map || !tuple)
1358
0
    goto error;
1359
4.99k
  ctx = isl_multi_pw_aff_get_ctx(tuple);
1360
4.99k
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1361
4.99k
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1362
4.99k
  if (!space)
1363
0
    goto error;
1364
4.99k
1365
4.99k
  if (type == isl_dim_param) {
1366
609
    if (isl_space_has_tuple_name(space, isl_dim_set) ||
1367
609
        isl_space_is_wrapping(space)) {
1368
0
      isl_die(ctx, isl_error_invalid,
1369
0
        "parameter tuples cannot be named or nested",
1370
0
        goto error);
1371
0
    }
1372
609
    map = isl_map_add_dims(map, type, n);
1373
1.41k
    for (i = 0; i < n; 
++i806
) {
1374
806
      isl_id *id;
1375
806
      if (!isl_space_has_dim_name(space, isl_dim_set, i))
1376
806
        
isl_die0
(ctx, isl_error_invalid,
1377
806
          "parameters must be named",
1378
806
          goto error);
1379
806
      id = isl_space_get_dim_id(space, isl_dim_set, i);
1380
806
      map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1381
806
    }
1382
4.38k
  } else if (type == isl_dim_in) {
1383
2.63k
    isl_set *set;
1384
2.63k
1385
2.63k
    set = isl_set_universe(isl_space_copy(space));
1386
2.63k
    if (rational)
1387
22
      set = isl_set_set_rational(set);
1388
2.63k
    set = isl_set_intersect_params(set, isl_map_params(map));
1389
2.63k
    map = isl_map_from_domain(set);
1390
2.63k
  } else {
1391
1.75k
    isl_set *set;
1392
1.75k
1393
1.75k
    set = isl_set_universe(isl_space_copy(space));
1394
1.75k
    if (rational)
1395
54
      set = isl_set_set_rational(set);
1396
4.38k
    map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1397
4.38k
  }
1398
4.99k
1399
11.9k
  
for (i = 0; 4.99k
i < n;
++i6.95k
) {
1400
6.95k
    isl_pw_aff *pa;
1401
6.95k
    isl_space *space;
1402
6.95k
    isl_aff *aff;
1403
6.95k
    isl_set *set;
1404
6.95k
    isl_map *map_i;
1405
6.95k
1406
6.95k
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1407
6.95k
    space = isl_pw_aff_get_domain_space(pa);
1408
6.95k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1409
6.95k
    aff = isl_aff_add_coefficient_si(aff,
1410
6.95k
            isl_dim_in, v->n - n + i, -1);
1411
6.95k
    pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1412
6.95k
    if (rational)
1413
171
      pa = isl_pw_aff_set_rational(pa);
1414
6.95k
    set = isl_pw_aff_zero_set(pa);
1415
6.95k
    map_i = isl_map_from_range(set);
1416
6.95k
    map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1417
6.95k
    map = isl_map_intersect(map, map_i);
1418
6.95k
  }
1419
4.99k
1420
4.99k
  isl_space_free(space);
1421
4.99k
  isl_multi_pw_aff_free(tuple);
1422
4.99k
  return map;
1423
0
error:
1424
0
  isl_space_free(space);
1425
0
  isl_multi_pw_aff_free(tuple);
1426
0
  isl_map_free(map);
1427
0
  return NULL;
1428
4.99k
}
1429
1430
/* Read a tuple from "s" and add it to "map".
1431
 * The tuple is initially represented as an isl_multi_pw_aff and
1432
 * then added to "map".
1433
 */
1434
static __isl_give isl_map *read_map_tuple(__isl_keep isl_stream *s,
1435
  __isl_take isl_map *map, enum isl_dim_type type, struct vars *v,
1436
  int rational, int comma)
1437
4.98k
{
1438
4.98k
  isl_multi_pw_aff *tuple;
1439
4.98k
1440
4.98k
  tuple = read_tuple(s, v, rational, comma);
1441
4.98k
  if (!tuple)
1442
2
    return isl_map_free(map);
1443
4.98k
1444
4.98k
  return map_from_tuple(tuple, map, type, v, rational);
1445
4.98k
}
1446
1447
/* Given two equal-length lists of piecewise affine expression with the space
1448
 * of "set" as domain, construct a set in the same space that expresses
1449
 * that "left" and "right" satisfy the comparison "type".
1450
 *
1451
 * A space is constructed of the same dimension as the number of elements
1452
 * in the two lists.  The comparison is then expressed in a map from
1453
 * this space to itself and wrapped into a set.  Finally the two lists
1454
 * of piecewise affine expressions are plugged into this set.
1455
 *
1456
 * Let S be the space of "set" and T the constructed space.
1457
 * The lists are first changed into two isl_multi_pw_affs in S -> T and
1458
 * then combined into an isl_multi_pw_aff in S -> [T -> T],
1459
 * while the comparison is first expressed in T -> T, then [T -> T]
1460
 * and finally in S.
1461
 */
1462
static __isl_give isl_set *list_cmp(__isl_keep isl_set *set, int type,
1463
  __isl_take isl_pw_aff_list *left, __isl_take isl_pw_aff_list *right)
1464
4
{
1465
4
  isl_space *space;
1466
4
  int n;
1467
4
  isl_multi_pw_aff *mpa1, *mpa2;
1468
4
1469
4
  if (!set || !left || !right)
1470
0
    goto error;
1471
4
1472
4
  space = isl_set_get_space(set);
1473
4
  n = isl_pw_aff_list_n_pw_aff(left);
1474
4
  space = isl_space_from_domain(space);
1475
4
  space = isl_space_add_dims(space, isl_dim_out, n);
1476
4
  mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1477
4
  mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1478
4
  mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1479
4
1480
4
  space = isl_space_range(space);
1481
4
  switch (type) {
1482
4
  case ISL_TOKEN_LEX_LT:
1483
1
    set = isl_map_wrap(isl_map_lex_lt(space));
1484
1
    break;
1485
4
  case ISL_TOKEN_LEX_GT:
1486
1
    set = isl_map_wrap(isl_map_lex_gt(space));
1487
1
    break;
1488
4
  case ISL_TOKEN_LEX_LE:
1489
1
    set = isl_map_wrap(isl_map_lex_le(space));
1490
1
    break;
1491
4
  case ISL_TOKEN_LEX_GE:
1492
1
    set = isl_map_wrap(isl_map_lex_ge(space));
1493
1
    break;
1494
4
  default:
1495
0
    isl_multi_pw_aff_free(mpa1);
1496
0
    isl_space_free(space);
1497
0
    isl_die(isl_set_get_ctx(set), isl_error_internal,
1498
4
      "unhandled list comparison type", return NULL);
1499
4
  }
1500
4
  set = isl_set_preimage_multi_pw_aff(set, mpa1);
1501
4
  return set;
1502
0
error:
1503
0
  isl_pw_aff_list_free(left);
1504
0
  isl_pw_aff_list_free(right);
1505
0
  return NULL;
1506
4
}
1507
1508
/* Construct constraints of the form
1509
 *
1510
 *  a op b
1511
 *
1512
 * where a is an element in "left", op is an operator of type "type" and
1513
 * b is an element in "right", add the constraints to "set" and return
1514
 * the result.
1515
 * "rational" is set if the constraints should be treated as
1516
 * a rational constraints.
1517
 *
1518
 * If "type" is the type of a comparison operator between lists
1519
 * of affine expressions, then a single (compound) constraint
1520
 * is constructed by list_cmp instead.
1521
 */
1522
static __isl_give isl_set *construct_constraints(
1523
  __isl_take isl_set *set, int type,
1524
  __isl_keep isl_pw_aff_list *left, __isl_keep isl_pw_aff_list *right,
1525
  int rational)
1526
4.28k
{
1527
4.28k
  isl_set *cond;
1528
4.28k
1529
4.28k
  left = isl_pw_aff_list_copy(left);
1530
4.28k
  right = isl_pw_aff_list_copy(right);
1531
4.28k
  if (rational) {
1532
24
    left = isl_pw_aff_list_set_rational(left);
1533
24
    right = isl_pw_aff_list_set_rational(right);
1534
24
  }
1535
4.28k
  if (is_list_comparator_type(type))
1536
4
    cond = list_cmp(set, type, left, right);
1537
4.28k
  else if (type == ISL_TOKEN_LE)
1538
2.24k
    cond = isl_pw_aff_list_le_set(left, right);
1539
2.03k
  else if (type == ISL_TOKEN_GE)
1540
1.12k
    cond = isl_pw_aff_list_ge_set(left, right);
1541
910
  else if (type == ISL_TOKEN_LT)
1542
350
    cond = isl_pw_aff_list_lt_set(left, right);
1543
560
  else if (type == ISL_TOKEN_GT)
1544
53
    cond = isl_pw_aff_list_gt_set(left, right);
1545
507
  else if (type == ISL_TOKEN_NE)
1546
12
    cond = isl_pw_aff_list_ne_set(left, right);
1547
495
  else
1548
495
    cond = isl_pw_aff_list_eq_set(left, right);
1549
4.28k
1550
4.28k
  return isl_set_intersect(set, cond);
1551
4.28k
}
1552
1553
/* Read a constraint from "s", add it to "map" and return the result.
1554
 * "v" contains a description of the identifiers parsed so far.
1555
 * "rational" is set if the constraint should be treated as
1556
 * a rational constraint.
1557
 * The constraint read from "s" may be applied to multiple pairs
1558
 * of affine expressions and may be chained.
1559
 * In particular, a list of affine expressions is read, followed
1560
 * by a comparison operator and another list of affine expressions.
1561
 * The comparison operator is then applied to each pair of elements
1562
 * in the two lists and the results are added to "map".
1563
 * However, if the operator expects two lists of affine expressions,
1564
 * then it is applied directly to those lists and the two lists
1565
 * are required to have the same length.
1566
 * If the next token is another comparison operator, then another
1567
 * list of affine expressions is read and the process repeats.
1568
 *
1569
 * The processing is performed on a wrapped copy of "map" because
1570
 * an affine expression cannot have a binary relation as domain.
1571
 */
1572
static __isl_give isl_map *add_constraint(__isl_keep isl_stream *s,
1573
  struct vars *v, __isl_take isl_map *map, int rational)
1574
3.60k
{
1575
3.60k
  struct isl_token *tok;
1576
3.60k
  int type;
1577
3.60k
  isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1578
3.60k
  int n1, n2;
1579
3.60k
  isl_set *set;
1580
3.60k
1581
3.60k
  set = isl_map_wrap(map);
1582
3.60k
  list1 = accept_affine_list(s, isl_set_get_space(set), v);
1583
3.60k
  if (!list1)
1584
1
    goto error;
1585
3.60k
  tok = isl_stream_next_token(s);
1586
3.60k
  if (!is_comparator(tok)) {
1587
0
    isl_stream_error(s, tok, "missing operator");
1588
0
    if (tok)
1589
0
      isl_stream_push_token(s, tok);
1590
0
    goto error;
1591
0
  }
1592
3.60k
  type = tok->type;
1593
3.60k
  isl_token_free(tok);
1594
4.28k
  for (;;) {
1595
4.28k
    list2 = accept_affine_list(s, isl_set_get_space(set), v);
1596
4.28k
    if (!list2)
1597
0
      goto error;
1598
4.28k
    n1 = isl_pw_aff_list_n_pw_aff(list1);
1599
4.28k
    n2 = isl_pw_aff_list_n_pw_aff(list2);
1600
4.28k
    if (is_list_comparator_type(type) && 
n1 != n24
) {
1601
0
      isl_stream_error(s, NULL,
1602
0
          "list arguments not of same size");
1603
0
      goto error;
1604
0
    }
1605
4.28k
1606
4.28k
    set = construct_constraints(set, type, list1, list2, rational);
1607
4.28k
    isl_pw_aff_list_free(list1);
1608
4.28k
    list1 = list2;
1609
4.28k
1610
4.28k
    tok = isl_stream_next_token(s);
1611
4.28k
    if (!is_comparator(tok)) {
1612
3.60k
      if (tok)
1613
3.60k
        isl_stream_push_token(s, tok);
1614
3.60k
      break;
1615
3.60k
    }
1616
682
    type = tok->type;
1617
682
    isl_token_free(tok);
1618
682
  }
1619
3.60k
  isl_pw_aff_list_free(list1);
1620
3.60k
1621
3.60k
  return isl_set_unwrap(set);
1622
1
error:
1623
1
  isl_pw_aff_list_free(list1);
1624
1
  isl_pw_aff_list_free(list2);
1625
1
  isl_set_free(set);
1626
1
  return NULL;
1627
3.60k
}
1628
1629
static __isl_give isl_map *read_exists(__isl_keep isl_stream *s,
1630
  struct vars *v, __isl_take isl_map *map, int rational)
1631
131
{
1632
131
  int n = v->n;
1633
131
  int seen_paren = isl_stream_eat_if_available(s, '(');
1634
131
1635
131
  map = isl_map_from_domain(isl_map_wrap(map));
1636
131
  map = read_defined_var_list(s, v, map, rational);
1637
131
1638
131
  if (isl_stream_eat(s, ':'))
1639
0
    goto error;
1640
131
1641
131
  map = read_formula(s, v, map, rational);
1642
131
  map = isl_set_unwrap(isl_map_domain(map));
1643
131
1644
131
  vars_drop(v, v->n - n);
1645
131
  if (seen_paren && 
isl_stream_eat(s, ')')88
)
1646
0
    goto error;
1647
131
1648
131
  return map;
1649
0
error:
1650
0
  isl_map_free(map);
1651
0
  return NULL;
1652
131
}
1653
1654
/* Parse an expression between parentheses and push the result
1655
 * back on the stream.
1656
 *
1657
 * The parsed expression may be either an affine expression
1658
 * or a condition.  The first type is pushed onto the stream
1659
 * as an isl_pw_aff, while the second is pushed as an isl_map.
1660
 *
1661
 * If the initial token indicates the start of a condition,
1662
 * we parse it as such.
1663
 * Otherwise, we first parse an affine expression and push
1664
 * that onto the stream.  If the affine expression covers the
1665
 * entire expression between parentheses, we return.
1666
 * Otherwise, we assume that the affine expression is the
1667
 * start of a condition and continue parsing.
1668
 */
1669
static int resolve_paren_expr(__isl_keep isl_stream *s,
1670
  struct vars *v, __isl_take isl_map *map, int rational)
1671
175
{
1672
175
  struct isl_token *tok, *tok2;
1673
175
  int line, col;
1674
175
  isl_pw_aff *pwaff;
1675
175
1676
175
  tok = isl_stream_next_token(s);
1677
175
  if (!tok || tok->type != '(')
1678
0
    goto error;
1679
175
1680
175
  if (isl_stream_next_token_is(s, '('))
1681
7
    if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1682
0
      goto error;
1683
175
1684
175
  if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1685
175
      
isl_stream_next_token_is(s, ISL_TOKEN_NOT)144
||
1686
175
      
isl_stream_next_token_is(s, ISL_TOKEN_TRUE)143
||
1687
175
      
isl_stream_next_token_is(s, ISL_TOKEN_FALSE)143
||
1688
175
      
isl_stream_next_token_is(s, ISL_TOKEN_MAP)143
) {
1689
39
    map = read_formula(s, v, map, rational);
1690
39
    if (isl_stream_eat(s, ')'))
1691
0
      goto error;
1692
39
    tok->type = ISL_TOKEN_MAP;
1693
39
    tok->u.map = map;
1694
39
    isl_stream_push_token(s, tok);
1695
39
    return 0;
1696
39
  }
1697
136
1698
136
  tok2 = isl_stream_next_token(s);
1699
136
  if (!tok2)
1700
0
    goto error;
1701
136
  line = tok2->line;
1702
136
  col = tok2->col;
1703
136
  isl_stream_push_token(s, tok2);
1704
136
1705
136
  pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1706
136
  if (!pwaff)
1707
0
    goto error;
1708
136
1709
136
  tok2 = isl_token_new(s->ctx, line, col, 0);
1710
136
  if (!tok2)
1711
0
    goto error2;
1712
136
  tok2->type = ISL_TOKEN_AFF;
1713
136
  tok2->u.pwaff = pwaff;
1714
136
1715
136
  if (isl_stream_eat_if_available(s, ')')) {
1716
12
    isl_stream_push_token(s, tok2);
1717
12
    isl_token_free(tok);
1718
12
    isl_map_free(map);
1719
12
    return 0;
1720
12
  }
1721
124
1722
124
  isl_stream_push_token(s, tok2);
1723
124
1724
124
  map = read_formula(s, v, map, rational);
1725
124
  if (isl_stream_eat(s, ')'))
1726
0
    goto error;
1727
124
1728
124
  tok->type = ISL_TOKEN_MAP;
1729
124
  tok->u.map = map;
1730
124
  isl_stream_push_token(s, tok);
1731
124
1732
124
  return 0;
1733
0
error2:
1734
0
  isl_pw_aff_free(pwaff);
1735
0
error:
1736
0
  isl_token_free(tok);
1737
0
  isl_map_free(map);
1738
0
  return -1;
1739
175
}
1740
1741
static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
1742
  struct vars *v, __isl_take isl_map *map, int rational)
1743
3.90k
{
1744
3.90k
  if (isl_stream_next_token_is(s, '('))
1745
168
    if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1746
0
      goto error;
1747
3.90k
1748
3.90k
  if (isl_stream_next_token_is(s, ISL_TOKEN_MAP)) {
1749
163
    struct isl_token *tok;
1750
163
    tok = isl_stream_next_token(s);
1751
163
    if (!tok)
1752
0
      goto error;
1753
163
    isl_map_free(map);
1754
163
    map = isl_map_copy(tok->u.map);
1755
163
    isl_token_free(tok);
1756
163
    return map;
1757
163
  }
1758
3.74k
1759
3.74k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1760
131
    return read_exists(s, v, map, rational);
1761
3.61k
1762
3.61k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1763
1
    return map;
1764
3.61k
1765
3.61k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1766
7
    isl_space *dim = isl_map_get_space(map);
1767
7
    isl_map_free(map);
1768
7
    return isl_map_empty(dim);
1769
7
  }
1770
3.60k
    
1771
3.60k
  return add_constraint(s, v, map, rational);
1772
0
error:
1773
0
  isl_map_free(map);
1774
0
  return NULL;
1775
3.90k
}
1776
1777
static __isl_give isl_map *read_conjuncts(__isl_keep isl_stream *s,
1778
  struct vars *v, __isl_take isl_map *map, int rational)
1779
1.83k
{
1780
1.83k
  isl_map *res;
1781
1.83k
  int negate;
1782
1.83k
1783
1.83k
  negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1784
1.83k
  res = read_conjunct(s, v, isl_map_copy(map), rational);
1785
1.83k
  if (negate)
1786
1
    res = isl_map_subtract(isl_map_copy(map), res);
1787
1.83k
1788
3.90k
  while (res && 
isl_stream_eat_if_available(s, ISL_TOKEN_AND)3.90k
) {
1789
2.06k
    isl_map *res_i;
1790
2.06k
1791
2.06k
    negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1792
2.06k
    res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1793
2.06k
    if (negate)
1794
0
      res = isl_map_subtract(res, res_i);
1795
2.06k
    else
1796
2.06k
      res = isl_map_intersect(res, res_i);
1797
2.06k
  }
1798
1.83k
1799
1.83k
  isl_map_free(map);
1800
1.83k
  return res;
1801
1.83k
}
1802
1803
static struct isl_map *read_disjuncts(__isl_keep isl_stream *s,
1804
  struct vars *v, __isl_take isl_map *map, int rational)
1805
1.75k
{
1806
1.75k
  isl_map *res;
1807
1.75k
1808
1.75k
  if (isl_stream_next_token_is(s, '}'))
1809
65
    return map;
1810
1.68k
1811
1.68k
  res = read_conjuncts(s, v, isl_map_copy(map), rational);
1812
1.83k
  while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1813
152
    isl_map *res_i;
1814
152
1815
152
    res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1816
152
    res = isl_map_union(res, res_i);
1817
152
  }
1818
1.75k
1819
1.75k
  isl_map_free(map);
1820
1.75k
  return res;
1821
1.75k
}
1822
1823
/* Read a first order formula from "s", add the corresponding
1824
 * constraints to "map" and return the result.
1825
 *
1826
 * In particular, read a formula of the form
1827
 *
1828
 *  a
1829
 *
1830
 * or
1831
 *
1832
 *  a implies b
1833
 *
1834
 * where a and b are disjunctions.
1835
 *
1836
 * In the first case, map is replaced by
1837
 *
1838
 *  map \cap { [..] : a }
1839
 *
1840
 * In the second case, it is replaced by
1841
 *
1842
 *  (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1843
 */
1844
static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
1845
  struct vars *v, __isl_take isl_map *map, int rational)
1846
1.74k
{
1847
1.74k
  isl_map *res;
1848
1.74k
1849
1.74k
  res = read_disjuncts(s, v, isl_map_copy(map), rational);
1850
1.74k
1851
1.74k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)) {
1852
1
    isl_map *res2;
1853
1
1854
1
    res = isl_map_subtract(isl_map_copy(map), res);
1855
1
    res2 = read_disjuncts(s, v, map, rational);
1856
1
    res = isl_map_union(res, res2);
1857
1
  } else
1858
1.74k
    isl_map_free(map);
1859
1.74k
1860
1.74k
  return res;
1861
1.74k
}
1862
1863
static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1864
1.88k
{
1865
1.88k
  if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1866
1.45k
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1867
1.45k
         isl_basic_map_dim(bmap, isl_dim_in) + pos;
1868
429
  pos -= isl_basic_map_dim(bmap, isl_dim_out);
1869
429
1870
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1871
0
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1872
429
  pos -= isl_basic_map_dim(bmap, isl_dim_in);
1873
429
1874
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1875
0
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1876
0
         isl_basic_map_dim(bmap, isl_dim_in) +
1877
0
         isl_basic_map_dim(bmap, isl_dim_out) + pos;
1878
429
  pos -= isl_basic_map_dim(bmap, isl_dim_div);
1879
429
1880
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1881
0
    return 1 + pos;
1882
429
1883
429
  return 0;
1884
429
}
1885
1886
static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1887
  __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
1888
429
{
1889
429
  int j;
1890
429
  struct isl_token *tok;
1891
429
  int type;
1892
429
  int k;
1893
429
  isl_int *c;
1894
429
1895
429
  if (!bmap)
1896
0
    return NULL;
1897
429
1898
429
  tok = isl_stream_next_token(s);
1899
429
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
1900
0
    isl_stream_error(s, tok, "expecting coefficient");
1901
0
    if (tok)
1902
0
      isl_stream_push_token(s, tok);
1903
0
    goto error;
1904
0
  }
1905
429
  if (!tok->on_new_line) {
1906
0
    isl_stream_error(s, tok, "coefficient should appear on new line");
1907
0
    isl_stream_push_token(s, tok);
1908
0
    goto error;
1909
0
  }
1910
429
1911
429
  type = isl_int_get_si(tok->u.v);
1912
429
  isl_token_free(tok);
1913
429
1914
429
  isl_assert(s->ctx, type == 0 || type == 1, goto error);
1915
429
  if (type == 0) {
1916
30
    k = isl_basic_map_alloc_equality(bmap);
1917
30
    c = bmap->eq[k];
1918
399
  } else {
1919
399
    k = isl_basic_map_alloc_inequality(bmap);
1920
399
    c = bmap->ineq[k];
1921
399
  }
1922
429
  if (k < 0)
1923
0
    goto error;
1924
429
1925
2.31k
  
for (j = 0; 429
j < 1 + isl_basic_map_total_dim(bmap);
++j1.88k
) {
1926
1.88k
    int pos;
1927
1.88k
    tok = isl_stream_next_token(s);
1928
1.88k
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
1929
0
      isl_stream_error(s, tok, "expecting coefficient");
1930
0
      if (tok)
1931
0
        isl_stream_push_token(s, tok);
1932
0
      goto error;
1933
0
    }
1934
1.88k
    if (tok->on_new_line) {
1935
0
      isl_stream_error(s, tok,
1936
0
        "coefficient should not appear on new line");
1937
0
      isl_stream_push_token(s, tok);
1938
0
      goto error;
1939
0
    }
1940
1.88k
    pos = polylib_pos_to_isl_pos(bmap, j);
1941
1.88k
    isl_int_set(c[pos], tok->u.v);
1942
1.88k
    isl_token_free(tok);
1943
1.88k
  }
1944
429
1945
429
  return bmap;
1946
0
error:
1947
0
  isl_basic_map_free(bmap);
1948
0
  return NULL;
1949
429
}
1950
1951
static __isl_give isl_basic_map *basic_map_read_polylib(
1952
  __isl_keep isl_stream *s)
1953
105
{
1954
105
  int i;
1955
105
  struct isl_token *tok;
1956
105
  struct isl_token *tok2;
1957
105
  int n_row, n_col;
1958
105
  int on_new_line;
1959
105
  unsigned in = 0, out, local = 0;
1960
105
  struct isl_basic_map *bmap = NULL;
1961
105
  int nparam = 0;
1962
105
1963
105
  tok = isl_stream_next_token(s);
1964
105
  if (!tok) {
1965
0
    isl_stream_error(s, NULL, "unexpected EOF");
1966
0
    return NULL;
1967
0
  }
1968
105
  tok2 = isl_stream_next_token(s);
1969
105
  if (!tok2) {
1970
0
    isl_token_free(tok);
1971
0
    isl_stream_error(s, NULL, "unexpected EOF");
1972
0
    return NULL;
1973
0
  }
1974
105
  if (tok->type != ISL_TOKEN_VALUE || tok2->type != ISL_TOKEN_VALUE) {
1975
0
    isl_stream_push_token(s, tok2);
1976
0
    isl_stream_push_token(s, tok);
1977
0
    isl_stream_error(s, NULL,
1978
0
         "expecting constraint matrix dimensions");
1979
0
    return NULL;
1980
0
  }
1981
105
  n_row = isl_int_get_si(tok->u.v);
1982
105
  n_col = isl_int_get_si(tok2->u.v);
1983
105
  on_new_line = tok2->on_new_line;
1984
105
  isl_token_free(tok2);
1985
105
  isl_token_free(tok);
1986
105
  isl_assert(s->ctx, !on_new_line, return NULL);
1987
105
  isl_assert(s->ctx, n_row >= 0, return NULL);
1988
105
  isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
1989
105
  tok = isl_stream_next_token_on_same_line(s);
1990
105
  if (tok) {
1991
0
    if (tok->type != ISL_TOKEN_VALUE) {
1992
0
      isl_stream_error(s, tok,
1993
0
            "expecting number of output dimensions");
1994
0
      isl_stream_push_token(s, tok);
1995
0
      goto error;
1996
0
    }
1997
0
    out = isl_int_get_si(tok->u.v);
1998
0
    isl_token_free(tok);
1999
0
2000
0
    tok = isl_stream_next_token_on_same_line(s);
2001
0
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
2002
0
      isl_stream_error(s, tok,
2003
0
            "expecting number of input dimensions");
2004
0
      if (tok)
2005
0
        isl_stream_push_token(s, tok);
2006
0
      goto error;
2007
0
    }
2008
0
    in = isl_int_get_si(tok->u.v);
2009
0
    isl_token_free(tok);
2010
0
2011
0
    tok = isl_stream_next_token_on_same_line(s);
2012
0
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
2013
0
      isl_stream_error(s, tok,
2014
0
            "expecting number of existentials");
2015
0
      if (tok)
2016
0
        isl_stream_push_token(s, tok);
2017
0
      goto error;
2018
0
    }
2019
0
    local = isl_int_get_si(tok->u.v);
2020
0
    isl_token_free(tok);
2021
0
2022
0
    tok = isl_stream_next_token_on_same_line(s);
2023
0
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
2024
0
      isl_stream_error(s, tok,
2025
0
            "expecting number of parameters");
2026
0
      if (tok)
2027
0
        isl_stream_push_token(s, tok);
2028
0
      goto error;
2029
0
    }
2030
0
    nparam = isl_int_get_si(tok->u.v);
2031
0
    isl_token_free(tok);
2032
0
    if (n_col != 1 + out + in + local + nparam + 1) {
2033
0
      isl_stream_error(s, NULL,
2034
0
            "dimensions don't match");
2035
0
      goto error;
2036
0
    }
2037
105
  } else
2038
105
    out = n_col - 2 - nparam;
2039
105
  bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2040
105
  if (!bmap)
2041
0
    return NULL;
2042
105
2043
105
  for (i = 0; i < local; 
++i0
) {
2044
0
    int k = isl_basic_map_alloc_div(bmap);
2045
0
    if (k < 0)
2046
0
      goto error;
2047
0
    isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2048
0
  }
2049
105
2050
534
  
for (i = 0; 105
i < n_row;
++i429
)
2051
429
    bmap = basic_map_read_polylib_constraint(s, bmap);
2052
105
2053
105
  tok = isl_stream_next_token_on_same_line(s);
2054
105
  if (tok) {
2055
0
    isl_stream_error(s, tok, "unexpected extra token on line");
2056
0
    isl_stream_push_token(s, tok);
2057
0
    goto error;
2058
0
  }
2059
105
2060
105
  bmap = isl_basic_map_simplify(bmap);
2061
105
  bmap = isl_basic_map_finalize(bmap);
2062
105
  return bmap;
2063
0
error:
2064
0
  isl_basic_map_free(bmap);
2065
0
  return NULL;
2066
105
}
2067
2068
static struct isl_map *map_read_polylib(__isl_keep isl_stream *s)
2069
105
{
2070
105
  struct isl_token *tok;
2071
105
  struct isl_token *tok2;
2072
105
  int i, n;
2073
105
  struct isl_map *map;
2074
105
2075
105
  tok = isl_stream_next_token(s);
2076
105
  if (!tok) {
2077
0
    isl_stream_error(s, NULL, "unexpected EOF");
2078
0
    return NULL;
2079
0
  }
2080
105
  tok2 = isl_stream_next_token_on_same_line(s);
2081
105
  if (tok2 && tok2->type == ISL_TOKEN_VALUE) {
2082
105
    isl_stream_push_token(s, tok2);
2083
105
    isl_stream_push_token(s, tok);
2084
105
    return isl_map_from_basic_map(basic_map_read_polylib(s));
2085
105
  }
2086
0
  if (tok2) {
2087
0
    isl_stream_error(s, tok2, "unexpected token");
2088
0
    isl_stream_push_token(s, tok2);
2089
0
    isl_stream_push_token(s, tok);
2090
0
    return NULL;
2091
0
  }
2092
0
  n = isl_int_get_si(tok->u.v);
2093
0
  isl_token_free(tok);
2094
0
2095
0
  isl_assert(s->ctx, n >= 1, return NULL);
2096
0
2097
0
  map = isl_map_from_basic_map(basic_map_read_polylib(s));
2098
0
2099
0
  for (i = 1; map && i < n; ++i)
2100
0
    map = isl_map_union(map,
2101
0
      isl_map_from_basic_map(basic_map_read_polylib(s)));
2102
0
2103
0
  return map;
2104
105
}
2105
2106
static int optional_power(__isl_keep isl_stream *s)
2107
66
{
2108
66
  int pow;
2109
66
  struct isl_token *tok;
2110
66
2111
66
  tok = isl_stream_next_token(s);
2112
66
  if (!tok)
2113
0
    return 1;
2114
66
  if (tok->type != '^') {
2115
60
    isl_stream_push_token(s, tok);
2116
60
    return 1;
2117
60
  }
2118
6
  isl_token_free(tok);
2119
6
  tok = isl_stream_next_token(s);
2120
6
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
2121
0
    isl_stream_error(s, tok, "expecting exponent");
2122
0
    if (tok)
2123
0
      isl_stream_push_token(s, tok);
2124
0
    return 1;
2125
0
  }
2126
6
  pow = isl_int_get_si(tok->u.v);
2127
66
  isl_token_free(tok);
2128
66
  return pow;
2129
66
}
2130
2131
static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2132
  __isl_keep isl_map *map, struct vars *v);
2133
2134
static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2135
  __isl_keep isl_map *map, struct vars *v)
2136
84
{
2137
84
  isl_pw_qpolynomial *pwqp;
2138
84
  struct isl_token *tok;
2139
84
2140
84
  tok = next_token(s);
2141
84
  if (!tok) {
2142
0
    isl_stream_error(s, NULL, "unexpected EOF");
2143
0
    return NULL;
2144
0
  }
2145
84
  if (tok->type == '(') {
2146
21
    int pow;
2147
21
2148
21
    isl_token_free(tok);
2149
21
    pwqp = read_term(s, map, v);
2150
21
    if (!pwqp)
2151
0
      return NULL;
2152
21
    if (isl_stream_eat(s, ')'))
2153
0
      goto error;
2154
21
    pow = optional_power(s);
2155
21
    pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2156
63
  } else if (tok->type == ISL_TOKEN_VALUE) {
2157
15
    struct isl_token *tok2;
2158
15
    isl_qpolynomial *qp;
2159
15
2160
15
    tok2 = isl_stream_next_token(s);
2161
15
    if (tok2 && tok2->type == '/') {
2162
4
      isl_token_free(tok2);
2163
4
      tok2 = next_token(s);
2164
4
      if (!tok2 || tok2->type != ISL_TOKEN_VALUE) {
2165
0
        isl_stream_error(s, tok2, "expected denominator");
2166
0
        isl_token_free(tok);
2167
0
        isl_token_free(tok2);
2168
0
        return NULL;
2169
0
      }
2170
4
      qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2171
4
                tok->u.v, tok2->u.v);
2172
4
      isl_token_free(tok2);
2173
11
    } else {
2174
11
      isl_stream_push_token(s, tok2);
2175
11
      qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2176
11
            tok->u.v);
2177
11
    }
2178
15
    isl_token_free(tok);
2179
15
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2180
48
  } else if (tok->type == ISL_TOKEN_INFTY) {
2181
1
    isl_qpolynomial *qp;
2182
1
    isl_token_free(tok);
2183
1
    qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2184
1
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2185
47
  } else if (tok->type == ISL_TOKEN_NAN) {
2186
0
    isl_qpolynomial *qp;
2187
0
    isl_token_free(tok);
2188
0
    qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2189
0
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2190
47
  } else if (tok->type == ISL_TOKEN_IDENT) {
2191
16
    int n = v->n;
2192
16
    int pos = vars_pos(v, tok->u.s, -1);
2193
16
    int pow;
2194
16
    isl_qpolynomial *qp;
2195
16
    if (pos < 0) {
2196
0
      isl_token_free(tok);
2197
0
      return NULL;
2198
0
    }
2199
16
    if (pos >= n) {
2200
0
      vars_drop(v, v->n - n);
2201
0
      isl_stream_error(s, tok, "unknown identifier");
2202
0
      isl_token_free(tok);
2203
0
      return NULL;
2204
0
    }
2205
16
    isl_token_free(tok);
2206
16
    pow = optional_power(s);
2207
16
    qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2208
16
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2209
31
  } else if (is_start_of_div(tok)) {
2210
29
    isl_pw_aff *pwaff;
2211
29
    int pow;
2212
29
2213
29
    isl_stream_push_token(s, tok);
2214
29
    pwaff = accept_div(s, isl_map_get_space(map), v);
2215
29
    pow = optional_power(s);
2216
29
    pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2217
29
    pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2218
29
  } else 
if (2
tok->type == '-'2
) {
2219
2
    isl_token_free(tok);
2220
2
    pwqp = read_factor(s, map, v);
2221
2
    pwqp = isl_pw_qpolynomial_neg(pwqp);
2222
2
  } else {
2223
0
    isl_stream_error(s, tok, "unexpected isl_token");
2224
0
    isl_stream_push_token(s, tok);
2225
0
    return NULL;
2226
0
  }
2227
84
2228
84
  if (isl_stream_eat_if_available(s, '*') ||
2229
84
      
isl_stream_next_token_is(s, ISL_TOKEN_IDENT)69
) {
2230
15
    isl_pw_qpolynomial *pwqp2;
2231
15
2232
15
    pwqp2 = read_factor(s, map, v);
2233
15
    pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2234
15
  }
2235
84
2236
84
  return pwqp;
2237
0
error:
2238
0
  isl_pw_qpolynomial_free(pwqp);
2239
0
  return NULL;
2240
84
}
2241
2242
static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2243
  __isl_keep isl_map *map, struct vars *v)
2244
54
{
2245
54
  struct isl_token *tok;
2246
54
  isl_pw_qpolynomial *pwqp;
2247
54
2248
54
  pwqp = read_factor(s, map, v);
2249
54
2250
67
  for (;;) {
2251
67
    tok = next_token(s);
2252
67
    if (!tok)
2253
0
      return pwqp;
2254
67
2255
67
    if (tok->type == '+') {
2256
12
      isl_pw_qpolynomial *pwqp2;
2257
12
2258
12
      isl_token_free(tok);
2259
12
      pwqp2 = read_factor(s, map, v);
2260
12
      pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2261
55
    } else if (tok->type == '-') {
2262
1
      isl_pw_qpolynomial *pwqp2;
2263
1
2264
1
      isl_token_free(tok);
2265
1
      pwqp2 = read_factor(s, map, v);
2266
1
      pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2267
54
    } else if (tok->type == ISL_TOKEN_VALUE &&
2268
54
          
isl_int_is_neg0
(tok->u.v)) {
2269
0
      isl_pw_qpolynomial *pwqp2;
2270
0
2271
0
      isl_stream_push_token(s, tok);
2272
0
      pwqp2 = read_factor(s, map, v);
2273
0
      pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2274
54
    } else {
2275
54
      isl_stream_push_token(s, tok);
2276
54
      break;
2277
54
    }
2278
54
  }
2279
54
2280
54
  return pwqp;
2281
54
}
2282
2283
static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2284
  __isl_take isl_map *map, struct vars *v, int rational)
2285
2.85k
{
2286
2.85k
  struct isl_token *tok;
2287
2.85k
2288
2.85k
  tok = isl_stream_next_token(s);
2289
2.85k
  if (!tok) {
2290
0
    isl_stream_error(s, NULL, "unexpected EOF");
2291
0
    goto error;
2292
0
  }
2293
2.85k
  if (tok->type == ':' ||
2294
2.85k
      
(1.40k
tok->type == ISL_TOKEN_OR1.40k
&&
!strcmp(tok->u.s, "|")0
)) {
2295
1.44k
    isl_token_free(tok);
2296
1.44k
    map = read_formula(s, v, map, rational);
2297
1.44k
  } else
2298
1.40k
    isl_stream_push_token(s, tok);
2299
2.85k
2300
2.85k
  return map;
2301
0
error:
2302
0
  isl_map_free(map);
2303
0
  return NULL;
2304
2.85k
}
2305
2306
static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2307
  __isl_take isl_map *map, struct vars *v, int n)
2308
33
{
2309
33
  struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2310
33
  isl_pw_qpolynomial *pwqp;
2311
33
  struct isl_set *set;
2312
33
2313
33
  pwqp = read_term(s, map, v);
2314
33
  map = read_optional_formula(s, map, v, 0);
2315
33
  set = isl_map_range(map);
2316
33
2317
33
  pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2318
33
2319
33
  vars_drop(v, v->n - n);
2320
33
2321
33
  obj.v = pwqp;
2322
33
  return obj;
2323
33
}
2324
2325
static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2326
  __isl_take isl_set *set, struct vars *v, int n)
2327
33
{
2328
33
  struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2329
33
  isl_pw_qpolynomial *pwqp;
2330
33
  isl_pw_qpolynomial_fold *pwf = NULL;
2331
33
2332
33
  if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
2333
33
    return obj_read_poly(s, set, v, n);
2334
0
2335
0
  if (isl_stream_eat(s, '('))
2336
0
    goto error;
2337
0
2338
0
  pwqp = read_term(s, set, v);
2339
0
  pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
2340
0
2341
0
  while (isl_stream_eat_if_available(s, ',')) {
2342
0
    isl_pw_qpolynomial_fold *pwf_i;
2343
0
    pwqp = read_term(s, set, v);
2344
0
    pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
2345
0
                  pwqp);
2346
0
    pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2347
0
  }
2348
0
2349
0
  if (isl_stream_eat(s, ')'))
2350
0
    goto error;
2351
0
2352
0
  set = read_optional_formula(s, set, v, 0);
2353
0
  pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2354
0
2355
0
  vars_drop(v, v->n - n);
2356
0
2357
0
  obj.v = pwf;
2358
0
  return obj;
2359
0
error:
2360
0
  isl_set_free(set);
2361
0
  isl_pw_qpolynomial_fold_free(pwf);
2362
0
  obj.type = isl_obj_none;
2363
0
  return obj;
2364
33
}
2365
2366
static int is_rational(__isl_keep isl_stream *s)
2367
2.75k
{
2368
2.75k
  struct isl_token *tok;
2369
2.75k
2370
2.75k
  tok = isl_stream_next_token(s);
2371
2.75k
  if (!tok)
2372
0
    return 0;
2373
2.75k
  if (tok->type == ISL_TOKEN_RAT && 
isl_stream_next_token_is(s, ':')22
) {
2374
22
    isl_token_free(tok);
2375
22
    isl_stream_eat(s, ':');
2376
22
    return 1;
2377
22
  }
2378
2.73k
2379
2.73k
  isl_stream_push_token(s, tok);
2380
2.73k
2381
2.73k
  return 0;
2382
2.73k
}
2383
2384
static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2385
  __isl_take isl_map *map, struct vars *v)
2386
2.75k
{
2387
2.75k
  struct isl_token *tok;
2388
2.75k
  struct isl_obj obj = { isl_obj_set, NULL };
2389
2.75k
  int n = v->n;
2390
2.75k
  int rational;
2391
2.75k
2392
2.75k
  rational = is_rational(s);
2393
2.75k
  if (rational)
2394
22
    map = isl_map_set_rational(map);
2395
2.75k
2396
2.75k
  if (isl_stream_next_token_is(s, ':')) {
2397
131
    obj.type = isl_obj_set;
2398
131
    obj.v = read_optional_formula(s, map, v, rational);
2399
131
    return obj;
2400
131
  }
2401
2.62k
2402
2.62k
  if (!next_is_tuple(s))
2403
1
    return obj_read_poly_or_fold(s, map, v, n);
2404
2.62k
2405
2.62k
  map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2406
2.62k
  if (!map)
2407
0
    goto error;
2408
2.62k
  tok = isl_stream_next_token(s);
2409
2.62k
  if (!tok)
2410
0
    goto error;
2411
2.62k
  if (tok->type == ISL_TOKEN_TO) {
2412
1.73k
    obj.type = isl_obj_map;
2413
1.73k
    isl_token_free(tok);
2414
1.73k
    if (!next_is_tuple(s)) {
2415
32
      isl_set *set = isl_map_domain(map);
2416
32
      return obj_read_poly_or_fold(s, set, v, n);
2417
32
    }
2418
1.70k
    map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2419
1.70k
    if (!map)
2420
1
      goto error;
2421
889
  } else {
2422
889
    map = isl_map_domain(map);
2423
889
    isl_stream_push_token(s, tok);
2424
889
  }
2425
2.62k
2426
2.62k
  map = read_optional_formula(s, map, v, rational);
2427
2.58k
2428
2.58k
  vars_drop(v, v->n - n);
2429
2.58k
2430
2.58k
  obj.v = map;
2431
2.58k
  return obj;
2432
1
error:
2433
1
  isl_map_free(map);
2434
1
  obj.type = isl_obj_none;
2435
1
  return obj;
2436
2.75k
}
2437
2438
static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2439
766
{
2440
766
  if (obj.type == isl_obj_map) {
2441
626
    obj.v = isl_union_map_from_map(obj.v);
2442
626
    obj.type = isl_obj_union_map;
2443
626
  } else 
if (140
obj.type == 140
isl_obj_set140
) {
2444
138
    obj.v = isl_union_set_from_set(obj.v);
2445
138
    obj.type = isl_obj_union_set;
2446
138
  } else 
if (2
obj.type == 2
isl_obj_pw_qpolynomial2
) {
2447
2
    obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2448
2
    obj.type = isl_obj_union_pw_qpolynomial;
2449
2
  } else 
if (0
obj.type == 0
isl_obj_pw_qpolynomial_fold0
) {
2450
0
    obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2451
0
    obj.type = isl_obj_union_pw_qpolynomial_fold;
2452
0
  } else
2453
0
    isl_assert(ctx, 0, goto error);
2454
766
  return obj;
2455
0
error:
2456
0
  obj.type->free(obj.v);
2457
0
  obj.type = isl_obj_none;
2458
0
  return obj;
2459
766
}
2460
2461
static struct isl_obj obj_add(__isl_keep isl_stream *s,
2462
  struct isl_obj obj1, struct isl_obj obj2)
2463
675
{
2464
675
  if (obj1.type == isl_obj_set && 
obj2.type == 196
isl_obj_union_set196
)
2465
675
    
obj1 = to_union(s->ctx, obj1)0
;
2466
675
  if (obj1.type == isl_obj_union_set && 
obj2.type == 22
isl_obj_set22
)
2467
675
    
obj2 = to_union(s->ctx, obj2)22
;
2468
675
  if (obj1.type == isl_obj_map && 
obj2.type == 355
isl_obj_union_map355
)
2469
675
    
obj1 = to_union(s->ctx, obj1)0
;
2470
675
  if (obj1.type == isl_obj_union_map && 
obj2.type == 98
isl_obj_map98
)
2471
675
    
obj2 = to_union(s->ctx, obj2)98
;
2472
675
  if (obj1.type == isl_obj_pw_qpolynomial &&
2473
675
      
obj2.type == 4
isl_obj_union_pw_qpolynomial4
)
2474
675
    
obj1 = to_union(s->ctx, obj1)0
;
2475
675
  if (obj1.type == isl_obj_union_pw_qpolynomial &&
2476
675
      
obj2.type == 0
isl_obj_pw_qpolynomial0
)
2477
675
    
obj2 = to_union(s->ctx, obj2)0
;
2478
675
  if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2479
675
      
obj2.type == 0
isl_obj_union_pw_qpolynomial_fold0
)
2480
675
    
obj1 = to_union(s->ctx, obj1)0
;
2481
675
  if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2482
675
      
obj2.type == 0
isl_obj_pw_qpolynomial_fold0
)
2483
675
    
obj2 = to_union(s->ctx, obj2)0
;
2484
675
  if (obj1.type != obj2.type) {
2485
0
    isl_stream_error(s, NULL,
2486
0
        "attempt to combine incompatible objects");
2487
0
    goto error;
2488
0
  }
2489
675
  if (!obj1.type->add)
2490
675
    
isl_die0
(s->ctx, isl_error_internal,
2491
675
      "combination not supported on object type", goto error);
2492
675
  if (obj1.type == isl_obj_map && 
!isl_map_has_equal_space(obj1.v, obj2.v)355
) {
2493
256
    obj1 = to_union(s->ctx, obj1);
2494
256
    obj2 = to_union(s->ctx, obj2);
2495
256
  }
2496
675
  if (obj1.type == isl_obj_set && 
!isl_set_has_equal_space(obj1.v, obj2.v)196
) {
2497
58
    obj1 = to_union(s->ctx, obj1);
2498
58
    obj2 = to_union(s->ctx, obj2);
2499
58
  }
2500
675
  if (obj1.type == isl_obj_pw_qpolynomial &&
2501
675
      
!isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)4
) {
2502
1
    obj1 = to_union(s->ctx, obj1);
2503
1
    obj2 = to_union(s->ctx, obj2);
2504
1
  }
2505
675
  if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2506
675
      
!isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)0
) {
2507
0
    obj1 = to_union(s->ctx, obj1);
2508
0
    obj2 = to_union(s->ctx, obj2);
2509
0
  }
2510
675
  obj1.v = obj1.type->add(obj1.v, obj2.v);
2511
675
  return obj1;
2512
0
error:
2513
0
  obj1.type->free(obj1.v);
2514
0
  obj2.type->free(obj2.v);
2515
0
  obj1.type = isl_obj_none;
2516
0
  obj1.v = NULL;
2517
0
  return obj1;
2518
675
}
2519
2520
/* Are the first two tokens on "s", "domain" (either as a string
2521
 * or as an identifier) followed by ":"?
2522
 */
2523
static int next_is_domain_colon(__isl_keep isl_stream *s)
2524
2.36k
{
2525
2.36k
  struct isl_token *tok;
2526
2.36k
  char *name;
2527
2.36k
  int res;
2528
2.36k
2529
2.36k
  tok = isl_stream_next_token(s);
2530
2.36k
  if (!tok)
2531
0
    return 0;
2532
2.36k
  if (tok->type != ISL_TOKEN_IDENT && 
tok->type != ISL_TOKEN_STRING1.60k
) {
2533
1.60k
    isl_stream_push_token(s, tok);
2534
1.60k
    return 0;
2535
1.60k
  }
2536
768
2537
768
  name = isl_token_get_str(s->ctx, tok);
2538
768
  res = !strcmp(name, "domain") && 
isl_stream_next_token_is(s, ':')0
;
2539
2.36k
  free(name);
2540
2.36k
2541
2.36k
  isl_stream_push_token(s, tok);
2542
2.36k
2543
2.36k
  return res;
2544
2.36k
}
2545
2546
/* Do the first tokens on "s" look like a schedule?
2547
 *
2548
 * The root of a schedule is always a domain node, so the first thing
2549
 * we expect in the stream is a domain key, i.e., "domain" followed
2550
 * by ":".  If the schedule was printed in YAML flow style, then
2551
 * we additionally expect a "{" to open the outer mapping.
2552
 */
2553
static int next_is_schedule(__isl_keep isl_stream *s)
2554
2.36k
{
2555
2.36k
  struct isl_token *tok;
2556
2.36k
  int is_schedule;
2557
2.36k
2558
2.36k
  tok = isl_stream_next_token(s);
2559
2.36k
  if (!tok)
2560
0
    return 0;
2561
2.36k
  if (tok->type != '{') {
2562
678
    isl_stream_push_token(s, tok);
2563
678
    return next_is_domain_colon(s);
2564
678
  }
2565
1.69k
2566
1.69k
  is_schedule = next_is_domain_colon(s);
2567
1.69k
  isl_stream_push_token(s, tok);
2568
1.69k
2569
1.69k
  return is_schedule;
2570
1.69k
}
2571
2572
/* Read an isl_schedule from "s" and store it in an isl_obj.
2573
 */
2574
static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2575
0
{
2576
0
  struct isl_obj obj;
2577
0
2578
0
  obj.type = isl_obj_schedule;
2579
0
  obj.v = isl_stream_read_schedule(s);
2580
0
2581
0
  return obj;
2582
0
}
2583
2584
static struct isl_obj obj_read(__isl_keep isl_stream *s)
2585
2.36k
{
2586
2.36k
  isl_map *map = NULL;
2587
2.36k
  struct isl_token *tok;
2588
2.36k
  struct vars *v = NULL;
2589
2.36k
  struct isl_obj obj = { isl_obj_set, NULL };
2590
2.36k
2591
2.36k
  if (next_is_schedule(s))
2592
0
    return schedule_read(s);
2593
2.36k
2594
2.36k
  tok = next_token(s);
2595
2.36k
  if (!tok) {
2596
0
    isl_stream_error(s, NULL, "unexpected EOF");
2597
0
    goto error;
2598
0
  }
2599
2.36k
  if (tok->type == ISL_TOKEN_VALUE) {
2600
105
    struct isl_token *tok2;
2601
105
    struct isl_map *map;
2602
105
2603
105
    tok2 = isl_stream_next_token(s);
2604
105
    if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2605
105
        isl_int_is_neg(tok2->u.v)) {
2606
0
      if (tok2)
2607
0
        isl_stream_push_token(s, tok2);
2608
0
      obj.type = isl_obj_val;
2609
0
      obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2610
0
      isl_token_free(tok);
2611
0
      return obj;
2612
0
    }
2613
105
    isl_stream_push_token(s, tok2);
2614
105
    isl_stream_push_token(s, tok);
2615
105
    map = map_read_polylib(s);
2616
105
    if (!map)
2617
0
      goto error;
2618
105
    if (isl_map_may_be_set(map))
2619
105
      obj.v = isl_map_range(map);
2620
0
    else {
2621
0
      obj.type = isl_obj_map;
2622
0
      obj.v = map;
2623
0
    }
2624
105
    return obj;
2625
105
  }
2626
2.26k
  v = vars_new(s->ctx);
2627
2.26k
  if (!v) {
2628
0
    isl_stream_push_token(s, tok);
2629
0
    goto error;
2630
0
  }
2631
2.26k
  map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2632
2.26k
  if (tok->type == '[') {
2633
573
    isl_stream_push_token(s, tok);
2634
573
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2635
573
    if (!map)
2636
1
      goto error;
2637
572
    tok = isl_stream_next_token(s);
2638
572
    if (!tok || tok->type != ISL_TOKEN_TO) {
2639
0
      isl_stream_error(s, tok, "expecting '->'");
2640
0
      if (tok)
2641
0
        isl_stream_push_token(s, tok);
2642
0
      goto error;
2643
0
    }
2644
572
    isl_token_free(tok);
2645
572
    tok = isl_stream_next_token(s);
2646
572
  }
2647
2.26k
  
if (2.26k
!tok2.26k
||
tok->type != '{'2.26k
) {
2648
0
    isl_stream_error(s, tok, "expecting '{'");
2649
0
    if (tok)
2650
0
      isl_stream_push_token(s, tok);
2651
0
    goto error;
2652
0
  }
2653
2.26k
  isl_token_free(tok);
2654
2.26k
2655
2.26k
  tok = isl_stream_next_token(s);
2656
2.26k
  if (!tok)
2657
0
    ;
2658
2.26k
  else if (tok->type == ISL_TOKEN_IDENT && 
!strcmp(tok->u.s, "Sym")1.12k
) {
2659
1
    isl_token_free(tok);
2660
1
    if (isl_stream_eat(s, '='))
2661
0
      goto error;
2662
1
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2663
1
    if (!map)
2664
0
      goto error;
2665
2.26k
  } else if (tok->type == '}') {
2666
185
    obj.type = isl_obj_union_set;
2667
185
    obj.v = isl_union_set_empty(isl_map_get_space(map));
2668
185
    isl_token_free(tok);
2669
185
    goto done;
2670
185
  } else
2671
2.07k
    isl_stream_push_token(s, tok);
2672
2.26k
2673
2.75k
  
for (;;) 2.07k
{
2674
2.75k
    struct isl_obj o;
2675
2.75k
    tok = NULL;
2676
2.75k
    o = obj_read_body(s, isl_map_copy(map), v);
2677
2.75k
    if (o.type == isl_obj_none || 
!o.v2.75k
)
2678
2
      goto error;
2679
2.75k
    if (!obj.v)
2680
2.07k
      obj = o;
2681
675
    else {
2682
675
      obj = obj_add(s, obj, o);
2683
675
      if (obj.type == isl_obj_none || !obj.v)
2684
0
        goto error;
2685
2.75k
    }
2686
2.75k
    tok = isl_stream_next_token(s);
2687
2.75k
    if (!tok || tok->type != ';')
2688
2.07k
      break;
2689
675
    isl_token_free(tok);
2690
675
    if (isl_stream_next_token_is(s, '}')) {
2691
0
      tok = isl_stream_next_token(s);
2692
0
      break;
2693
0
    }
2694
2.07k
  }
2695
2.07k
2696
2.07k
  if (tok && tok->type == '}') {
2697
2.07k
    isl_token_free(tok);
2698
2.07k
  } else {
2699
0
    isl_stream_error(s, tok, "unexpected isl_token");
2700
0
    if (tok)
2701
0
      isl_token_free(tok);
2702
0
    goto error;
2703
0
  }
2704
2.26k
done:
2705
2.26k
  vars_free(v);
2706
2.26k
  isl_map_free(map);
2707
2.26k
2708
2.26k
  return obj;
2709
3
error:
2710
3
  isl_map_free(map);
2711
3
  obj.type->free(obj.v);
2712
3
  if (v)
2713
3
    vars_free(v);
2714
3
  obj.v = NULL;
2715
3
  return obj;
2716
2.36k
}
2717
2718
struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2719
16
{
2720
16
  return obj_read(s);
2721
16
}
2722
2723
__isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2724
625
{
2725
625
  struct isl_obj obj;
2726
625
2727
625
  obj = obj_read(s);
2728
625
  if (obj.v)
2729
625
    isl_assert(s->ctx, obj.type == isl_obj_map ||
2730
625
           obj.type == isl_obj_set, goto error);
2731
625
  
2732
625
  if (obj.type == isl_obj_set)
2733
625
    
obj.v = isl_map_from_range(obj.v)32
;
2734
625
2735
625
  return obj.v;
2736
0
error:
2737
0
  obj.type->free(obj.v);
2738
0
  return NULL;
2739
625
}
2740
2741
__isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2742
469
{
2743
469
  struct isl_obj obj;
2744
469
2745
469
  obj = obj_read(s);
2746
469
  if (obj.v) {
2747
468
    if (obj.type == isl_obj_map && 
isl_map_may_be_set(obj.v)0
) {
2748
0
      obj.v = isl_map_range(obj.v);
2749
0
      obj.type = isl_obj_set;
2750
0
    }
2751
468
    isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2752
468
  }
2753
469
2754
469
  return obj.v;
2755
0
error:
2756
0
  obj.type->free(obj.v);
2757
0
  return NULL;
2758
469
}
2759
2760
__isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2761
727
{
2762
727
  struct isl_obj obj;
2763
727
2764
727
  obj = obj_read(s);
2765
727
  if (obj.type == isl_obj_map) {
2766
367
    obj.type = isl_obj_union_map;
2767
367
    obj.v = isl_union_map_from_map(obj.v);
2768
367
  }
2769
727
  if (obj.type == isl_obj_set) {
2770
0
    obj.type = isl_obj_union_set;
2771
0
    obj.v = isl_union_set_from_set(obj.v);
2772
0
  }
2773
727
  if (obj.v && obj.type == isl_obj_union_set &&
2774
727
      
isl_union_set_is_empty(obj.v)110
)
2775
110
    obj.type = isl_obj_union_map;
2776
727
  if (obj.v && obj.type != isl_obj_union_map)
2777
727
    
isl_die0
(s->ctx, isl_error_invalid, "invalid input", goto error);
2778
727
2779
727
  return obj.v;
2780
0
error:
2781
0
  obj.type->free(obj.v);
2782
0
  return NULL;
2783
727
}
2784
2785
__isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2786
303
{
2787
303
  struct isl_obj obj;
2788
303
2789
303
  obj = obj_read(s);
2790
303
  if (obj.type == isl_obj_set) {
2791
171
    obj.type = isl_obj_union_set;
2792
171
    obj.v = isl_union_set_from_set(obj.v);
2793
171
  }
2794
303
  if (obj.v)
2795
303
    isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2796
303
2797
303
  return obj.v;
2798
0
error:
2799
0
  obj.type->free(obj.v);
2800
0
  return NULL;
2801
303
}
2802
2803
static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2804
177
{
2805
177
  struct isl_obj obj;
2806
177
  struct isl_map *map;
2807
177
  struct isl_basic_map *bmap;
2808
177
2809
177
  obj = obj_read(s);
2810
177
  if (obj.v && (obj.type != isl_obj_map && 
obj.type != 167
isl_obj_set167
))
2811
177
    
isl_die0
(s->ctx, isl_error_invalid, "not a (basic) set or map",
2812
177
      goto error);
2813
177
  map = obj.v;
2814
177
  if (!map)
2815
0
    return NULL;
2816
177
2817
177
  if (map->n > 1)
2818
177
    
isl_die0
(s->ctx, isl_error_invalid,
2819
177
      "set or map description involves "
2820
177
      "more than one disjunct", goto error);
2821
177
2822
177
  if (map->n == 0)
2823
4
    bmap = isl_basic_map_empty(isl_map_get_space(map));
2824
173
  else
2825
173
    bmap = isl_basic_map_copy(map->p[0]);
2826
177
2827
177
  isl_map_free(map);
2828
177
2829
177
  return bmap;
2830
0
error:
2831
0
  obj.type->free(obj.v);
2832
0
  return NULL;
2833
177
}
2834
2835
static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2836
167
{
2837
167
  isl_basic_map *bmap;
2838
167
  bmap = basic_map_read(s);
2839
167
  if (!bmap)
2840
0
    return NULL;
2841
167
  if (!isl_basic_map_may_be_set(bmap))
2842
167
    
isl_die0
(s->ctx, isl_error_invalid,
2843
167
      "input is not a set", goto error);
2844
167
  return isl_basic_map_range(bmap);
2845
0
error:
2846
0
  isl_basic_map_free(bmap);
2847
0
  return NULL;
2848
167
}
2849
2850
__isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2851
  FILE *input)
2852
2
{
2853
2
  struct isl_basic_map *bmap;
2854
2
  isl_stream *s = isl_stream_new_file(ctx, input);
2855
2
  if (!s)
2856
0
    return NULL;
2857
2
  bmap = basic_map_read(s);
2858
2
  isl_stream_free(s);
2859
2
  return bmap;
2860
2
}
2861
2862
__isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2863
  FILE *input)
2864
110
{
2865
110
  isl_basic_set *bset;
2866
110
  isl_stream *s = isl_stream_new_file(ctx, input);
2867
110
  if (!s)
2868
0
    return NULL;
2869
110
  bset = basic_set_read(s);
2870
110
  isl_stream_free(s);
2871
110
  return bset;
2872
110
}
2873
2874
struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2875
  const char *str)
2876
8
{
2877
8
  struct isl_basic_map *bmap;
2878
8
  isl_stream *s = isl_stream_new_str(ctx, str);
2879
8
  if (!s)
2880
0
    return NULL;
2881
8
  bmap = basic_map_read(s);
2882
8
  isl_stream_free(s);
2883
8
  return bmap;
2884
8
}
2885
2886
struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2887
  const char *str)
2888
57
{
2889
57
  isl_basic_set *bset;
2890
57
  isl_stream *s = isl_stream_new_str(ctx, str);
2891
57
  if (!s)
2892
0
    return NULL;
2893
57
  bset = basic_set_read(s);
2894
57
  isl_stream_free(s);
2895
57
  return bset;
2896
57
}
2897
2898
__isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2899
  FILE *input)
2900
0
{
2901
0
  struct isl_map *map;
2902
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2903
0
  if (!s)
2904
0
    return NULL;
2905
0
  map = isl_stream_read_map(s);
2906
0
  isl_stream_free(s);
2907
0
  return map;
2908
0
}
2909
2910
__isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2911
  const char *str)
2912
625
{
2913
625
  struct isl_map *map;
2914
625
  isl_stream *s = isl_stream_new_str(ctx, str);
2915
625
  if (!s)
2916
0
    return NULL;
2917
625
  map = isl_stream_read_map(s);
2918
625
  isl_stream_free(s);
2919
625
  return map;
2920
625
}
2921
2922
__isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2923
  FILE *input)
2924
0
{
2925
0
  isl_set *set;
2926
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2927
0
  if (!s)
2928
0
    return NULL;
2929
0
  set = isl_stream_read_set(s);
2930
0
  isl_stream_free(s);
2931
0
  return set;
2932
0
}
2933
2934
struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2935
  const char *str)
2936
469
{
2937
469
  isl_set *set;
2938
469
  isl_stream *s = isl_stream_new_str(ctx, str);
2939
469
  if (!s)
2940
0
    return NULL;
2941
469
  set = isl_stream_read_set(s);
2942
469
  isl_stream_free(s);
2943
469
  return set;
2944
469
}
2945
2946
__isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2947
  FILE *input)
2948
0
{
2949
0
  isl_union_map *umap;
2950
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2951
0
  if (!s)
2952
0
    return NULL;
2953
0
  umap = isl_stream_read_union_map(s);
2954
0
  isl_stream_free(s);
2955
0
  return umap;
2956
0
}
2957
2958
__isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2959
    const char *str)
2960
727
{
2961
727
  isl_union_map *umap;
2962
727
  isl_stream *s = isl_stream_new_str(ctx, str);
2963
727
  if (!s)
2964
0
    return NULL;
2965
727
  umap = isl_stream_read_union_map(s);
2966
727
  isl_stream_free(s);
2967
727
  return umap;
2968
727
}
2969
2970
__isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2971
  FILE *input)
2972
0
{
2973
0
  isl_union_set *uset;
2974
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2975
0
  if (!s)
2976
0
    return NULL;
2977
0
  uset = isl_stream_read_union_set(s);
2978
0
  isl_stream_free(s);
2979
0
  return uset;
2980
0
}
2981
2982
__isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2983
    const char *str)
2984
303
{
2985
303
  isl_union_set *uset;
2986
303
  isl_stream *s = isl_stream_new_str(ctx, str);
2987
303
  if (!s)
2988
0
    return NULL;
2989
303
  uset = isl_stream_read_union_set(s);
2990
303
  isl_stream_free(s);
2991
303
  return uset;
2992
303
}
2993
2994
static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
2995
0
{
2996
0
  struct isl_vec *vec = NULL;
2997
0
  struct isl_token *tok;
2998
0
  unsigned size;
2999
0
  int j;
3000
0
3001
0
  tok = isl_stream_next_token(s);
3002
0
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
3003
0
    isl_stream_error(s, tok, "expecting vector length");
3004
0
    goto error;
3005
0
  }
3006
0
3007
0
  size = isl_int_get_si(tok->u.v);
3008
0
  isl_token_free(tok);
3009
0
3010
0
  vec = isl_vec_alloc(s->ctx, size);
3011
0
3012
0
  for (j = 0; j < size; ++j) {
3013
0
    tok = isl_stream_next_token(s);
3014
0
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
3015
0
      isl_stream_error(s, tok, "expecting constant value");
3016
0
      goto error;
3017
0
    }
3018
0
    isl_int_set(vec->el[j], tok->u.v);
3019
0
    isl_token_free(tok);
3020
0
  }
3021
0
3022
0
  return vec;
3023
0
error:
3024
0
  isl_token_free(tok);
3025
0
  isl_vec_free(vec);
3026
0
  return NULL;
3027
0
}
3028
3029
static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3030
0
{
3031
0
  return isl_vec_read_polylib(s);
3032
0
}
3033
3034
__isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3035
0
{
3036
0
  isl_vec *v;
3037
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3038
0
  if (!s)
3039
0
    return NULL;
3040
0
  v = vec_read(s);
3041
0
  isl_stream_free(s);
3042
0
  return v;
3043
0
}
3044
3045
__isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3046
  __isl_keep isl_stream *s)
3047
27
{
3048
27
  struct isl_obj obj;
3049
27
3050
27
  obj = obj_read(s);
3051
27
  if (obj.v)
3052
27
    isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3053
27
         goto error);
3054
27
3055
27
  return obj.v;
3056
0
error:
3057
0
  obj.type->free(obj.v);
3058
0
  return NULL;
3059
27
}
3060
3061
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3062
    const char *str)
3063
27
{
3064
27
  isl_pw_qpolynomial *pwqp;
3065
27
  isl_stream *s = isl_stream_new_str(ctx, str);
3066
27
  if (!s)
3067
0
    return NULL;
3068
27
  pwqp = isl_stream_read_pw_qpolynomial(s);
3069
27
  isl_stream_free(s);
3070
27
  return pwqp;
3071
27
}
3072
3073
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3074
    FILE *input)
3075
0
{
3076
0
  isl_pw_qpolynomial *pwqp;
3077
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3078
0
  if (!s)
3079
0
    return NULL;
3080
0
  pwqp = isl_stream_read_pw_qpolynomial(s);
3081
0
  isl_stream_free(s);
3082
0
  return pwqp;
3083
0
}
3084
3085
/* Is the next token an identifer not in "v"?
3086
 */
3087
static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3088
26
{
3089
26
  int n = v->n;
3090
26
  int fresh;
3091
26
  struct isl_token *tok;
3092
26
3093
26
  tok = isl_stream_next_token(s);
3094
26
  if (!tok)
3095
0
    return 0;
3096
26
  fresh = tok->type == ISL_TOKEN_IDENT && 
vars_pos(v, tok->u.s, -1) >= n23
;
3097
26
  isl_stream_push_token(s, tok);
3098
26
3099
26
  vars_drop(v, v->n - n);
3100
26
3101
26
  return fresh;
3102
26
}
3103
3104
/* First read the domain of the affine expression, which may be
3105
 * a parameter space or a set.
3106
 * The tricky part is that we don't know if the domain is a set or not,
3107
 * so when we are trying to read the domain, we may actually be reading
3108
 * the affine expression itself (defined on a parameter domains)
3109
 * If the tuple we are reading is named, we assume it's the domain.
3110
 * Also, if inside the tuple, the first thing we find is a nested tuple
3111
 * or a new identifier, we again assume it's the domain.
3112
 * Finally, if the tuple is empty, then it must be the domain
3113
 * since it does not contain an affine expression.
3114
 * Otherwise, we assume we are reading an affine expression.
3115
 */
3116
static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3117
  __isl_take isl_set *dom, struct vars *v)
3118
58
{
3119
58
  struct isl_token *tok, *tok2;
3120
58
  int is_empty;
3121
58
3122
58
  tok = isl_stream_next_token(s);
3123
58
  if (tok && (tok->type == ISL_TOKEN_IDENT || 
tok->is_keyword27
)) {
3124
31
    isl_stream_push_token(s, tok);
3125
31
    return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
3126
31
  }
3127
27
  if (!tok || tok->type != '[') {
3128
0
    isl_stream_error(s, tok, "expecting '['");
3129
0
    goto error;
3130
0
  }
3131
27
  tok2 = isl_stream_next_token(s);
3132
27
  is_empty = tok2 && tok2->type == ']';
3133
27
  if (tok2)
3134
27
    isl_stream_push_token(s, tok2);
3135
27
  if (is_empty || 
next_is_tuple(s)26
||
next_is_fresh_ident(s, v)26
) {
3136
23
    isl_stream_push_token(s, tok);
3137
23
    dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
3138
23
  } else
3139
4
    isl_stream_push_token(s, tok);
3140
27
3141
27
  return dom;
3142
0
error:
3143
0
  if (tok)
3144
0
    isl_stream_push_token(s, tok);
3145
0
  isl_set_free(dom);
3146
0
  return NULL;
3147
58
}
3148
3149
/* Read an affine expression from "s".
3150
 */
3151
__isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3152
62
{
3153
62
  isl_aff *aff;
3154
62
  isl_multi_aff *ma;
3155
62
3156
62
  ma = isl_stream_read_multi_aff(s);
3157
62
  if (!ma)
3158
0
    return NULL;
3159
62
  if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
3160
62
    
isl_die0
(s->ctx, isl_error_invalid,
3161
62
      "expecting single affine expression",
3162
62
      goto error);
3163
62
3164
62
  aff = isl_multi_aff_get_aff(ma, 0);
3165
62
  isl_multi_aff_free(ma);
3166
62
  return aff;
3167
0
error:
3168
0
  isl_multi_aff_free(ma);
3169
0
  return NULL;
3170
62
}
3171
3172
/* Read a piecewise affine expression from "s" with domain (space) "dom".
3173
 */
3174
static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3175
  __isl_take isl_set *dom, struct vars *v)
3176
58
{
3177
58
  isl_pw_aff *pwaff = NULL;
3178
58
3179
58
  if (!isl_set_is_params(dom) && 
isl_stream_eat(s, ISL_TOKEN_TO)54
)
3180
0
    goto error;
3181
58
3182
58
  if (isl_stream_eat(s, '['))
3183
0
    goto error;
3184
58
3185
58
  pwaff = accept_affine(s, isl_set_get_space(dom), v);
3186
58
3187
58
  if (isl_stream_eat(s, ']'))
3188
0
    goto error;
3189
58
3190
58
  dom = read_optional_formula(s, dom, v, 0);
3191
58
  pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3192
58
3193
58
  return pwaff;
3194
0
error:
3195
0
  isl_set_free(dom);
3196
0
  isl_pw_aff_free(pwaff);
3197
0
  return NULL;
3198
58
}
3199
3200
__isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3201
22
{
3202
22
  struct vars *v;
3203
22
  isl_set *dom = NULL;
3204
22
  isl_set *aff_dom;
3205
22
  isl_pw_aff *pa = NULL;
3206
22
  int n;
3207
22
3208
22
  v = vars_new(s->ctx);
3209
22
  if (!v)
3210
0
    return NULL;
3211
22
3212
22
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3213
22
  if (next_is_tuple(s)) {
3214
2
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3215
2
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3216
0
      goto error;
3217
22
  }
3218
22
  if (isl_stream_eat(s, '{'))
3219
0
    goto error;
3220
22
3221
22
  n = v->n;
3222
22
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3223
22
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3224
22
  vars_drop(v, v->n - n);
3225
22
3226
27
  while (isl_stream_eat_if_available(s, ';')) {
3227
5
    isl_pw_aff *pa_i;
3228
5
3229
5
    n = v->n;
3230
5
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3231
5
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3232
5
    vars_drop(v, v->n - n);
3233
5
3234
5
    pa = isl_pw_aff_union_add(pa, pa_i);
3235
5
  }
3236
22
3237
22
  if (isl_stream_eat(s, '}'))
3238
0
    goto error;
3239
22
3240
22
  vars_free(v);
3241
22
  isl_set_free(dom);
3242
22
  return pa;
3243
0
error:
3244
0
  vars_free(v);
3245
0
  isl_set_free(dom);
3246
0
  isl_pw_aff_free(pa);
3247
0
  return NULL;
3248
22
}
3249
3250
__isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3251
62
{
3252
62
  isl_aff *aff;
3253
62
  isl_stream *s = isl_stream_new_str(ctx, str);
3254
62
  if (!s)
3255
0
    return NULL;
3256
62
  aff = isl_stream_read_aff(s);
3257
62
  isl_stream_free(s);
3258
62
  return aff;
3259
62
}
3260
3261
__isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3262
22
{
3263
22
  isl_pw_aff *pa;
3264
22
  isl_stream *s = isl_stream_new_str(ctx, str);
3265
22
  if (!s)
3266
0
    return NULL;
3267
22
  pa = isl_stream_read_pw_aff(s);
3268
22
  isl_stream_free(s);
3269
22
  return pa;
3270
22
}
3271
3272
/* Extract an isl_multi_pw_aff with domain space "dom_space"
3273
 * from a tuple "tuple" read by read_tuple.
3274
 *
3275
 * Note that the function read_tuple accepts tuples where some output or
3276
 * set dimensions are defined in terms of other output or set dimensions
3277
 * since this function is also used to read maps.  As a special case,
3278
 * read_tuple also accept dimensions that are defined in terms of themselves
3279
 * (i.e., that are not defined).
3280
 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3281
 * that the definitions of the output/set dimensions do not involve any
3282
 * output/set dimensions.
3283
 * Finally, drop the output dimensions from the domain of the result
3284
 * of read_tuple (which is of the form [input, output] -> [output],
3285
 * with anonymous domain) and reset the space.
3286
 */
3287
static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3288
  __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3289
44
{
3290
44
  int dim, i, n;
3291
44
  isl_space *space;
3292
44
  isl_multi_pw_aff *mpa;
3293
44
3294
44
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3295
44
  dim = isl_space_dim(dom_space, isl_dim_all);
3296
44
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3297
44
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3298
44
  if (!isl_space_is_params(dom_space))
3299
10
    space = isl_space_map_from_domain_and_range(
3300
10
        isl_space_copy(dom_space), space);
3301
44
  isl_space_free(dom_space);
3302
44
  mpa = isl_multi_pw_aff_alloc(space);
3303
44
3304
135
  for (i = 0; i < n; 
++i91
) {
3305
91
    isl_pw_aff *pa;
3306
91
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3307
91
    if (!pa)
3308
0
      return isl_multi_pw_aff_free(mpa);
3309
91
    if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3310
0
      isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3311
0
      isl_pw_aff_free(pa);
3312
0
      isl_die(ctx, isl_error_invalid,
3313
0
        "not an affine expression",
3314
0
        return isl_multi_pw_aff_free(mpa));
3315
0
    }
3316
91
    pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3317
91
    space = isl_multi_pw_aff_get_domain_space(mpa);
3318
91
    pa = isl_pw_aff_reset_domain_space(pa, space);
3319
91
    mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3320
91
  }
3321
44
3322
44
  return mpa;
3323
44
}
3324
3325
/* Read a tuple of affine expressions, together with optional constraints
3326
 * on the domain from "s".  "dom" represents the initial constraints
3327
 * on the domain.
3328
 *
3329
 * The isl_multi_aff may live in either a set or a map space.
3330
 * First read the first tuple and check if it is followed by a "->".
3331
 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3332
 * read in the next tuple.  This tuple (or the first tuple if it was
3333
 * not followed by a "->") is then converted into an isl_multi_pw_aff
3334
 * through a call to extract_mpa_from_tuple.
3335
 * The result is converted to an isl_pw_multi_aff and
3336
 * its domain is intersected with the domain.
3337
 */
3338
static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3339
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3340
41
{
3341
41
  isl_multi_pw_aff *tuple;
3342
41
  isl_multi_pw_aff *mpa;
3343
41
  isl_pw_multi_aff *pma;
3344
41
  int n = v->n;
3345
41
3346
41
  tuple = read_tuple(s, v, 0, 0);
3347
41
  if (!tuple)
3348
0
    goto error;
3349
41
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3350
7
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3351
7
    dom = isl_map_domain(map);
3352
7
    tuple = read_tuple(s, v, 0, 0);
3353
7
    if (!tuple)
3354
0
      goto error;
3355
41
  }
3356
41
3357
41
  dom = read_optional_formula(s, dom, v, 0);
3358
41
3359
41
  vars_drop(v, v->n - n);
3360
41
3361
41
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3362
41
  isl_multi_pw_aff_free(tuple);
3363
41
  pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3364
41
  pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3365
41
3366
41
  return pma;
3367
0
error:
3368
0
  isl_set_free(dom);
3369
0
  return NULL;
3370
41
}
3371
3372
/* Read an isl_pw_multi_aff from "s".
3373
 *
3374
 * In particular, first read the parameters and then read a sequence
3375
 * of one or more tuples of affine expressions with optional conditions and
3376
 * add them up.
3377
 */
3378
__isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3379
  __isl_keep isl_stream *s)
3380
38
{
3381
38
  struct vars *v;
3382
38
  isl_set *dom;
3383
38
  isl_pw_multi_aff *pma = NULL;
3384
38
3385
38
  v = vars_new(s->ctx);
3386
38
  if (!v)
3387
0
    return NULL;
3388
38
3389
38
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3390
38
  if (next_is_tuple(s)) {
3391
27
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3392
27
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3393
0
      goto error;
3394
38
  }
3395
38
  if (isl_stream_eat(s, '{'))
3396
0
    goto error;
3397
38
3398
38
  pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3399
38
3400
41
  while (isl_stream_eat_if_available(s, ';')) {
3401
3
    isl_pw_multi_aff *pma2;
3402
3
3403
3
    pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3404
3
    pma = isl_pw_multi_aff_union_add(pma, pma2);
3405
3
    if (!pma)
3406
0
      goto error;
3407
3
  }
3408
38
3409
38
  if (isl_stream_eat(s, '}'))
3410
0
    goto error;
3411
38
3412
38
  isl_set_free(dom);
3413
38
  vars_free(v);
3414
38
  return pma;
3415
0
error:
3416
0
  isl_pw_multi_aff_free(pma);
3417
0
  isl_set_free(dom);
3418
0
  vars_free(v);
3419
0
  return NULL;
3420
38
}
3421
3422
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3423
  const char *str)
3424
38
{
3425
38
  isl_pw_multi_aff *pma;
3426
38
  isl_stream *s = isl_stream_new_str(ctx, str);
3427
38
  if (!s)
3428
0
    return NULL;
3429
38
  pma = isl_stream_read_pw_multi_aff(s);
3430
38
  isl_stream_free(s);
3431
38
  return pma;
3432
38
}
3433
3434
/* Read an isl_union_pw_multi_aff from "s".
3435
 * We currently read a generic object and if it turns out to be a set or
3436
 * a map, we convert that to an isl_union_pw_multi_aff.
3437
 * It would be more efficient if we were to construct
3438
 * the isl_union_pw_multi_aff directly.
3439
 */
3440
__isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3441
  __isl_keep isl_stream *s)
3442
23
{
3443
23
  struct isl_obj obj;
3444
23
3445
23
  obj = obj_read(s);
3446
23
  if (!obj.v)
3447
0
    return NULL;
3448
23
3449
23
  if (obj.type == isl_obj_map || 
obj.type == 7
isl_obj_set7
)
3450
23
    
obj = to_union(s->ctx, obj)16
;
3451
23
  if (obj.type == isl_obj_union_map)
3452
23
    
return isl_union_pw_multi_aff_from_union_map(obj.v)22
;
3453
1
  if (obj.type == isl_obj_union_set)
3454
1
    return isl_union_pw_multi_aff_from_union_set(obj.v);
3455
0
3456
0
  obj.type->free(obj.v);
3457
0
  isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3458
23
    return NULL);
3459
23
}
3460
3461
/* Read an isl_union_pw_multi_aff from "str".
3462
 */
3463
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3464
  isl_ctx *ctx, const char *str)
3465
23
{
3466
23
  isl_union_pw_multi_aff *upma;
3467
23
  isl_stream *s = isl_stream_new_str(ctx, str);
3468
23
  if (!s)
3469
0
    return NULL;
3470
23
  upma = isl_stream_read_union_pw_multi_aff(s);
3471
23
  isl_stream_free(s);
3472
23
  return upma;
3473
23
}
3474
3475
/* Assuming "pa" represents a single affine expression defined on a universe
3476
 * domain, extract this affine expression.
3477
 */
3478
static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3479
126
{
3480
126
  isl_aff *aff;
3481
126
3482
126
  if (!pa)
3483
0
    return NULL;
3484
126
  if (pa->n != 1)
3485
126
    
isl_die0
(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3486
126
      "expecting single affine expression",
3487
126
      goto error);
3488
126
  if (!isl_set_plain_is_universe(pa->p[0].set))
3489
126
    
isl_die0
(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3490
126
      "expecting universe domain",
3491
126
      goto error);
3492
126
3493
126
  aff = isl_aff_copy(pa->p[0].aff);
3494
126
  isl_pw_aff_free(pa);
3495
126
  return aff;
3496
0
error:
3497
0
  isl_pw_aff_free(pa);
3498
0
  return NULL;
3499
126
}
3500
3501
/* This function is called for each element in a tuple inside
3502
 * isl_stream_read_multi_val.
3503
 * Read an isl_val from "s" and add it to *list.
3504
 */
3505
static __isl_give isl_space *read_val_el(__isl_keep isl_stream *s,
3506
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3507
17
{
3508
17
  isl_val_list **list = (isl_val_list **) user;
3509
17
  isl_val *val;
3510
17
3511
17
  val = isl_stream_read_val(s);
3512
17
  *list = isl_val_list_add(*list, val);
3513
17
  if (!*list)
3514
0
    return isl_space_free(space);
3515
17
3516
17
  return space;
3517
17
}
3518
3519
/* Read an isl_multi_val from "s".
3520
 *
3521
 * We first read a tuple space, collecting the element values in a list.
3522
 * Then we create an isl_multi_val from the space and the isl_val_list.
3523
 */
3524
__isl_give isl_multi_val *isl_stream_read_multi_val(__isl_keep isl_stream *s)
3525
7
{
3526
7
  struct vars *v;
3527
7
  isl_set *dom = NULL;
3528
7
  isl_space *space;
3529
7
  isl_multi_val *mv = NULL;
3530
7
  isl_val_list *list;
3531
7
3532
7
  v = vars_new(s->ctx);
3533
7
  if (!v)
3534
0
    return NULL;
3535
7
3536
7
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3537
7
  if (next_is_tuple(s)) {
3538
1
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3539
1
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3540
0
      goto error;
3541
7
  }
3542
7
  if (!isl_set_plain_is_universe(dom))
3543
7
    
isl_die0
(s->ctx, isl_error_invalid,
3544
7
      "expecting universe parameter domain", goto error);
3545
7
  if (isl_stream_eat(s, '{'))
3546
0
    goto error;
3547
7
3548
7
  space = isl_set_get_space(dom);
3549
7
3550
7
  list = isl_val_list_alloc(s->ctx, 0);
3551
7
  space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list);
3552
7
  mv = isl_multi_val_from_val_list(space, list);
3553
7
3554
7
  if (isl_stream_eat(s, '}'))
3555
0
    goto error;
3556
7
3557
7
  vars_free(v);
3558
7
  isl_set_free(dom);
3559
7
  return mv;
3560
0
error:
3561
0
  vars_free(v);
3562
0
  isl_set_free(dom);
3563
0
  isl_multi_val_free(mv);
3564
0
  return NULL;
3565
7
}
3566
3567
/* Read an isl_multi_val from "str".
3568
 */
3569
__isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx,
3570
  const char *str)
3571
7
{
3572
7
  isl_multi_val *mv;
3573
7
  isl_stream *s = isl_stream_new_str(ctx, str);
3574
7
  if (!s)
3575
0
    return NULL;
3576
7
  mv = isl_stream_read_multi_val(s);
3577
7
  isl_stream_free(s);
3578
7
  return mv;
3579
7
}
3580
3581
/* Read a multi-affine expression from "s".
3582
 * If the multi-affine expression has a domain, then the tuple
3583
 * representing this domain cannot involve any affine expressions.
3584
 * The tuple representing the actual expressions needs to consist
3585
 * of only affine expressions.  Moreover, these expressions can
3586
 * only depend on parameters and input dimensions and not on other
3587
 * output dimensions.
3588
 */
3589
__isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3590
114
{
3591
114
  struct vars *v;
3592
114
  isl_set *dom = NULL;
3593
114
  isl_multi_pw_aff *tuple = NULL;
3594
114
  int dim, i, n;
3595
114
  isl_space *space, *dom_space;
3596
114
  isl_multi_aff *ma = NULL;
3597
114
3598
114
  v = vars_new(s->ctx);
3599
114
  if (!v)
3600
0
    return NULL;
3601
114
3602
114
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3603
114
  if (next_is_tuple(s)) {
3604
6
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3605
6
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3606
0
      goto error;
3607
114
  }
3608
114
  if (!isl_set_plain_is_universe(dom))
3609
114
    
isl_die0
(s->ctx, isl_error_invalid,
3610
114
      "expecting universe parameter domain", goto error);
3611
114
  if (isl_stream_eat(s, '{'))
3612
0
    goto error;
3613
114
3614
114
  tuple = read_tuple(s, v, 0, 0);
3615
114
  if (!tuple)
3616
0
    goto error;
3617
114
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3618
107
    isl_set *set;
3619
107
    isl_space *space;
3620
107
    int has_expr;
3621
107
3622
107
    has_expr = tuple_has_expr(tuple);
3623
107
    if (has_expr < 0)
3624
0
      goto error;
3625
107
    if (has_expr)
3626
107
      
isl_die0
(s->ctx, isl_error_invalid,
3627
107
        "expecting universe domain", goto error);
3628
107
    space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3629
107
    set = isl_set_universe(space);
3630
107
    dom = isl_set_intersect_params(set, dom);
3631
107
    isl_multi_pw_aff_free(tuple);
3632
107
    tuple = read_tuple(s, v, 0, 0);
3633
107
    if (!tuple)
3634
0
      goto error;
3635
114
  }
3636
114
3637
114
  if (isl_stream_eat(s, '}'))
3638
0
    goto error;
3639
114
3640
114
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3641
114
  dim = isl_set_dim(dom, isl_dim_all);
3642
114
  dom_space = isl_set_get_space(dom);
3643
114
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3644
114
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3645
114
  if (!isl_space_is_params(dom_space))
3646
107
    space = isl_space_map_from_domain_and_range(
3647
107
        isl_space_copy(dom_space), space);
3648
114
  isl_space_free(dom_space);
3649
114
  ma = isl_multi_aff_alloc(space);
3650
114
3651
240
  for (i = 0; i < n; 
++i126
) {
3652
126
    isl_pw_aff *pa;
3653
126
    isl_aff *aff;
3654
126
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3655
126
    aff = aff_from_pw_aff(pa);
3656
126
    if (!aff)
3657
0
      goto error;
3658
126
    if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3659
0
      isl_aff_free(aff);
3660
0
      isl_die(s->ctx, isl_error_invalid,
3661
0
        "not an affine expression", goto error);
3662
0
    }
3663
126
    aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3664
126
    space = isl_multi_aff_get_domain_space(ma);
3665
126
    aff = isl_aff_reset_domain_space(aff, space);
3666
126
    ma = isl_multi_aff_set_aff(ma, i, aff);
3667
126
  }
3668
114
3669
114
  isl_multi_pw_aff_free(tuple);
3670
114
  vars_free(v);
3671
114
  isl_set_free(dom);
3672
114
  return ma;
3673
0
error:
3674
0
  isl_multi_pw_aff_free(tuple);
3675
0
  vars_free(v);
3676
0
  isl_set_free(dom);
3677
0
  isl_multi_aff_free(ma);
3678
0
  return NULL;
3679
114
}
3680
3681
__isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3682
  const char *str)
3683
52
{
3684
52
  isl_multi_aff *maff;
3685
52
  isl_stream *s = isl_stream_new_str(ctx, str);
3686
52
  if (!s)
3687
0
    return NULL;
3688
52
  maff = isl_stream_read_multi_aff(s);
3689
52
  isl_stream_free(s);
3690
52
  return maff;
3691
52
}
3692
3693
/* Read an isl_multi_pw_aff from "s".
3694
 *
3695
 * The input format is similar to that of map, except that any conditions
3696
 * on the domains should be specified inside the tuple since each
3697
 * piecewise affine expression may have a different domain.
3698
 *
3699
 * Since we do not know in advance if the isl_multi_pw_aff lives
3700
 * in a set or a map space, we first read the first tuple and check
3701
 * if it is followed by a "->".  If so, we convert the tuple into
3702
 * the domain of the isl_multi_pw_aff and read in the next tuple.
3703
 * This tuple (or the first tuple if it was not followed by a "->")
3704
 * is then converted into the isl_multi_pw_aff through a call
3705
 * to extract_mpa_from_tuple and the domain of the result
3706
 * is intersected with the domain.
3707
 */
3708
__isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3709
  __isl_keep isl_stream *s)
3710
3
{
3711
3
  struct vars *v;
3712
3
  isl_set *dom = NULL;
3713
3
  isl_multi_pw_aff *tuple = NULL;
3714
3
  isl_multi_pw_aff *mpa = NULL;
3715
3
3716
3
  v = vars_new(s->ctx);
3717
3
  if (!v)
3718
0
    return NULL;
3719
3
3720
3
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3721
3
  if (next_is_tuple(s)) {
3722
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3723
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3724
0
      goto error;
3725
3
  }
3726
3
  if (isl_stream_eat(s, '{'))
3727
0
    goto error;
3728
3
3729
3
  tuple = read_tuple(s, v, 0, 0);
3730
3
  if (!tuple)
3731
0
    goto error;
3732
3
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3733
3
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3734
3
    dom = isl_map_domain(map);
3735
3
    tuple = read_tuple(s, v, 0, 0);
3736
3
    if (!tuple)
3737
0
      goto error;
3738
3
  }
3739
3
3740
3
  if (isl_stream_eat(s, '}'))
3741
0
    goto error;
3742
3
3743
3
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3744
3
3745
3
  isl_multi_pw_aff_free(tuple);
3746
3
  vars_free(v);
3747
3
  mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3748
3
  return mpa;
3749
0
error:
3750
0
  isl_multi_pw_aff_free(tuple);
3751
0
  vars_free(v);
3752
0
  isl_set_free(dom);
3753
0
  isl_multi_pw_aff_free(mpa);
3754
0
  return NULL;
3755
3
}
3756
3757
/* Read an isl_multi_pw_aff from "str".
3758
 */
3759
__isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3760
  const char *str)
3761
3
{
3762
3
  isl_multi_pw_aff *mpa;
3763
3
  isl_stream *s = isl_stream_new_str(ctx, str);
3764
3
  if (!s)
3765
0
    return NULL;
3766
3
  mpa = isl_stream_read_multi_pw_aff(s);
3767
3
  isl_stream_free(s);
3768
3
  return mpa;
3769
3
}
3770
3771
/* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3772
 */
3773
static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3774
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3775
27
{
3776
27
  isl_pw_aff *pa;
3777
27
  isl_union_pw_aff *upa = NULL;
3778
27
  isl_set *aff_dom;
3779
27
  int n;
3780
27
3781
27
  n = v->n;
3782
27
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3783
27
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3784
27
  vars_drop(v, v->n - n);
3785
27
3786
27
  upa = isl_union_pw_aff_from_pw_aff(pa);
3787
27
3788
31
  while (isl_stream_eat_if_available(s, ';')) {
3789
4
    isl_pw_aff *pa_i;
3790
4
    isl_union_pw_aff *upa_i;
3791
4
3792
4
    n = v->n;
3793
4
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3794
4
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3795
4
    vars_drop(v, v->n - n);
3796
4
3797
4
    upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3798
4
    upa = isl_union_pw_aff_union_add(upa, upa_i);
3799
4
  }
3800
27
3801
27
  isl_set_free(dom);
3802
27
  return upa;
3803
27
}
3804
3805
/* Read an isl_union_pw_aff from "s".
3806
 *
3807
 * First check if there are any paramters, then read in the opening brace
3808
 * and use read_union_pw_aff_with_dom to read in the body of
3809
 * the isl_union_pw_aff.  Finally, read the closing brace.
3810
 */
3811
__isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3812
  __isl_keep isl_stream *s)
3813
0
{
3814
0
  struct vars *v;
3815
0
  isl_set *dom;
3816
0
  isl_union_pw_aff *upa = NULL;
3817
0
3818
0
  v = vars_new(s->ctx);
3819
0
  if (!v)
3820
0
    return NULL;
3821
0
3822
0
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3823
0
  if (next_is_tuple(s)) {
3824
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3825
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3826
0
      goto error;
3827
0
  }
3828
0
  if (isl_stream_eat(s, '{'))
3829
0
    goto error;
3830
0
3831
0
  upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3832
0
3833
0
  if (isl_stream_eat(s, '}'))
3834
0
    goto error;
3835
0
3836
0
  vars_free(v);
3837
0
  isl_set_free(dom);
3838
0
  return upa;
3839
0
error:
3840
0
  vars_free(v);
3841
0
  isl_set_free(dom);
3842
0
  isl_union_pw_aff_free(upa);
3843
0
  return NULL;
3844
0
}
3845
3846
/* Read an isl_union_pw_aff from "str".
3847
 */
3848
__isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3849
  const char *str)
3850
0
{
3851
0
  isl_union_pw_aff *upa;
3852
0
  isl_stream *s = isl_stream_new_str(ctx, str);
3853
0
  if (!s)
3854
0
    return NULL;
3855
0
  upa = isl_stream_read_union_pw_aff(s);
3856
0
  isl_stream_free(s);
3857
0
  return upa;
3858
0
}
3859
3860
/* This function is called for each element in a tuple inside
3861
 * isl_stream_read_multi_union_pw_aff.
3862
 *
3863
 * Read a '{', the union piecewise affine expression body and a '}' and
3864
 * add the isl_union_pw_aff to *list.
3865
 */
3866
static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3867
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3868
27
{
3869
27
  isl_set *dom;
3870
27
  isl_union_pw_aff *upa;
3871
27
  isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3872
27
3873
27
  dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3874
27
  if (isl_stream_eat(s, '{'))
3875
0
    goto error;
3876
27
  upa = read_union_pw_aff_with_dom(s, dom, v);
3877
27
  *list = isl_union_pw_aff_list_add(*list, upa);
3878
27
  if (isl_stream_eat(s, '}'))
3879
0
    return isl_space_free(space);
3880
27
  if (!*list)
3881
0
    return isl_space_free(space);
3882
27
  return space;
3883
0
error:
3884
0
  isl_set_free(dom);
3885
0
  return isl_space_free(space);
3886
27
}
3887
3888
/* Do the next tokens in "s" correspond to an empty tuple?
3889
 * In particular, does the stream start with a '[', followed by a ']',
3890
 * not followed by a "->"?
3891
 */
3892
static int next_is_empty_tuple(__isl_keep isl_stream *s)
3893
15
{
3894
15
  struct isl_token *tok, *tok2, *tok3;
3895
15
  int is_empty_tuple = 0;
3896
15
3897
15
  tok = isl_stream_next_token(s);
3898
15
  if (!tok)
3899
0
    return 0;
3900
15
  if (tok->type != '[') {
3901
0
    isl_stream_push_token(s, tok);
3902
0
    return 0;
3903
0
  }
3904
15
3905
15
  tok2 = isl_stream_next_token(s);
3906
15
  if (tok2 && tok2->type == ']') {
3907
0
    tok3 = isl_stream_next_token(s);
3908
0
    is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3909
0
    if (tok3)
3910
0
      isl_stream_push_token(s, tok3);
3911
0
  }
3912
15
  if (tok2)
3913
15
    isl_stream_push_token(s, tok2);
3914
15
  isl_stream_push_token(s, tok);
3915
15
3916
15
  return is_empty_tuple;
3917
15
}
3918
3919
/* Do the next tokens in "s" correspond to a tuple of parameters?
3920
 * In particular, does the stream start with a '[' that is not
3921
 * followed by a '{' or a nested tuple?
3922
 */
3923
static int next_is_param_tuple(__isl_keep isl_stream *s)
3924
15
{
3925
15
  struct isl_token *tok, *tok2;
3926
15
  int is_tuple;
3927
15
3928
15
  tok = isl_stream_next_token(s);
3929
15
  if (!tok)
3930
0
    return 0;
3931
15
  if (tok->type != '[' || next_is_tuple(s)) {
3932
0
    isl_stream_push_token(s, tok);
3933
0
    return 0;
3934
0
  }
3935
15
3936
15
  tok2 = isl_stream_next_token(s);
3937
15
  is_tuple = tok2 && tok2->type != '{';
3938
15
  if (tok2)
3939
15
    isl_stream_push_token(s, tok2);
3940
15
  isl_stream_push_token(s, tok);
3941
15
3942
15
  return is_tuple;
3943
15
}
3944
3945
/* Read an isl_multi_union_pw_aff from "s".
3946
 *
3947
 * The input has the form
3948
 *
3949
 *  [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3950
 *
3951
 * or
3952
 *
3953
 *  [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3954
 *
3955
 * We first check for the special case of an empty tuple "[]".
3956
 * Then we check if there are any parameters.
3957
 * Finally, we read the tuple, collecting the individual isl_union_pw_aff
3958
 * elements in a list and construct the result from the tuple space and
3959
 * the list.
3960
 */
3961
__isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
3962
  __isl_keep isl_stream *s)
3963
15
{
3964
15
  struct vars *v;
3965
15
  isl_set *dom = NULL;
3966
15
  isl_space *space;
3967
15
  isl_multi_union_pw_aff *mupa = NULL;
3968
15
  isl_union_pw_aff_list *list;
3969
15
3970
15
  if (next_is_empty_tuple(s)) {
3971
0
    if (isl_stream_eat(s, '['))
3972
0
      return NULL;
3973
0
    if (isl_stream_eat(s, ']'))
3974
0
      return NULL;
3975
0
    space = isl_space_set_alloc(s->ctx, 0, 0);
3976
0
    return isl_multi_union_pw_aff_zero(space);
3977
0
  }
3978
15
3979
15
  v = vars_new(s->ctx);
3980
15
  if (!v)
3981
0
    return NULL;
3982
15
3983
15
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3984
15
  if (next_is_param_tuple(s)) {
3985
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3986
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3987
0
      goto error;
3988
15
  }
3989
15
  space = isl_set_get_space(dom);
3990
15
  isl_set_free(dom);
3991
15
  list = isl_union_pw_aff_list_alloc(s->ctx, 0);
3992
15
  space = read_tuple_space(s, v, space, 1, 0,
3993
15
        &read_union_pw_aff_el, &list);
3994
15
  mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
3995
15
3996
15
  vars_free(v);
3997
15
3998
15
  return mupa;
3999
0
error:
4000
0
  vars_free(v);
4001
0
  isl_set_free(dom);
4002
0
  isl_multi_union_pw_aff_free(mupa);
4003
0
  return NULL;
4004
15
}
4005
4006
/* Read an isl_multi_union_pw_aff from "str".
4007
 */
4008
__isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4009
  isl_ctx *ctx, const char *str)
4010
15
{
4011
15
  isl_multi_union_pw_aff *mupa;
4012
15
  isl_stream *s = isl_stream_new_str(ctx, str);
4013
15
  if (!s)
4014
0
    return NULL;
4015
15
  mupa = isl_stream_read_multi_union_pw_aff(s);
4016
15
  isl_stream_free(s);
4017
15
  return mupa;
4018
15
}
4019
4020
__isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4021
  __isl_keep isl_stream *s)
4022
2
{
4023
2
  struct isl_obj obj;
4024
2
4025
2
  obj = obj_read(s);
4026
2
  if (obj.type == isl_obj_pw_qpolynomial) {
4027
1
    obj.type = isl_obj_union_pw_qpolynomial;
4028
1
    obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4029
1
  }
4030
2
  if (obj.v)
4031
2
    isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4032
2
         goto error);
4033
2
4034
2
  return obj.v;
4035
0
error:
4036
0
  obj.type->free(obj.v);
4037
0
  return NULL;
4038
2
}
4039
4040
__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4041
  isl_ctx *ctx, const char *str)
4042
2
{
4043
2
  isl_union_pw_qpolynomial *upwqp;
4044
2
  isl_stream *s = isl_stream_new_str(ctx, str);
4045
2
  if (!s)
4046
0
    return NULL;
4047
2
  upwqp = isl_stream_read_union_pw_qpolynomial(s);
4048
2
  isl_stream_free(s);
4049
2
  return upwqp;
4050
2
}