Coverage Report

Created: 2018-04-24 22:41

/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
3.11k
{
47
3.11k
  struct vars *v;
48
3.11k
  v = isl_alloc_type(ctx, struct vars);
49
3.11k
  if (!v)
50
0
    return NULL;
51
3.11k
  v->ctx = ctx;
52
3.11k
  v->n = 0;
53
3.11k
  v->v = NULL;
54
3.11k
  return v;
55
3.11k
}
56
57
static void variable_free(struct variable *var)
58
3.11k
{
59
4.63k
  while (var) {
60
1.52k
    struct variable *next = var->next;
61
1.52k
    free(var->name);
62
1.52k
    free(var);
63
1.52k
    var = next;
64
1.52k
  }
65
3.11k
}
66
67
static void vars_free(struct vars *v)
68
3.11k
{
69
3.11k
  if (!v)
70
0
    return;
71
3.11k
  variable_free(v->v);
72
3.11k
  free(v);
73
3.11k
}
74
75
static void vars_drop(struct vars *v, int n)
76
3.44k
{
77
3.44k
  struct variable *var;
78
3.44k
79
3.44k
  if (!v || !v->v)
80
209
    return;
81
3.23k
82
3.23k
  v->n -= n;
83
3.23k
84
3.23k
  var = v->v;
85
10.5k
  while (--n >= 0) {
86
7.33k
    struct variable *next = var->next;
87
7.33k
    free(var->name);
88
7.33k
    free(var);
89
7.33k
    var = next;
90
7.33k
  }
91
3.23k
  v->v = var;
92
3.23k
}
93
94
static struct variable *variable_new(struct vars *v, const char *name, int len,
95
        int pos)
96
8.85k
{
97
8.85k
  struct variable *var;
98
8.85k
  var = isl_calloc_type(v->ctx, struct variable);
99
8.85k
  if (!var)
100
0
    goto error;
101
8.85k
  var->name = strdup(name);
102
8.85k
  var->name[len] = '\0';
103
8.85k
  var->pos = pos;
104
8.85k
  var->next = v->v;
105
8.85k
  return var;
106
0
error:
107
0
  variable_free(v->v);
108
0
  return NULL;
109
8.85k
}
110
111
static int vars_pos(struct vars *v, const char *s, int len)
112
15.6k
{
113
15.6k
  int pos;
114
15.6k
  struct variable *q;
115
15.6k
116
15.6k
  if (len == -1)
117
15.6k
    len = strlen(s);
118
47.6k
  for (q = v->v; q; 
q = q->next31.9k
) {
119
41.9k
    if (strncmp(q->name, s, len) == 0 && 
q->name[len] == '\0'10.3k
)
120
10.0k
      break;
121
41.9k
  }
122
15.6k
  if (q)
123
10.0k
    pos = q->pos;
124
5.63k
  else {
125
5.63k
    pos = v->n;
126
5.63k
    v->v = variable_new(v, s, len, v->n);
127
5.63k
    if (!v->v)
128
0
      return -1;
129
5.63k
    v->n++;
130
5.63k
  }
131
15.6k
  return pos;
132
15.6k
}
133
134
static int vars_add_anon(struct vars *v)
135
3.22k
{
136
3.22k
  v->v = variable_new(v, "", 0, v->n);
137
3.22k
138
3.22k
  if (!v->v)
139
0
    return -1;
140
3.22k
  v->n++;
141
3.22k
142
3.22k
  return 0;
143
3.22k
}
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
53.2k
{
151
53.2k
  struct isl_token *tok, *tok2;
152
53.2k
153
53.2k
  tok = isl_stream_next_token(s);
154
53.2k
  if (!tok || tok->type != ISL_TOKEN_VALUE)
155
43.1k
    return tok;
156
10.0k
  if (!isl_stream_eat_if_available(s, '^'))
157
10.0k
    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
0
}
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
365
{
188
365
  struct isl_token *tok = NULL;
189
365
  struct isl_token *tok2 = NULL;
190
365
  isl_val *val;
191
365
192
365
  tok = next_token(s);
193
365
  if (!tok) {
194
0
    isl_stream_error(s, NULL, "unexpected EOF");
195
0
    goto error;
196
0
  }
197
365
  if (tok->type == ISL_TOKEN_INFTY) {
198
50
    isl_token_free(tok);
199
50
    return isl_val_infty(s->ctx);
200
50
  }
201
315
  if (tok->type == '-' &&
202
315
      
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
283
  if (tok->type == ISL_TOKEN_NAN) {
207
52
    isl_token_free(tok);
208
52
    return isl_val_nan(s->ctx);
209
52
  }
210
231
  if (tok->type != ISL_TOKEN_VALUE) {
211
0
    isl_stream_error(s, tok, "expecting value");
212
0
    goto error;
213
0
  }
214
231
215
231
  if (isl_stream_eat_if_available(s, '/')) {
216
54
    tok2 = next_token(s);
217
54
    if (!tok2) {
218
0
      isl_stream_error(s, NULL, "unexpected EOF");
219
0
      goto error;
220
0
    }
221
54
    if (tok2->type != ISL_TOKEN_VALUE) {
222
0
      isl_stream_error(s, tok2, "expecting value");
223
0
      goto error;
224
0
    }
225
54
    val = isl_val_rat_from_isl_int(s->ctx, tok->u.v, tok2->u.v);
226
54
    val = isl_val_normalize(val);
227
177
  } else {
228
177
    val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
229
177
  }
230
231
231
231
  isl_token_free(tok);
232
231
  isl_token_free(tok2);
233
231
  return val;
234
0
error:
235
0
  isl_token_free(tok);
236
0
  isl_token_free(tok2);
237
0
  return NULL;
238
231
}
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
348
{
245
348
  isl_val *val;
246
348
  isl_stream *s = isl_stream_new_str(ctx, str);
247
348
  if (!s)
248
0
    return NULL;
249
348
  val = isl_stream_read_val(s);
250
348
  isl_stream_free(s);
251
348
  return val;
252
348
}
253
254
static int accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
255
365
{
256
365
  struct isl_token *tok;
257
365
258
365
  tok = next_token(s);
259
365
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
260
0
    isl_stream_error(s, tok, "expecting constant value");
261
0
    goto error;
262
0
  }
263
365
264
365
  isl_int_mul(*f, *f, tok->u.v);
265
365
266
365
  isl_token_free(tok);
267
365
268
365
  if (isl_stream_eat_if_available(s, '*'))
269
0
    return accept_cst_factor(s, f);
270
365
271
365
  return 0;
272
0
error:
273
0
  isl_token_free(tok);
274
0
  return -1;
275
365
}
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
118
{
287
118
  struct isl_token *tok;
288
118
  isl_pw_aff *q;
289
118
290
118
  tok = next_token(s);
291
118
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
292
0
    isl_stream_error(s, tok, "expecting constant value");
293
0
    goto error;
294
0
  }
295
118
296
118
  q = isl_pw_aff_copy(aff);
297
118
  q = isl_pw_aff_scale_down(q, tok->u.v);
298
118
  q = isl_pw_aff_floor(q);
299
118
  q = isl_pw_aff_scale(q, tok->u.v);
300
118
301
118
  aff = isl_pw_aff_sub(aff, q);
302
118
303
118
  isl_token_free(tok);
304
118
  return aff;
305
0
error:
306
0
  isl_pw_aff_free(aff);
307
0
  isl_token_free(tok);
308
0
  return NULL;
309
118
}
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
15.7k
{
351
15.7k
  if (!tok)
352
0
    return 0;
353
15.7k
  if (tok->type == '[')
354
272
    return 1;
355
15.4k
  if (tok->type == ISL_TOKEN_FLOOR)
356
269
    return 1;
357
15.2k
  if (tok->type == ISL_TOKEN_CEIL)
358
0
    return 1;
359
15.2k
  if (tok->type == ISL_TOKEN_FLOORD)
360
2
    return 1;
361
15.2k
  if (tok->type == ISL_TOKEN_CEILD)
362
0
    return 1;
363
15.2k
  return 0;
364
15.2k
}
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
337
{
379
337
  struct isl_token *tok;
380
337
  int f = 0;
381
337
  int c = 0;
382
337
  int extra = 0;
383
337
  isl_pw_aff *pwaff = NULL;
384
337
385
337
  if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
386
1
    extra = f = 1;
387
336
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEILD))
388
0
    extra = c = 1;
389
336
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR))
390
177
    f = 1;
391
159
  else if (isl_stream_eat_if_available(s, ISL_TOKEN_CEIL))
392
0
    c = 1;
393
337
  if (f || 
c159
) {
394
178
    if (isl_stream_eat(s, '('))
395
0
      goto error;
396
159
  } else {
397
159
    if (isl_stream_eat(s, '['))
398
0
      goto error;
399
337
  }
400
337
401
337
  pwaff = accept_affine(s, isl_space_copy(dim), v);
402
337
403
337
  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
337
419
337
  if (c)
420
0
    pwaff = isl_pw_aff_ceil(pwaff);
421
337
  else
422
337
    pwaff = isl_pw_aff_floor(pwaff);
423
337
424
337
  if (f || 
c159
) {
425
178
    if (isl_stream_eat(s, ')'))
426
0
      goto error;
427
159
  } else {
428
159
    if (isl_stream_eat(s, ']'))
429
0
      goto error;
430
337
  }
431
337
432
337
  isl_space_free(dim);
433
337
  return pwaff;
434
0
error:
435
0
  isl_space_free(dim);
436
0
  isl_pw_aff_free(pwaff);
437
0
  return NULL;
438
337
}
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
9.59k
{
443
9.59k
  struct isl_token *tok = NULL;
444
9.59k
  isl_pw_aff *res = NULL;
445
9.59k
446
9.59k
  tok = next_token(s);
447
9.59k
  if (!tok) {
448
0
    isl_stream_error(s, NULL, "unexpected EOF");
449
0
    goto error;
450
0
  }
451
9.59k
452
9.59k
  if (tok->type == ISL_TOKEN_AFF) {
453
270
    res = isl_pw_aff_copy(tok->u.pwaff);
454
270
    isl_token_free(tok);
455
9.32k
  } else if (tok->type == ISL_TOKEN_IDENT) {
456
8.77k
    int n = v->n;
457
8.77k
    int pos = vars_pos(v, tok->u.s, -1);
458
8.77k
    isl_aff *aff;
459
8.77k
460
8.77k
    if (pos < 0)
461
0
      goto error;
462
8.77k
    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
8.77k
468
8.77k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
469
8.77k
    if (!aff)
470
0
      goto error;
471
8.77k
    isl_int_set_si(aff->v->el[2 + pos], 1);
472
8.77k
    res = isl_pw_aff_from_aff(aff);
473
8.77k
    isl_token_free(tok);
474
8.77k
  } else 
if (556
tok->type == ISL_TOKEN_VALUE556
) {
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
556
  } else if (tok->type == '(') {
488
242
    isl_token_free(tok);
489
242
    tok = NULL;
490
242
    res = accept_affine(s, isl_space_copy(dim), v);
491
242
    if (!res)
492
0
      goto error;
493
242
    if (isl_stream_eat(s, ')'))
494
0
      goto error;
495
314
  } else if (is_start_of_div(tok)) {
496
308
    isl_stream_push_token(s, tok);
497
308
    tok = NULL;
498
308
    res = accept_div(s, isl_space_copy(dim), v);
499
308
  } 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
9.59k
  if (isl_stream_eat_if_available(s, '%') ||
508
9.59k
      
isl_stream_eat_if_available(s, ISL_TOKEN_MOD)9.55k
) {
509
118
    isl_space_free(dim);
510
118
    return affine_mod(s, v, res);
511
118
  }
512
9.47k
  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
9.47k
  if (isl_stream_eat_if_available(s, '/')) {
524
358
    isl_int f;
525
358
    isl_int_init(f);
526
358
    isl_int_set_si(f, 1);
527
358
    if (accept_cst_factor(s, &f) < 0) {
528
0
      isl_int_clear(f);
529
0
      goto error2;
530
0
    }
531
358
    res = isl_pw_aff_scale_down(res, f);
532
358
    isl_int_clear(f);
533
358
  }
534
9.47k
535
9.47k
  isl_space_free(dim);
536
9.47k
  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
0
}
544
545
static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
546
6.02k
{
547
6.02k
  isl_aff *aff;
548
6.02k
  isl_space *space;
549
6.02k
550
6.02k
  space = isl_pw_aff_get_domain_space(pwaff);
551
6.02k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
552
6.02k
  aff = isl_aff_add_constant(aff, v);
553
6.02k
554
6.02k
  return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
555
6.02k
}
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
13.5k
{
571
13.5k
  struct isl_token *tok = NULL;
572
13.5k
  isl_local_space *ls;
573
13.5k
  isl_pw_aff *res;
574
13.5k
  int sign = 1;
575
13.5k
576
13.5k
  ls = isl_local_space_from_space(isl_space_copy(space));
577
13.5k
  res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
578
13.5k
  if (!res)
579
0
    goto error;
580
13.5k
581
15.7k
  
for (;;)13.5k
{
582
15.7k
    tok = next_token(s);
583
15.7k
    if (!tok) {
584
0
      isl_stream_error(s, NULL, "unexpected EOF");
585
0
      goto error;
586
0
    }
587
15.7k
    if (tok->type == '-') {
588
105
      sign = -sign;
589
105
      isl_token_free(tok);
590
105
      continue;
591
105
    }
592
15.6k
    if (tok->type == '(' || 
is_start_of_div(tok)15.4k
||
593
15.6k
        
tok->type == ISL_TOKEN_MIN15.2k
||
tok->type == ISL_TOKEN_MAX15.1k
||
594
15.6k
        
tok->type == ISL_TOKEN_IDENT15.1k
||
595
15.6k
        
tok->type == ISL_TOKEN_AFF7.66k
) {
596
8.25k
      isl_pw_aff *term;
597
8.25k
      isl_stream_push_token(s, tok);
598
8.25k
      tok = NULL;
599
8.25k
      term = accept_affine_factor(s,
600
8.25k
                isl_space_copy(space), v);
601
8.25k
      if (sign < 0)
602
445
        res = isl_pw_aff_sub(res, term);
603
7.81k
      else
604
7.81k
        res = isl_pw_aff_add(res, term);
605
8.25k
      if (!res)
606
0
        goto error;
607
8.25k
      sign = 1;
608
8.25k
    } else 
if (7.39k
tok->type == ISL_TOKEN_VALUE7.39k
) {
609
7.35k
      if (sign < 0)
610
7.35k
        
isl_int_neg242
(tok->u.v, tok->u.v);
611
7.35k
      if (isl_stream_eat_if_available(s, '*') ||
612
7.35k
          
isl_stream_next_token_is(s, ISL_TOKEN_IDENT)7.24k
) {
613
1.33k
        isl_pw_aff *term;
614
1.33k
        term = accept_affine_factor(s,
615
1.33k
                isl_space_copy(space), v);
616
1.33k
        term = isl_pw_aff_scale(term, tok->u.v);
617
1.33k
        res = isl_pw_aff_add(res, term);
618
1.33k
        if (!res)
619
0
          goto error;
620
6.02k
      } else {
621
6.02k
        res = add_cst(res, tok->u.v);
622
6.02k
      }
623
7.35k
      sign = 1;
624
7.35k
    } 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
15.6k
    isl_token_free(tok);
634
15.6k
635
15.6k
    tok = next_token(s);
636
15.6k
    if (tok && tok->type == '-') {
637
582
      sign = -sign;
638
582
      isl_token_free(tok);
639
15.0k
    } else if (tok && tok->type == '+') {
640
1.52k
      /* nothing */
641
1.52k
      isl_token_free(tok);
642
13.5k
    } else if (tok && tok->type == ISL_TOKEN_VALUE &&
643
13.5k
         
isl_int_is_neg37
(tok->u.v)) {
644
37
      isl_stream_push_token(s, tok);
645
13.5k
    } else {
646
13.5k
      if (tok)
647
13.5k
        isl_stream_push_token(s, tok);
648
13.5k
      break;
649
13.5k
    }
650
15.6k
  }
651
13.5k
652
13.5k
  isl_space_free(space);
653
13.5k
  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
13.5k
}
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
21.8k
{
666
21.8k
  switch (type) {
667
21.8k
  case ISL_TOKEN_LEX_LT:
668
18
  case ISL_TOKEN_LEX_GT:
669
18
  case ISL_TOKEN_LEX_LE:
670
18
  case ISL_TOKEN_LEX_GE:
671
18
    return 1;
672
21.8k
  default:
673
21.8k
    return 0;
674
0
  }
675
0
}
676
677
static int is_comparator(struct isl_token *tok)
678
12.3k
{
679
12.3k
  if (!tok)
680
0
    return 0;
681
12.3k
  if (is_list_comparator_type(tok->type))
682
6
    return 1;
683
12.3k
684
12.3k
  switch (tok->type) {
685
12.3k
  case ISL_TOKEN_LT:
686
4.77k
  case ISL_TOKEN_GT:
687
4.77k
  case ISL_TOKEN_LE:
688
4.77k
  case ISL_TOKEN_GE:
689
4.77k
  case ISL_TOKEN_NE:
690
4.77k
  case '=':
691
4.77k
    return 1;
692
7.54k
  default:
693
7.54k
    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.84k
{
742
6.84k
  struct isl_token *tok;
743
6.84k
744
6.84k
  tok = isl_stream_next_token(s);
745
6.84k
  if (!tok)
746
0
    return;
747
6.84k
748
6.84k
  *line = tok->line;
749
6.84k
  *col = tok->col;
750
6.84k
  isl_stream_push_token(s, tok);
751
6.84k
}
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
123
{
759
123
  struct isl_token *tok;
760
123
761
123
  tok = isl_token_new(s->ctx, line, col, 0);
762
123
  if (!tok)
763
0
    goto error;
764
123
  tok->type = ISL_TOKEN_AFF;
765
123
  tok->u.pwaff = pa;
766
123
  isl_stream_push_token(s, tok);
767
123
768
123
  return 0;
769
0
error:
770
0
  isl_pw_aff_free(pa);
771
0
  return -1;
772
123
}
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.49k
{
783
3.49k
  isl_space *space;
784
3.49k
  isl_map *cond;
785
3.49k
  isl_pw_aff *pwaff;
786
3.49k
  struct isl_token *tok;
787
3.49k
  int line = -1, col = -1;
788
3.49k
  int is_comp;
789
3.49k
790
3.49k
  set_current_line_col(s, &line, &col);
791
3.49k
792
3.49k
  pwaff = accept_affine(s, dim, v);
793
3.49k
  if (rational)
794
4
    pwaff = isl_pw_aff_set_rational(pwaff);
795
3.49k
  if (!pwaff)
796
0
    return NULL;
797
3.49k
798
3.49k
  tok = isl_stream_next_token(s);
799
3.49k
  if (!tok)
800
0
    return isl_pw_aff_free(pwaff);
801
3.49k
802
3.49k
  is_comp = is_comparator(tok);
803
3.49k
  isl_stream_push_token(s, tok);
804
3.49k
  if (!is_comp)
805
3.48k
    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
130
{
824
130
  isl_pw_aff *def;
825
130
  int pos;
826
130
  isl_map *def_map;
827
130
828
130
  if (type == isl_dim_param)
829
0
    pos = isl_map_dim(map, isl_dim_param);
830
130
  else {
831
130
    pos = isl_map_dim(map, isl_dim_in);
832
130
    if (type == isl_dim_out)
833
130
      pos += isl_map_dim(map, isl_dim_out);
834
130
    type = isl_dim_in;
835
130
  }
836
130
  --pos;
837
130
838
130
  def = accept_extended_affine(s, isl_space_wrap(isl_map_get_space(map)),
839
130
          v, rational);
840
130
  def_map = isl_map_from_pw_aff(def);
841
130
  def_map = isl_map_equate(def_map, type, pos, isl_dim_out, 0);
842
130
  def_map = isl_set_unwrap(isl_map_domain(def_map));
843
130
844
130
  map = isl_map_intersect(map, def_map);
845
130
846
130
  return map;
847
130
}
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
8.84k
{
852
8.84k
  isl_pw_aff *pwaff;
853
8.84k
  isl_pw_aff_list *list;
854
8.84k
  struct isl_token *tok = NULL;
855
8.84k
856
8.84k
  pwaff = accept_affine(s, isl_space_copy(dim), v);
857
8.84k
  list = isl_pw_aff_list_from_pw_aff(pwaff);
858
8.84k
  if (!list)
859
1
    goto error;
860
8.84k
861
8.99k
  
for (;;)8.84k
{
862
8.99k
    tok = isl_stream_next_token(s);
863
8.99k
    if (!tok) {
864
0
      isl_stream_error(s, NULL, "unexpected EOF");
865
0
      goto error;
866
0
    }
867
8.99k
    if (tok->type != ',') {
868
8.84k
      isl_stream_push_token(s, tok);
869
8.84k
      break;
870
8.84k
    }
871
152
    isl_token_free(tok);
872
152
873
152
    pwaff = accept_affine(s, isl_space_copy(dim), v);
874
152
    list = isl_pw_aff_list_concat(list,
875
152
        isl_pw_aff_list_from_pw_aff(pwaff));
876
152
    if (!list)
877
0
      goto error;
878
152
  }
879
8.84k
880
8.84k
  isl_space_free(dim);
881
8.84k
  return list;
882
1
error:
883
1
  isl_space_free(dim);
884
1
  isl_pw_aff_list_free(list);
885
1
  return NULL;
886
8.84k
}
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
134
{
891
134
  struct isl_token *tok;
892
134
893
220
  while ((tok = isl_stream_next_token(s)) != NULL) {
894
220
    int p;
895
220
    int n = v->n;
896
220
897
220
    if (tok->type != ISL_TOKEN_IDENT)
898
0
      break;
899
220
900
220
    p = vars_pos(v, tok->u.s, -1);
901
220
    if (p < 0)
902
0
      goto error;
903
220
    if (p < n) {
904
0
      isl_stream_error(s, tok, "expecting unique identifier");
905
0
      goto error;
906
0
    }
907
220
908
220
    map = isl_map_add_dims(map, isl_dim_out, 1);
909
220
910
220
    isl_token_free(tok);
911
220
    tok = isl_stream_next_token(s);
912
220
    if (tok && tok->type == '=') {
913
130
      isl_token_free(tok);
914
130
      map = read_var_def(s, map, isl_dim_out, v, rational);
915
130
      tok = isl_stream_next_token(s);
916
130
    }
917
220
918
220
    if (!tok || tok->type != ',')
919
134
      break;
920
86
921
86
    isl_token_free(tok);
922
86
  }
923
134
  if (tok)
924
134
    isl_stream_push_token(s, tok);
925
134
926
134
  return map;
927
0
error:
928
0
  isl_token_free(tok);
929
0
  isl_map_free(map);
930
0
  return NULL;
931
134
}
932
933
static int next_is_tuple(__isl_keep isl_stream *s)
934
12.3k
{
935
12.3k
  struct isl_token *tok;
936
12.3k
  int is_tuple;
937
12.3k
938
12.3k
  tok = isl_stream_next_token(s);
939
12.3k
  if (!tok)
940
0
    return 0;
941
12.3k
  if (tok->type == '[') {
942
2.13k
    isl_stream_push_token(s, tok);
943
2.13k
    return 1;
944
2.13k
  }
945
10.2k
  if (tok->type != ISL_TOKEN_IDENT && 
!tok->is_keyword3.03k
) {
946
2.99k
    isl_stream_push_token(s, tok);
947
2.99k
    return 0;
948
2.99k
  }
949
7.26k
950
7.26k
  is_tuple = isl_stream_next_token_is(s, '[');
951
7.26k
952
7.26k
  isl_stream_push_token(s, tok);
953
7.26k
954
7.26k
  return is_tuple;
955
7.26k
}
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
128
{
966
128
  isl_aff *aff;
967
128
968
128
  if (!pa)
969
0
    return -1;
970
128
  if (pa->n != 1)
971
0
    return 1;
972
128
  if (!isl_set_plain_is_universe(pa->p[0].set))
973
0
    return 1;
974
128
975
128
  aff = pa->p[0].aff;
976
128
  if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
977
128
    
return 10
;
978
128
  return 0;
979
128
}
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
116
{
986
116
  int i, n;
987
116
  int has_expr = 0;
988
116
  isl_pw_aff *pa;
989
116
990
116
  if (!tuple)
991
0
    return -1;
992
116
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
993
244
  for (i = 0; i < n; 
++i128
) {
994
128
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
995
128
    has_expr = pw_aff_is_expr(pa, i, n);
996
128
    isl_pw_aff_free(pa);
997
128
    if (has_expr < 0 || has_expr)
998
0
      break;
999
128
  }
1000
116
1001
116
  return has_expr;
1002
116
}
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
5.36k
{
1012
5.36k
  char *prime;
1013
5.36k
1014
5.36k
  if (!name)
1015
0
    return space;
1016
5.36k
1017
5.36k
  prime = strchr(name, '\'');
1018
5.36k
  if (prime)
1019
82
    *prime = '\0';
1020
5.36k
  space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1021
5.36k
  if (prime)
1022
82
    *prime = '\'';
1023
5.36k
1024
5.36k
  return space;
1025
5.36k
}
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.35k
{
1051
3.35k
  isl_pw_aff *res;
1052
3.35k
  isl_space *res_space;
1053
3.35k
1054
3.35k
  res_space = isl_space_from_domain(isl_space_copy(space));
1055
3.35k
  res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1056
3.35k
  res = isl_pw_aff_empty(res_space);
1057
3.35k
  do {
1058
3.35k
    isl_pw_aff *pa;
1059
3.35k
    int seen_paren;
1060
3.35k
    int line = -1, col = -1;
1061
3.35k
1062
3.35k
    set_current_line_col(s, &line, &col);
1063
3.35k
    seen_paren = isl_stream_eat_if_available(s, '(');
1064
3.35k
    if (seen_paren)
1065
122
      pa = accept_piecewise_affine(s, isl_space_copy(space),
1066
122
              v, rational);
1067
3.23k
    else
1068
3.23k
      pa = accept_extended_affine(s, isl_space_copy(space),
1069
3.23k
              v, rational);
1070
3.35k
    if (seen_paren && 
isl_stream_eat_if_available(s, ')')122
) {
1071
122
      seen_paren = 0;
1072
122
      if (push_aff(s, line, col, pa) < 0)
1073
0
        goto error;
1074
122
      pa = accept_extended_affine(s, isl_space_copy(space),
1075
122
              v, rational);
1076
122
    }
1077
3.35k
    if (isl_stream_eat_if_available(s, ':')) {
1078
81
      isl_space *dom_space;
1079
81
      isl_set *dom;
1080
81
1081
81
      dom_space = isl_pw_aff_get_domain_space(pa);
1082
81
      dom = isl_set_universe(dom_space);
1083
81
      dom = read_formula(s, v, dom, rational);
1084
81
      pa = isl_pw_aff_intersect_domain(pa, dom);
1085
81
    }
1086
3.35k
1087
3.35k
    res = isl_pw_aff_union_add(res, pa);
1088
3.35k
1089
3.35k
    if (seen_paren && 
isl_stream_eat(s, ')')0
)
1090
0
      goto error;
1091
3.35k
  } while (isl_stream_eat_if_available(s, ';'));
1092
3.35k
1093
3.35k
  isl_space_free(space);
1094
3.35k
1095
3.35k
  return res;
1096
0
error:
1097
0
  isl_space_free(space);
1098
0
  return isl_pw_aff_free(res);
1099
3.35k
}
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
3.23k
{
1111
3.23k
  isl_space *space;
1112
3.23k
  isl_pw_aff *def;
1113
3.23k
1114
3.23k
  space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1115
3.23k
1116
3.23k
  def = accept_piecewise_affine(s, space, v, rational);
1117
3.23k
1118
3.23k
  space = isl_space_set_alloc(s->ctx, 0, v->n);
1119
3.23k
  def = isl_pw_aff_reset_domain_space(def, space);
1120
3.23k
1121
3.23k
  return def;
1122
3.23k
}
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
6.85k
{
1137
6.85k
  if (!space)
1138
0
    return NULL;
1139
6.85k
1140
6.85k
  space = isl_space_set_from_params(space);
1141
6.85k
1142
6.85k
  if (isl_stream_next_token_is(s, ']'))
1143
1.30k
    return space;
1144
5.55k
1145
8.74k
  
for (;;)5.55k
{
1146
8.74k
    struct isl_token *tok;
1147
8.74k
1148
8.74k
    space = isl_space_add_dims(space, isl_dim_set, 1);
1149
8.74k
1150
8.74k
    space = read_el(s, v, space, rational, user);
1151
8.74k
    if (!space)
1152
0
      return NULL;
1153
8.74k
1154
8.74k
    tok = isl_stream_next_token(s);
1155
8.74k
    if (!comma && 
tok8.74k
&&
tok->type == ']'8.74k
&&
1156
8.74k
        
isl_stream_next_token_is(s, '[')5.55k
) {
1157
0
      isl_token_free(tok);
1158
0
      tok = isl_stream_next_token(s);
1159
8.74k
    } else if (!tok || tok->type != ',') {
1160
5.55k
      if (tok)
1161
5.55k
        isl_stream_push_token(s, tok);
1162
5.55k
      break;
1163
5.55k
    }
1164
3.19k
1165
3.19k
    isl_token_free(tok);
1166
3.19k
  }
1167
5.55k
1168
5.55k
  return space;
1169
5.55k
}
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
7.12k
{
1181
7.12k
  struct isl_token *tok;
1182
7.12k
  char *name = NULL;
1183
7.12k
  isl_space *res = NULL;
1184
7.12k
1185
7.12k
  tok = isl_stream_next_token(s);
1186
7.12k
  if (!tok)
1187
0
    goto error;
1188
7.12k
  if (tok->type == ISL_TOKEN_IDENT || 
tok->is_keyword3.27k
) {
1189
3.85k
    name = strdup(tok->u.s);
1190
3.85k
    isl_token_free(tok);
1191
3.85k
    if (!name)
1192
0
      goto error;
1193
3.27k
  } else
1194
3.27k
    isl_stream_push_token(s, tok);
1195
7.12k
  if (isl_stream_eat(s, '['))
1196
0
    goto error;
1197
7.12k
  if (next_is_tuple(s)) {
1198
272
    isl_space *out;
1199
272
    res = read_tuple_space(s, v, isl_space_copy(space),
1200
272
          rational, comma, read_el, user);
1201
272
    if (isl_stream_eat(s, ISL_TOKEN_TO))
1202
0
      goto error;
1203
272
    out = read_tuple_space(s, v, isl_space_copy(space),
1204
272
          rational, comma, read_el, user);
1205
272
    res = isl_space_product(res, out);
1206
272
  } else
1207
6.85k
    res = read_tuple_list(s, v, isl_space_copy(space),
1208
6.85k
          rational, comma, read_el, user);
1209
7.12k
  if (isl_stream_eat(s, ']'))
1210
2
    goto error;
1211
7.12k
1212
7.12k
  if (name) {
1213
3.85k
    res = isl_space_set_tuple_name(res, isl_dim_set, name);
1214
3.85k
    free(name);
1215
3.85k
  }
1216
7.12k
1217
7.12k
  isl_space_free(space);
1218
7.12k
  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
7.12k
}
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
5.35k
{
1231
5.35k
  isl_space *space;
1232
5.35k
  isl_aff *aff;
1233
5.35k
1234
5.35k
  space = isl_space_set_alloc(v->ctx, 0, v->n);
1235
5.35k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1236
5.35k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1237
5.35k
  return isl_pw_aff_from_aff(aff);
1238
5.35k
}
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
8.58k
{
1253
8.58k
  isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1254
8.58k
  isl_pw_aff *pa;
1255
8.58k
  struct isl_token *tok;
1256
8.58k
  int new_name = 0;
1257
8.58k
1258
8.58k
  tok = next_token(s);
1259
8.58k
  if (!tok) {
1260
0
    isl_stream_error(s, NULL, "unexpected EOF");
1261
0
    return isl_space_free(space);
1262
0
  }
1263
8.58k
1264
8.58k
  if (tok->type == ISL_TOKEN_IDENT) {
1265
6.61k
    int n = v->n;
1266
6.61k
    int p = vars_pos(v, tok->u.s, -1);
1267
6.61k
    if (p < 0)
1268
0
      goto error;
1269
6.61k
    new_name = p >= n;
1270
6.61k
  }
1271
8.58k
1272
8.58k
  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
8.58k
  } else if (new_name) {
1278
5.36k
    int pos = isl_space_dim(space, isl_dim_out) - 1;
1279
5.36k
    space = space_set_dim_name(space, pos, v->v->name);
1280
5.36k
    isl_token_free(tok);
1281
5.36k
    if (isl_stream_eat_if_available(s, '='))
1282
10
      pa = read_tuple_var_def(s, v, rational);
1283
5.35k
    else
1284
5.35k
      pa = identity_tuple_el(v);
1285
5.36k
  } else {
1286
3.22k
    isl_stream_push_token(s, tok);
1287
3.22k
    tok = NULL;
1288
3.22k
    if (vars_add_anon(v) < 0)
1289
0
      goto error;
1290
3.22k
    pa = read_tuple_var_def(s, v, rational);
1291
3.22k
  }
1292
8.58k
1293
8.58k
  *list = isl_pw_aff_list_add(*list, pa);
1294
8.58k
  if (!*list)
1295
0
    return isl_space_free(space);
1296
8.58k
1297
8.58k
  return space;
1298
0
error:
1299
0
  isl_token_free(tok);
1300
0
  return isl_space_free(space);
1301
8.58k
}
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
6.32k
{
1321
6.32k
  int i, n;
1322
6.32k
  isl_space *space;
1323
6.32k
  isl_pw_aff_list *list;
1324
6.32k
1325
6.32k
  space = isl_space_params_alloc(v->ctx, 0);
1326
6.32k
  list = isl_pw_aff_list_alloc(s->ctx, 0);
1327
6.32k
  space = read_tuple_space(s, v, space, rational, comma,
1328
6.32k
        &read_tuple_pw_aff_el, &list);
1329
6.32k
  n = isl_space_dim(space, isl_dim_set);
1330
9.54k
  for (i = 0; i + 1 < n; 
++i3.22k
) {
1331
3.22k
    isl_pw_aff *pa;
1332
3.22k
1333
3.22k
    pa = isl_pw_aff_list_get_pw_aff(list, i);
1334
3.22k
    pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1335
3.22k
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1336
3.22k
  }
1337
6.32k
1338
6.32k
  space = isl_space_from_range(space);
1339
6.32k
  space = isl_space_add_dims(space, isl_dim_in, v->n);
1340
6.32k
  return isl_multi_pw_aff_from_pw_aff_list(space, list);
1341
6.32k
}
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
5.87k
{
1353
5.87k
  int i, n;
1354
5.87k
  isl_ctx *ctx;
1355
5.87k
  isl_space *space = NULL;
1356
5.87k
1357
5.87k
  if (!map || !tuple)
1358
0
    goto error;
1359
5.87k
  ctx = isl_multi_pw_aff_get_ctx(tuple);
1360
5.87k
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1361
5.87k
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1362
5.87k
  if (!space)
1363
0
    goto error;
1364
5.87k
1365
5.87k
  if (type == isl_dim_param) {
1366
800
    if (isl_space_has_tuple_name(space, isl_dim_set) ||
1367
800
        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
800
    map = isl_map_add_dims(map, type, n);
1373
1.80k
    for (i = 0; i < n; 
++i1.00k
) {
1374
1.00k
      isl_id *id;
1375
1.00k
      if (!isl_space_has_dim_name(space, isl_dim_set, i))
1376
1.00k
        
isl_die0
(ctx, isl_error_invalid,
1377
1.00k
          "parameters must be named",
1378
1.00k
          goto error);
1379
1.00k
      id = isl_space_get_dim_id(space, isl_dim_set, i);
1380
1.00k
      map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1381
1.00k
    }
1382
5.07k
  } else if (type == isl_dim_in) {
1383
3.03k
    isl_set *set;
1384
3.03k
1385
3.03k
    set = isl_set_universe(isl_space_copy(space));
1386
3.03k
    if (rational)
1387
22
      set = isl_set_set_rational(set);
1388
3.03k
    set = isl_set_intersect_params(set, isl_map_params(map));
1389
3.03k
    map = isl_map_from_domain(set);
1390
3.03k
  } else {
1391
2.03k
    isl_set *set;
1392
2.03k
1393
2.03k
    set = isl_set_universe(isl_space_copy(space));
1394
2.03k
    if (rational)
1395
0
      set = isl_set_set_rational(set);
1396
2.03k
    map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1397
2.03k
  }
1398
5.87k
1399
13.9k
  
for (i = 0; 5.87k
i < n;
++i8.10k
) {
1400
8.10k
    isl_pw_aff *pa;
1401
8.10k
    isl_space *space;
1402
8.10k
    isl_aff *aff;
1403
8.10k
    isl_set *set;
1404
8.10k
    isl_map *map_i;
1405
8.10k
1406
8.10k
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1407
8.10k
    space = isl_pw_aff_get_domain_space(pa);
1408
8.10k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1409
8.10k
    aff = isl_aff_add_coefficient_si(aff,
1410
8.10k
            isl_dim_in, v->n - n + i, -1);
1411
8.10k
    pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1412
8.10k
    if (rational)
1413
248
      pa = isl_pw_aff_set_rational(pa);
1414
8.10k
    set = isl_pw_aff_zero_set(pa);
1415
8.10k
    map_i = isl_map_from_range(set);
1416
8.10k
    map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1417
8.10k
    map = isl_map_intersect(map, map_i);
1418
8.10k
  }
1419
5.87k
1420
5.87k
  isl_space_free(space);
1421
5.87k
  isl_multi_pw_aff_free(tuple);
1422
5.87k
  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
5.87k
}
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
5.71k
{
1438
5.71k
  isl_multi_pw_aff *tuple;
1439
5.71k
1440
5.71k
  tuple = read_tuple(s, v, rational, comma);
1441
5.71k
  if (!tuple)
1442
2
    return isl_map_free(map);
1443
5.71k
1444
5.71k
  return map_from_tuple(tuple, map, type, v, rational);
1445
5.71k
}
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
6
{
1465
6
  isl_space *space;
1466
6
  int n;
1467
6
  isl_multi_pw_aff *mpa1, *mpa2;
1468
6
1469
6
  if (!set || !left || !right)
1470
0
    goto error;
1471
6
1472
6
  space = isl_set_get_space(set);
1473
6
  n = isl_pw_aff_list_n_pw_aff(left);
1474
6
  space = isl_space_from_domain(space);
1475
6
  space = isl_space_add_dims(space, isl_dim_out, n);
1476
6
  mpa1 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), left);
1477
6
  mpa2 = isl_multi_pw_aff_from_pw_aff_list(isl_space_copy(space), right);
1478
6
  mpa1 = isl_multi_pw_aff_range_product(mpa1, mpa2);
1479
6
1480
6
  space = isl_space_range(space);
1481
6
  switch (type) {
1482
6
  case ISL_TOKEN_LEX_LT:
1483
2
    set = isl_map_wrap(isl_map_lex_lt(space));
1484
2
    break;
1485
6
  case ISL_TOKEN_LEX_GT:
1486
2
    set = isl_map_wrap(isl_map_lex_gt(space));
1487
2
    break;
1488
6
  case ISL_TOKEN_LEX_LE:
1489
1
    set = isl_map_wrap(isl_map_lex_le(space));
1490
1
    break;
1491
6
  case ISL_TOKEN_LEX_GE:
1492
1
    set = isl_map_wrap(isl_map_lex_ge(space));
1493
1
    break;
1494
6
  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
6
      "unhandled list comparison type", return NULL);
1499
6
  }
1500
6
  set = isl_set_preimage_multi_pw_aff(set, mpa1);
1501
6
  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
6
}
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.78k
{
1527
4.78k
  isl_set *cond;
1528
4.78k
1529
4.78k
  left = isl_pw_aff_list_copy(left);
1530
4.78k
  right = isl_pw_aff_list_copy(right);
1531
4.78k
  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.78k
  if (is_list_comparator_type(type))
1536
6
    cond = list_cmp(set, type, left, right);
1537
4.77k
  else if (type == ISL_TOKEN_LE)
1538
2.37k
    cond = isl_pw_aff_list_le_set(left, right);
1539
2.39k
  else if (type == ISL_TOKEN_GE)
1540
1.34k
    cond = isl_pw_aff_list_ge_set(left, right);
1541
1.05k
  else if (type == ISL_TOKEN_LT)
1542
391
    cond = isl_pw_aff_list_lt_set(left, right);
1543
664
  else if (type == ISL_TOKEN_GT)
1544
123
    cond = isl_pw_aff_list_gt_set(left, right);
1545
541
  else if (type == ISL_TOKEN_NE)
1546
12
    cond = isl_pw_aff_list_ne_set(left, right);
1547
529
  else
1548
529
    cond = isl_pw_aff_list_eq_set(left, right);
1549
4.78k
1550
4.78k
  return isl_set_intersect(set, cond);
1551
4.78k
}
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
4.05k
{
1575
4.05k
  struct isl_token *tok;
1576
4.05k
  int type;
1577
4.05k
  isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1578
4.05k
  int n1, n2;
1579
4.05k
  isl_set *set;
1580
4.05k
1581
4.05k
  set = isl_map_wrap(map);
1582
4.05k
  list1 = accept_affine_list(s, isl_set_get_space(set), v);
1583
4.05k
  if (!list1)
1584
1
    goto error;
1585
4.05k
  tok = isl_stream_next_token(s);
1586
4.05k
  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
4.05k
  type = tok->type;
1593
4.05k
  isl_token_free(tok);
1594
4.78k
  for (;;) {
1595
4.78k
    list2 = accept_affine_list(s, isl_set_get_space(set), v);
1596
4.78k
    if (!list2)
1597
0
      goto error;
1598
4.78k
    n1 = isl_pw_aff_list_n_pw_aff(list1);
1599
4.78k
    n2 = isl_pw_aff_list_n_pw_aff(list2);
1600
4.78k
    if (is_list_comparator_type(type) && 
n1 != n26
) {
1601
0
      isl_stream_error(s, NULL,
1602
0
          "list arguments not of same size");
1603
0
      goto error;
1604
0
    }
1605
4.78k
1606
4.78k
    set = construct_constraints(set, type, list1, list2, rational);
1607
4.78k
    isl_pw_aff_list_free(list1);
1608
4.78k
    list1 = list2;
1609
4.78k
1610
4.78k
    tok = isl_stream_next_token(s);
1611
4.78k
    if (!is_comparator(tok)) {
1612
4.05k
      if (tok)
1613
4.05k
        isl_stream_push_token(s, tok);
1614
4.05k
      break;
1615
4.05k
    }
1616
725
    type = tok->type;
1617
725
    isl_token_free(tok);
1618
725
  }
1619
4.05k
  isl_pw_aff_list_free(list1);
1620
4.05k
1621
4.05k
  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
4.05k
}
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
134
{
1632
134
  int n = v->n;
1633
134
  int seen_paren = isl_stream_eat_if_available(s, '(');
1634
134
1635
134
  map = isl_map_from_domain(isl_map_wrap(map));
1636
134
  map = read_defined_var_list(s, v, map, rational);
1637
134
1638
134
  if (isl_stream_eat(s, ':'))
1639
0
    goto error;
1640
134
1641
134
  map = read_formula(s, v, map, rational);
1642
134
  map = isl_set_unwrap(isl_map_domain(map));
1643
134
1644
134
  vars_drop(v, v->n - n);
1645
134
  if (seen_paren && 
isl_stream_eat(s, ')')90
)
1646
0
    goto error;
1647
134
1648
134
  return map;
1649
0
error:
1650
0
  isl_map_free(map);
1651
0
  return NULL;
1652
134
}
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
186
{
1672
186
  struct isl_token *tok, *tok2;
1673
186
  int line, col;
1674
186
  isl_pw_aff *pwaff;
1675
186
1676
186
  tok = isl_stream_next_token(s);
1677
186
  if (!tok || tok->type != '(')
1678
0
    goto error;
1679
186
1680
186
  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
186
1684
186
  if (isl_stream_next_token_is(s, ISL_TOKEN_EXISTS) ||
1685
186
      
isl_stream_next_token_is(s, ISL_TOKEN_NOT)155
||
1686
186
      
isl_stream_next_token_is(s, ISL_TOKEN_TRUE)154
||
1687
186
      
isl_stream_next_token_is(s, ISL_TOKEN_FALSE)154
||
1688
186
      
isl_stream_next_token_is(s, ISL_TOKEN_MAP)154
) {
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
147
1698
147
  tok2 = isl_stream_next_token(s);
1699
147
  if (!tok2)
1700
0
    goto error;
1701
147
  line = tok2->line;
1702
147
  col = tok2->col;
1703
147
  isl_stream_push_token(s, tok2);
1704
147
1705
147
  pwaff = accept_affine(s, isl_space_wrap(isl_map_get_space(map)), v);
1706
147
  if (!pwaff)
1707
0
    goto error;
1708
147
1709
147
  tok2 = isl_token_new(s->ctx, line, col, 0);
1710
147
  if (!tok2)
1711
0
    goto error2;
1712
147
  tok2->type = ISL_TOKEN_AFF;
1713
147
  tok2->u.pwaff = pwaff;
1714
147
1715
147
  if (isl_stream_eat_if_available(s, ')')) {
1716
23
    isl_stream_push_token(s, tok2);
1717
23
    isl_token_free(tok);
1718
23
    isl_map_free(map);
1719
23
    return 0;
1720
23
  }
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
0
}
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
4.36k
{
1744
4.36k
  if (isl_stream_next_token_is(s, '('))
1745
179
    if (resolve_paren_expr(s, v, isl_map_copy(map), rational))
1746
0
      goto error;
1747
4.36k
1748
4.36k
  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
4.20k
1759
4.20k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS))
1760
134
    return read_exists(s, v, map, rational);
1761
4.07k
1762
4.07k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TRUE))
1763
1
    return map;
1764
4.07k
1765
4.07k
  if (isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)) {
1766
12
    isl_space *dim = isl_map_get_space(map);
1767
12
    isl_map_free(map);
1768
12
    return isl_map_empty(dim);
1769
12
  }
1770
4.05k
    
1771
4.05k
  return add_constraint(s, v, map, rational);
1772
0
error:
1773
0
  isl_map_free(map);
1774
0
  return NULL;
1775
4.05k
}
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
2.25k
{
1780
2.25k
  isl_map *res;
1781
2.25k
  int negate;
1782
2.25k
1783
2.25k
  negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1784
2.25k
  res = read_conjunct(s, v, isl_map_copy(map), rational);
1785
2.25k
  if (negate)
1786
1
    res = isl_map_subtract(isl_map_copy(map), res);
1787
2.25k
1788
4.36k
  while (res && 
isl_stream_eat_if_available(s, ISL_TOKEN_AND)4.36k
) {
1789
2.11k
    isl_map *res_i;
1790
2.11k
1791
2.11k
    negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1792
2.11k
    res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1793
2.11k
    if (negate)
1794
0
      res = isl_map_subtract(res, res_i);
1795
2.11k
    else
1796
2.11k
      res = isl_map_intersect(res, res_i);
1797
2.11k
  }
1798
2.25k
1799
2.25k
  isl_map_free(map);
1800
2.25k
  return res;
1801
2.25k
}
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
2.17k
{
1806
2.17k
  isl_map *res;
1807
2.17k
1808
2.17k
  if (isl_stream_next_token_is(s, '}'))
1809
72
    return map;
1810
2.10k
1811
2.10k
  res = read_conjuncts(s, v, isl_map_copy(map), rational);
1812
2.25k
  while (isl_stream_eat_if_available(s, ISL_TOKEN_OR)) {
1813
153
    isl_map *res_i;
1814
153
1815
153
    res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1816
153
    res = isl_map_union(res, res_i);
1817
153
  }
1818
2.10k
1819
2.10k
  isl_map_free(map);
1820
2.10k
  return res;
1821
2.10k
}
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
2.17k
{
1847
2.17k
  isl_map *res;
1848
2.17k
1849
2.17k
  res = read_disjuncts(s, v, isl_map_copy(map), rational);
1850
2.17k
1851
2.17k
  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
2.17k
    isl_map_free(map);
1859
2.17k
1860
2.17k
  return res;
1861
2.17k
}
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
0
}
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
6
  isl_token_free(tok);
2128
6
  return pow;
2129
6
}
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
67
  }
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
3.41k
{
2286
3.41k
  struct isl_token *tok;
2287
3.41k
2288
3.41k
  tok = isl_stream_next_token(s);
2289
3.41k
  if (!tok) {
2290
0
    isl_stream_error(s, NULL, "unexpected EOF");
2291
0
    goto error;
2292
0
  }
2293
3.41k
  if (tok->type == ':' ||
2294
3.41k
      
(1.68k
tok->type == ISL_TOKEN_OR1.68k
&&
!strcmp(tok->u.s, "|")0
)) {
2295
1.73k
    isl_token_free(tok);
2296
1.73k
    map = read_formula(s, v, map, rational);
2297
1.73k
  } else
2298
1.68k
    isl_stream_push_token(s, tok);
2299
3.41k
2300
3.41k
  return map;
2301
0
error:
2302
0
  isl_map_free(map);
2303
0
  return NULL;
2304
3.41k
}
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
0
}
2365
2366
static int is_rational(__isl_keep isl_stream *s)
2367
3.05k
{
2368
3.05k
  struct isl_token *tok;
2369
3.05k
2370
3.05k
  tok = isl_stream_next_token(s);
2371
3.05k
  if (!tok)
2372
0
    return 0;
2373
3.05k
  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
3.03k
2379
3.03k
  isl_stream_push_token(s, tok);
2380
3.03k
2381
3.03k
  return 0;
2382
3.03k
}
2383
2384
static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2385
  __isl_take isl_map *map, struct vars *v)
2386
3.05k
{
2387
3.05k
  struct isl_token *tok;
2388
3.05k
  struct isl_obj obj = { isl_obj_set, NULL };
2389
3.05k
  int n = v->n;
2390
3.05k
  int rational;
2391
3.05k
2392
3.05k
  rational = is_rational(s);
2393
3.05k
  if (rational)
2394
22
    map = isl_map_set_rational(map);
2395
3.05k
2396
3.05k
  if (isl_stream_next_token_is(s, ':')) {
2397
172
    obj.type = isl_obj_set;
2398
172
    obj.v = read_optional_formula(s, map, v, rational);
2399
172
    return obj;
2400
172
  }
2401
2.88k
2402
2.88k
  if (!next_is_tuple(s))
2403
1
    return obj_read_poly_or_fold(s, map, v, n);
2404
2.87k
2405
2.87k
  map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2406
2.87k
  if (!map)
2407
0
    goto error;
2408
2.87k
  tok = isl_stream_next_token(s);
2409
2.87k
  if (!tok)
2410
0
    goto error;
2411
2.87k
  if (tok->type == ISL_TOKEN_TO) {
2412
1.79k
    obj.type = isl_obj_map;
2413
1.79k
    isl_token_free(tok);
2414
1.79k
    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.76k
    map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2419
1.76k
    if (!map)
2420
1
      goto error;
2421
1.08k
  } else {
2422
1.08k
    map = isl_map_domain(map);
2423
1.08k
    isl_stream_push_token(s, tok);
2424
1.08k
  }
2425
2.87k
2426
2.87k
  map = read_optional_formula(s, map, v, rational);
2427
2.84k
2428
2.84k
  vars_drop(v, v->n - n);
2429
2.84k
2430
2.84k
  obj.v = map;
2431
2.84k
  return obj;
2432
1
error:
2433
1
  isl_map_free(map);
2434
1
  obj.type = isl_obj_none;
2435
1
  return obj;
2436
2.87k
}
2437
2438
static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2439
831
{
2440
831
  if (obj.type == isl_obj_map) {
2441
651
    obj.v = isl_union_map_from_map(obj.v);
2442
651
    obj.type = isl_obj_union_map;
2443
651
  } else 
if (180
obj.type == 180
isl_obj_set180
) {
2444
178
    obj.v = isl_union_set_from_set(obj.v);
2445
178
    obj.type = isl_obj_union_set;
2446
178
  } 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
831
  return obj;
2455
0
error:
2456
0
  obj.type->free(obj.v);
2457
0
  obj.type = isl_obj_none;
2458
0
  return obj;
2459
831
}
2460
2461
static struct isl_obj obj_add(__isl_keep isl_stream *s,
2462
  struct isl_obj obj1, struct isl_obj obj2)
2463
712
{
2464
712
  if (obj1.type == isl_obj_set && 
obj2.type == 226
isl_obj_union_set226
)
2465
712
    
obj1 = to_union(s->ctx, obj1)0
;
2466
712
  if (obj1.type == isl_obj_union_set && 
obj2.type == 22
isl_obj_set22
)
2467
712
    
obj2 = to_union(s->ctx, obj2)22
;
2468
712
  if (obj1.type == isl_obj_map && 
obj2.type == 362
isl_obj_union_map362
)
2469
712
    
obj1 = to_union(s->ctx, obj1)0
;
2470
712
  if (obj1.type == isl_obj_union_map && 
obj2.type == 98
isl_obj_map98
)
2471
712
    
obj2 = to_union(s->ctx, obj2)98
;
2472
712
  if (obj1.type == isl_obj_pw_qpolynomial &&
2473
712
      
obj2.type == 4
isl_obj_union_pw_qpolynomial4
)
2474
712
    
obj1 = to_union(s->ctx, obj1)0
;
2475
712
  if (obj1.type == isl_obj_union_pw_qpolynomial &&
2476
712
      
obj2.type == 0
isl_obj_pw_qpolynomial0
)
2477
712
    
obj2 = to_union(s->ctx, obj2)0
;
2478
712
  if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2479
712
      
obj2.type == 0
isl_obj_union_pw_qpolynomial_fold0
)
2480
712
    
obj1 = to_union(s->ctx, obj1)0
;
2481
712
  if (obj1.type == isl_obj_union_pw_qpolynomial_fold &&
2482
712
      
obj2.type == 0
isl_obj_pw_qpolynomial_fold0
)
2483
712
    
obj2 = to_union(s->ctx, obj2)0
;
2484
712
  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
712
  if (!obj1.type->add)
2490
712
    
isl_die0
(s->ctx, isl_error_internal,
2491
712
      "combination not supported on object type", goto error);
2492
712
  if (obj1.type == isl_obj_map && 
!isl_map_has_equal_space(obj1.v, obj2.v)362
) {
2493
262
    obj1 = to_union(s->ctx, obj1);
2494
262
    obj2 = to_union(s->ctx, obj2);
2495
262
  }
2496
712
  if (obj1.type == isl_obj_set && 
!isl_set_has_equal_space(obj1.v, obj2.v)226
) {
2497
78
    obj1 = to_union(s->ctx, obj1);
2498
78
    obj2 = to_union(s->ctx, obj2);
2499
78
  }
2500
712
  if (obj1.type == isl_obj_pw_qpolynomial &&
2501
712
      
!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
712
  if (obj1.type == isl_obj_pw_qpolynomial_fold &&
2506
712
      
!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
712
  obj1.v = obj1.type->add(obj1.v, obj2.v);
2511
712
  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
712
}
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.56k
{
2525
2.56k
  struct isl_token *tok;
2526
2.56k
  char *name;
2527
2.56k
  int res;
2528
2.56k
2529
2.56k
  tok = isl_stream_next_token(s);
2530
2.56k
  if (!tok)
2531
0
    return 0;
2532
2.56k
  if (tok->type != ISL_TOKEN_IDENT && 
tok->type != ISL_TOKEN_STRING1.69k
) {
2533
1.69k
    isl_stream_push_token(s, tok);
2534
1.69k
    return 0;
2535
1.69k
  }
2536
869
2537
869
  name = isl_token_get_str(s->ctx, tok);
2538
869
  res = !strcmp(name, "domain") && 
isl_stream_next_token_is(s, ':')0
;
2539
869
  free(name);
2540
869
2541
869
  isl_stream_push_token(s, tok);
2542
869
2543
869
  return res;
2544
869
}
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.56k
{
2555
2.56k
  struct isl_token *tok;
2556
2.56k
  int is_schedule;
2557
2.56k
2558
2.56k
  tok = isl_stream_next_token(s);
2559
2.56k
  if (!tok)
2560
0
    return 0;
2561
2.56k
  if (tok->type != '{') {
2562
715
    isl_stream_push_token(s, tok);
2563
715
    return next_is_domain_colon(s);
2564
715
  }
2565
1.85k
2566
1.85k
  is_schedule = next_is_domain_colon(s);
2567
1.85k
  isl_stream_push_token(s, tok);
2568
1.85k
2569
1.85k
  return is_schedule;
2570
1.85k
}
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
/* Read a disjunction of object bodies from "s".
2585
 * That is, read the inside of the braces, but not the braces themselves.
2586
 * "v" contains a description of the identifiers parsed so far.
2587
 * "map" contains information about the parameters.
2588
 */
2589
static struct isl_obj obj_read_disjuncts(__isl_keep isl_stream *s,
2590
  struct vars *v, __isl_keep isl_map *map)
2591
2.53k
{
2592
2.53k
  struct isl_obj obj = { isl_obj_set, NULL };
2593
2.53k
2594
2.53k
  if (isl_stream_next_token_is(s, '}')) {
2595
190
    obj.type = isl_obj_union_set;
2596
190
    obj.v = isl_union_set_empty(isl_map_get_space(map));
2597
190
    return obj;
2598
190
  }
2599
2.34k
2600
3.05k
  
for (;;)2.34k
{
2601
3.05k
    struct isl_obj o;
2602
3.05k
    o = obj_read_body(s, isl_map_copy(map), v);
2603
3.05k
    if (o.type == isl_obj_none || 
!o.v3.05k
)
2604
2
      return o;
2605
3.05k
    if (!obj.v)
2606
2.33k
      obj = o;
2607
712
    else {
2608
712
      obj = obj_add(s, obj, o);
2609
712
      if (obj.type == isl_obj_none || !obj.v)
2610
0
        return obj;
2611
3.05k
    }
2612
3.05k
    if (!isl_stream_eat_if_available(s, ';'))
2613
2.33k
      break;
2614
712
    if (isl_stream_next_token_is(s, '}'))
2615
0
      break;
2616
712
  }
2617
2.34k
2618
2.34k
  
return obj2.33k
;
2619
2.34k
}
2620
2621
static struct isl_obj obj_read(__isl_keep isl_stream *s)
2622
2.56k
{
2623
2.56k
  isl_map *map = NULL;
2624
2.56k
  struct isl_token *tok;
2625
2.56k
  struct vars *v = NULL;
2626
2.56k
  struct isl_obj obj = { isl_obj_set, NULL };
2627
2.56k
2628
2.56k
  if (next_is_schedule(s))
2629
0
    return schedule_read(s);
2630
2.56k
2631
2.56k
  tok = next_token(s);
2632
2.56k
  if (!tok) {
2633
0
    isl_stream_error(s, NULL, "unexpected EOF");
2634
0
    goto error;
2635
0
  }
2636
2.56k
  if (tok->type == ISL_TOKEN_VALUE) {
2637
105
    struct isl_token *tok2;
2638
105
    struct isl_map *map;
2639
105
2640
105
    tok2 = isl_stream_next_token(s);
2641
105
    if (!tok2 || tok2->type != ISL_TOKEN_VALUE ||
2642
105
        isl_int_is_neg(tok2->u.v)) {
2643
0
      if (tok2)
2644
0
        isl_stream_push_token(s, tok2);
2645
0
      obj.type = isl_obj_val;
2646
0
      obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2647
0
      isl_token_free(tok);
2648
0
      return obj;
2649
0
    }
2650
105
    isl_stream_push_token(s, tok2);
2651
105
    isl_stream_push_token(s, tok);
2652
105
    map = map_read_polylib(s);
2653
105
    if (!map)
2654
0
      goto error;
2655
105
    if (isl_map_may_be_set(map))
2656
105
      obj.v = isl_map_range(map);
2657
0
    else {
2658
0
      obj.type = isl_obj_map;
2659
0
      obj.v = map;
2660
0
    }
2661
105
    return obj;
2662
105
  }
2663
2.46k
  v = vars_new(s->ctx);
2664
2.46k
  if (!v) {
2665
0
    isl_stream_push_token(s, tok);
2666
0
    goto error;
2667
0
  }
2668
2.46k
  map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2669
2.46k
  if (tok->type == '[') {
2670
610
    isl_stream_push_token(s, tok);
2671
610
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2672
610
    if (!map)
2673
1
      goto error;
2674
609
    tok = isl_stream_next_token(s);
2675
609
    if (!tok || tok->type != ISL_TOKEN_TO) {
2676
0
      isl_stream_error(s, tok, "expecting '->'");
2677
0
      if (tok)
2678
0
        isl_stream_push_token(s, tok);
2679
0
      goto error;
2680
0
    }
2681
609
    isl_token_free(tok);
2682
609
    tok = isl_stream_next_token(s);
2683
609
  }
2684
2.46k
  
if (2.45k
!tok2.45k
||
tok->type != '{'2.45k
) {
2685
0
    isl_stream_error(s, tok, "expecting '{'");
2686
0
    if (tok)
2687
0
      isl_stream_push_token(s, tok);
2688
0
    goto error;
2689
0
  }
2690
2.45k
  isl_token_free(tok);
2691
2.45k
2692
2.45k
  tok = isl_stream_next_token(s);
2693
2.45k
  if (!tok)
2694
0
    ;
2695
2.45k
  else if (tok->type == ISL_TOKEN_IDENT && 
!strcmp(tok->u.s, "Sym")1.25k
) {
2696
1
    isl_token_free(tok);
2697
1
    if (isl_stream_eat(s, '='))
2698
0
      goto error;
2699
1
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2700
1
    if (!map)
2701
0
      goto error;
2702
2.45k
  } else
2703
2.45k
    isl_stream_push_token(s, tok);
2704
2.45k
2705
2.45k
  obj = obj_read_disjuncts(s, v, map);
2706
2.45k
  if (obj.type == isl_obj_none || 
!obj.v2.45k
)
2707
2
    goto error;
2708
2.45k
2709
2.45k
  tok = isl_stream_next_token(s);
2710
2.45k
  if (tok && tok->type == '}') {
2711
2.45k
    isl_token_free(tok);
2712
2.45k
  } else {
2713
0
    isl_stream_error(s, tok, "unexpected isl_token");
2714
0
    if (tok)
2715
0
      isl_token_free(tok);
2716
0
    goto error;
2717
0
  }
2718
2.45k
2719
2.45k
  vars_free(v);
2720
2.45k
  isl_map_free(map);
2721
2.45k
2722
2.45k
  return obj;
2723
3
error:
2724
3
  isl_map_free(map);
2725
3
  obj.type->free(obj.v);
2726
3
  if (v)
2727
3
    vars_free(v);
2728
3
  obj.v = NULL;
2729
3
  return obj;
2730
2.45k
}
2731
2732
struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2733
16
{
2734
16
  return obj_read(s);
2735
16
}
2736
2737
__isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2738
636
{
2739
636
  struct isl_obj obj;
2740
636
2741
636
  obj = obj_read(s);
2742
636
  if (obj.v)
2743
636
    isl_assert(s->ctx, obj.type == isl_obj_map ||
2744
636
           obj.type == isl_obj_set, goto error);
2745
636
  
2746
636
  if (obj.type == isl_obj_set)
2747
636
    
obj.v = isl_map_from_range(obj.v)31
;
2748
636
2749
636
  return obj.v;
2750
0
error:
2751
0
  obj.type->free(obj.v);
2752
0
  return NULL;
2753
636
}
2754
2755
__isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2756
542
{
2757
542
  struct isl_obj obj;
2758
542
2759
542
  obj = obj_read(s);
2760
542
  if (obj.v) {
2761
541
    if (obj.type == isl_obj_map && 
isl_map_may_be_set(obj.v)0
) {
2762
0
      obj.v = isl_map_range(obj.v);
2763
0
      obj.type = isl_obj_set;
2764
0
    }
2765
541
    isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2766
541
  }
2767
542
2768
542
  return obj.v;
2769
0
error:
2770
0
  obj.type->free(obj.v);
2771
0
  return NULL;
2772
542
}
2773
2774
__isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2775
756
{
2776
756
  struct isl_obj obj;
2777
756
2778
756
  obj = obj_read(s);
2779
756
  if (obj.type == isl_obj_map) {
2780
390
    obj.type = isl_obj_union_map;
2781
390
    obj.v = isl_union_map_from_map(obj.v);
2782
390
  }
2783
756
  if (obj.type == isl_obj_set) {
2784
0
    obj.type = isl_obj_union_set;
2785
0
    obj.v = isl_union_set_from_set(obj.v);
2786
0
  }
2787
756
  if (obj.v && obj.type == isl_obj_union_set &&
2788
756
      
isl_union_set_is_empty(obj.v)110
)
2789
110
    obj.type = isl_obj_union_map;
2790
756
  if (obj.v && obj.type != isl_obj_union_map)
2791
756
    
isl_die0
(s->ctx, isl_error_invalid, "invalid input", goto error);
2792
756
2793
756
  return obj.v;
2794
0
error:
2795
0
  obj.type->free(obj.v);
2796
0
  return NULL;
2797
756
}
2798
2799
/* Extract an isl_union_set from "obj".
2800
 * This only works if the object was detected as either a set
2801
 * (in which case it is converted to a union set) or a union set.
2802
 */
2803
static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
2804
  struct isl_obj obj)
2805
444
{
2806
444
  if (obj.type == isl_obj_set) {
2807
287
    obj.type = isl_obj_union_set;
2808
287
    obj.v = isl_union_set_from_set(obj.v);
2809
287
  }
2810
444
  if (obj.v)
2811
444
    isl_assert(ctx, obj.type == isl_obj_union_set, goto error);
2812
444
2813
444
  return obj.v;
2814
0
error:
2815
0
  obj.type->free(obj.v);
2816
0
  return NULL;
2817
444
}
2818
2819
/* Read an isl_union_set from "s".
2820
 * First read a generic object and then try and extract
2821
 * an isl_union_set from that.
2822
 */
2823
__isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2824
373
{
2825
373
  struct isl_obj obj;
2826
373
2827
373
  obj = obj_read(s);
2828
373
  return extract_union_set(s->ctx, obj);
2829
373
}
2830
2831
static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2832
177
{
2833
177
  struct isl_obj obj;
2834
177
  struct isl_map *map;
2835
177
  struct isl_basic_map *bmap;
2836
177
2837
177
  obj = obj_read(s);
2838
177
  if (obj.v && (obj.type != isl_obj_map && 
obj.type != 167
isl_obj_set167
))
2839
177
    
isl_die0
(s->ctx, isl_error_invalid, "not a (basic) set or map",
2840
177
      goto error);
2841
177
  map = obj.v;
2842
177
  if (!map)
2843
0
    return NULL;
2844
177
2845
177
  if (map->n > 1)
2846
177
    
isl_die0
(s->ctx, isl_error_invalid,
2847
177
      "set or map description involves "
2848
177
      "more than one disjunct", goto error);
2849
177
2850
177
  if (map->n == 0)
2851
4
    bmap = isl_basic_map_empty(isl_map_get_space(map));
2852
173
  else
2853
173
    bmap = isl_basic_map_copy(map->p[0]);
2854
177
2855
177
  isl_map_free(map);
2856
177
2857
177
  return bmap;
2858
0
error:
2859
0
  obj.type->free(obj.v);
2860
0
  return NULL;
2861
177
}
2862
2863
static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2864
167
{
2865
167
  isl_basic_map *bmap;
2866
167
  bmap = basic_map_read(s);
2867
167
  if (!bmap)
2868
0
    return NULL;
2869
167
  if (!isl_basic_map_may_be_set(bmap))
2870
167
    
isl_die0
(s->ctx, isl_error_invalid,
2871
167
      "input is not a set", goto error);
2872
167
  return isl_basic_map_range(bmap);
2873
0
error:
2874
0
  isl_basic_map_free(bmap);
2875
0
  return NULL;
2876
167
}
2877
2878
__isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2879
  FILE *input)
2880
2
{
2881
2
  struct isl_basic_map *bmap;
2882
2
  isl_stream *s = isl_stream_new_file(ctx, input);
2883
2
  if (!s)
2884
0
    return NULL;
2885
2
  bmap = basic_map_read(s);
2886
2
  isl_stream_free(s);
2887
2
  return bmap;
2888
2
}
2889
2890
__isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2891
  FILE *input)
2892
110
{
2893
110
  isl_basic_set *bset;
2894
110
  isl_stream *s = isl_stream_new_file(ctx, input);
2895
110
  if (!s)
2896
0
    return NULL;
2897
110
  bset = basic_set_read(s);
2898
110
  isl_stream_free(s);
2899
110
  return bset;
2900
110
}
2901
2902
struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2903
  const char *str)
2904
8
{
2905
8
  struct isl_basic_map *bmap;
2906
8
  isl_stream *s = isl_stream_new_str(ctx, str);
2907
8
  if (!s)
2908
0
    return NULL;
2909
8
  bmap = basic_map_read(s);
2910
8
  isl_stream_free(s);
2911
8
  return bmap;
2912
8
}
2913
2914
struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2915
  const char *str)
2916
57
{
2917
57
  isl_basic_set *bset;
2918
57
  isl_stream *s = isl_stream_new_str(ctx, str);
2919
57
  if (!s)
2920
0
    return NULL;
2921
57
  bset = basic_set_read(s);
2922
57
  isl_stream_free(s);
2923
57
  return bset;
2924
57
}
2925
2926
__isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2927
  FILE *input)
2928
0
{
2929
0
  struct isl_map *map;
2930
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2931
0
  if (!s)
2932
0
    return NULL;
2933
0
  map = isl_stream_read_map(s);
2934
0
  isl_stream_free(s);
2935
0
  return map;
2936
0
}
2937
2938
__isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2939
  const char *str)
2940
636
{
2941
636
  struct isl_map *map;
2942
636
  isl_stream *s = isl_stream_new_str(ctx, str);
2943
636
  if (!s)
2944
0
    return NULL;
2945
636
  map = isl_stream_read_map(s);
2946
636
  isl_stream_free(s);
2947
636
  return map;
2948
636
}
2949
2950
__isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2951
  FILE *input)
2952
0
{
2953
0
  isl_set *set;
2954
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2955
0
  if (!s)
2956
0
    return NULL;
2957
0
  set = isl_stream_read_set(s);
2958
0
  isl_stream_free(s);
2959
0
  return set;
2960
0
}
2961
2962
struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2963
  const char *str)
2964
542
{
2965
542
  isl_set *set;
2966
542
  isl_stream *s = isl_stream_new_str(ctx, str);
2967
542
  if (!s)
2968
0
    return NULL;
2969
542
  set = isl_stream_read_set(s);
2970
542
  isl_stream_free(s);
2971
542
  return set;
2972
542
}
2973
2974
__isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2975
  FILE *input)
2976
0
{
2977
0
  isl_union_map *umap;
2978
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2979
0
  if (!s)
2980
0
    return NULL;
2981
0
  umap = isl_stream_read_union_map(s);
2982
0
  isl_stream_free(s);
2983
0
  return umap;
2984
0
}
2985
2986
__isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2987
    const char *str)
2988
756
{
2989
756
  isl_union_map *umap;
2990
756
  isl_stream *s = isl_stream_new_str(ctx, str);
2991
756
  if (!s)
2992
0
    return NULL;
2993
756
  umap = isl_stream_read_union_map(s);
2994
756
  isl_stream_free(s);
2995
756
  return umap;
2996
756
}
2997
2998
__isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2999
  FILE *input)
3000
0
{
3001
0
  isl_union_set *uset;
3002
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3003
0
  if (!s)
3004
0
    return NULL;
3005
0
  uset = isl_stream_read_union_set(s);
3006
0
  isl_stream_free(s);
3007
0
  return uset;
3008
0
}
3009
3010
__isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
3011
    const char *str)
3012
320
{
3013
320
  isl_union_set *uset;
3014
320
  isl_stream *s = isl_stream_new_str(ctx, str);
3015
320
  if (!s)
3016
0
    return NULL;
3017
320
  uset = isl_stream_read_union_set(s);
3018
320
  isl_stream_free(s);
3019
320
  return uset;
3020
320
}
3021
3022
static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
3023
0
{
3024
0
  struct isl_vec *vec = NULL;
3025
0
  struct isl_token *tok;
3026
0
  unsigned size;
3027
0
  int j;
3028
0
3029
0
  tok = isl_stream_next_token(s);
3030
0
  if (!tok || tok->type != ISL_TOKEN_VALUE) {
3031
0
    isl_stream_error(s, tok, "expecting vector length");
3032
0
    goto error;
3033
0
  }
3034
0
3035
0
  size = isl_int_get_si(tok->u.v);
3036
0
  isl_token_free(tok);
3037
0
3038
0
  vec = isl_vec_alloc(s->ctx, size);
3039
0
3040
0
  for (j = 0; j < size; ++j) {
3041
0
    tok = isl_stream_next_token(s);
3042
0
    if (!tok || tok->type != ISL_TOKEN_VALUE) {
3043
0
      isl_stream_error(s, tok, "expecting constant value");
3044
0
      goto error;
3045
0
    }
3046
0
    isl_int_set(vec->el[j], tok->u.v);
3047
0
    isl_token_free(tok);
3048
0
  }
3049
0
3050
0
  return vec;
3051
0
error:
3052
0
  isl_token_free(tok);
3053
0
  isl_vec_free(vec);
3054
0
  return NULL;
3055
0
}
3056
3057
static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3058
0
{
3059
0
  return isl_vec_read_polylib(s);
3060
0
}
3061
3062
__isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3063
0
{
3064
0
  isl_vec *v;
3065
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3066
0
  if (!s)
3067
0
    return NULL;
3068
0
  v = vec_read(s);
3069
0
  isl_stream_free(s);
3070
0
  return v;
3071
0
}
3072
3073
__isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3074
  __isl_keep isl_stream *s)
3075
27
{
3076
27
  struct isl_obj obj;
3077
27
3078
27
  obj = obj_read(s);
3079
27
  if (obj.v)
3080
27
    isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3081
27
         goto error);
3082
27
3083
27
  return obj.v;
3084
0
error:
3085
0
  obj.type->free(obj.v);
3086
0
  return NULL;
3087
27
}
3088
3089
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3090
    const char *str)
3091
27
{
3092
27
  isl_pw_qpolynomial *pwqp;
3093
27
  isl_stream *s = isl_stream_new_str(ctx, str);
3094
27
  if (!s)
3095
0
    return NULL;
3096
27
  pwqp = isl_stream_read_pw_qpolynomial(s);
3097
27
  isl_stream_free(s);
3098
27
  return pwqp;
3099
27
}
3100
3101
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3102
    FILE *input)
3103
0
{
3104
0
  isl_pw_qpolynomial *pwqp;
3105
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3106
0
  if (!s)
3107
0
    return NULL;
3108
0
  pwqp = isl_stream_read_pw_qpolynomial(s);
3109
0
  isl_stream_free(s);
3110
0
  return pwqp;
3111
0
}
3112
3113
/* Is the next token an identifer not in "v"?
3114
 */
3115
static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3116
65
{
3117
65
  int n = v->n;
3118
65
  int fresh;
3119
65
  struct isl_token *tok;
3120
65
3121
65
  tok = isl_stream_next_token(s);
3122
65
  if (!tok)
3123
0
    return 0;
3124
65
  fresh = tok->type == ISL_TOKEN_IDENT && 
vars_pos(v, tok->u.s, -1) >= n57
;
3125
65
  isl_stream_push_token(s, tok);
3126
65
3127
65
  vars_drop(v, v->n - n);
3128
65
3129
65
  return fresh;
3130
65
}
3131
3132
/* First read the domain of the affine expression, which may be
3133
 * a parameter space or a set.
3134
 * The tricky part is that we don't know if the domain is a set or not,
3135
 * so when we are trying to read the domain, we may actually be reading
3136
 * the affine expression itself (defined on a parameter domains)
3137
 * If the tuple we are reading is named, we assume it's the domain.
3138
 * Also, if inside the tuple, the first thing we find is a nested tuple
3139
 * or a new identifier, we again assume it's the domain.
3140
 * Finally, if the tuple is empty, then it must be the domain
3141
 * since it does not contain an affine expression.
3142
 * Otherwise, we assume we are reading an affine expression.
3143
 */
3144
static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3145
  __isl_take isl_set *dom, struct vars *v)
3146
294
{
3147
294
  struct isl_token *tok, *tok2;
3148
294
  int is_empty;
3149
294
3150
294
  tok = isl_stream_next_token(s);
3151
294
  if (tok && (tok->type == ISL_TOKEN_IDENT || 
tok->is_keyword66
)) {
3152
228
    isl_stream_push_token(s, tok);
3153
228
    return read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3154
228
  }
3155
66
  if (!tok || tok->type != '[') {
3156
0
    isl_stream_error(s, tok, "expecting '['");
3157
0
    goto error;
3158
0
  }
3159
66
  tok2 = isl_stream_next_token(s);
3160
66
  is_empty = tok2 && tok2->type == ']';
3161
66
  if (tok2)
3162
66
    isl_stream_push_token(s, tok2);
3163
66
  if (is_empty || 
next_is_tuple(s)65
||
next_is_fresh_ident(s, v)65
) {
3164
48
    isl_stream_push_token(s, tok);
3165
48
    dom = read_map_tuple(s, dom, isl_dim_set, v, 0, 0);
3166
48
  } else
3167
18
    isl_stream_push_token(s, tok);
3168
66
3169
66
  return dom;
3170
0
error:
3171
0
  if (tok)
3172
0
    isl_stream_push_token(s, tok);
3173
0
  isl_set_free(dom);
3174
0
  return NULL;
3175
66
}
3176
3177
/* Read an affine expression from "s".
3178
 */
3179
__isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3180
63
{
3181
63
  isl_aff *aff;
3182
63
  isl_multi_aff *ma;
3183
63
3184
63
  ma = isl_stream_read_multi_aff(s);
3185
63
  if (!ma)
3186
0
    return NULL;
3187
63
  if (isl_multi_aff_dim(ma, isl_dim_out) != 1)
3188
63
    
isl_die0
(s->ctx, isl_error_invalid,
3189
63
      "expecting single affine expression",
3190
63
      goto error);
3191
63
3192
63
  aff = isl_multi_aff_get_aff(ma, 0);
3193
63
  isl_multi_aff_free(ma);
3194
63
  return aff;
3195
0
error:
3196
0
  isl_multi_aff_free(ma);
3197
0
  return NULL;
3198
63
}
3199
3200
/* Read a piecewise affine expression from "s" with domain (space) "dom".
3201
 */
3202
static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3203
  __isl_take isl_set *dom, struct vars *v)
3204
294
{
3205
294
  isl_pw_aff *pwaff = NULL;
3206
294
3207
294
  if (!isl_set_is_params(dom) && 
isl_stream_eat(s, ISL_TOKEN_TO)276
)
3208
0
    goto error;
3209
294
3210
294
  if (isl_stream_eat(s, '['))
3211
0
    goto error;
3212
294
3213
294
  pwaff = accept_affine(s, isl_set_get_space(dom), v);
3214
294
3215
294
  if (isl_stream_eat(s, ']'))
3216
0
    goto error;
3217
294
3218
294
  dom = read_optional_formula(s, dom, v, 0);
3219
294
  pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3220
294
3221
294
  return pwaff;
3222
0
error:
3223
0
  isl_set_free(dom);
3224
0
  isl_pw_aff_free(pwaff);
3225
0
  return NULL;
3226
294
}
3227
3228
__isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3229
54
{
3230
54
  struct vars *v;
3231
54
  isl_set *dom = NULL;
3232
54
  isl_set *aff_dom;
3233
54
  isl_pw_aff *pa = NULL;
3234
54
  int n;
3235
54
3236
54
  v = vars_new(s->ctx);
3237
54
  if (!v)
3238
0
    return NULL;
3239
54
3240
54
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3241
54
  if (next_is_tuple(s)) {
3242
10
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3243
10
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3244
0
      goto error;
3245
54
  }
3246
54
  if (isl_stream_eat(s, '{'))
3247
0
    goto error;
3248
54
3249
54
  n = v->n;
3250
54
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3251
54
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3252
54
  vars_drop(v, v->n - n);
3253
54
3254
65
  while (isl_stream_eat_if_available(s, ';')) {
3255
11
    isl_pw_aff *pa_i;
3256
11
3257
11
    n = v->n;
3258
11
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3259
11
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3260
11
    vars_drop(v, v->n - n);
3261
11
3262
11
    pa = isl_pw_aff_union_add(pa, pa_i);
3263
11
  }
3264
54
3265
54
  if (isl_stream_eat(s, '}'))
3266
0
    goto error;
3267
54
3268
54
  vars_free(v);
3269
54
  isl_set_free(dom);
3270
54
  return pa;
3271
0
error:
3272
0
  vars_free(v);
3273
0
  isl_set_free(dom);
3274
0
  isl_pw_aff_free(pa);
3275
0
  return NULL;
3276
54
}
3277
3278
__isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3279
63
{
3280
63
  isl_aff *aff;
3281
63
  isl_stream *s = isl_stream_new_str(ctx, str);
3282
63
  if (!s)
3283
0
    return NULL;
3284
63
  aff = isl_stream_read_aff(s);
3285
63
  isl_stream_free(s);
3286
63
  return aff;
3287
63
}
3288
3289
__isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3290
54
{
3291
54
  isl_pw_aff *pa;
3292
54
  isl_stream *s = isl_stream_new_str(ctx, str);
3293
54
  if (!s)
3294
0
    return NULL;
3295
54
  pa = isl_stream_read_pw_aff(s);
3296
54
  isl_stream_free(s);
3297
54
  return pa;
3298
54
}
3299
3300
/* Extract an isl_multi_pw_aff with domain space "dom_space"
3301
 * from a tuple "tuple" read by read_tuple.
3302
 *
3303
 * Note that the function read_tuple accepts tuples where some output or
3304
 * set dimensions are defined in terms of other output or set dimensions
3305
 * since this function is also used to read maps.  As a special case,
3306
 * read_tuple also accept dimensions that are defined in terms of themselves
3307
 * (i.e., that are not defined).
3308
 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3309
 * that the definitions of the output/set dimensions do not involve any
3310
 * output/set dimensions.
3311
 * Finally, drop the output dimensions from the domain of the result
3312
 * of read_tuple (which is of the form [input, output] -> [output],
3313
 * with anonymous domain) and reset the space.
3314
 */
3315
static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3316
  __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3317
208
{
3318
208
  int dim, i, n;
3319
208
  isl_space *space;
3320
208
  isl_multi_pw_aff *mpa;
3321
208
3322
208
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3323
208
  dim = isl_space_dim(dom_space, isl_dim_all);
3324
208
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3325
208
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3326
208
  if (!isl_space_is_params(dom_space))
3327
158
    space = isl_space_map_from_domain_and_range(
3328
158
        isl_space_copy(dom_space), space);
3329
208
  isl_space_free(dom_space);
3330
208
  mpa = isl_multi_pw_aff_alloc(space);
3331
208
3332
425
  for (i = 0; i < n; 
++i217
) {
3333
217
    isl_pw_aff *pa;
3334
217
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3335
217
    if (!pa)
3336
0
      return isl_multi_pw_aff_free(mpa);
3337
217
    if (isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)) {
3338
0
      isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3339
0
      isl_pw_aff_free(pa);
3340
0
      isl_die(ctx, isl_error_invalid,
3341
0
        "not an affine expression",
3342
0
        return isl_multi_pw_aff_free(mpa));
3343
0
    }
3344
217
    pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3345
217
    space = isl_multi_pw_aff_get_domain_space(mpa);
3346
217
    pa = isl_pw_aff_reset_domain_space(pa, space);
3347
217
    mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3348
217
  }
3349
208
3350
208
  return mpa;
3351
208
}
3352
3353
/* Read a tuple of affine expressions, together with optional constraints
3354
 * on the domain from "s".  "dom" represents the initial constraints
3355
 * on the domain.
3356
 *
3357
 * The isl_multi_aff may live in either a set or a map space.
3358
 * First read the first tuple and check if it is followed by a "->".
3359
 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3360
 * read in the next tuple.  This tuple (or the first tuple if it was
3361
 * not followed by a "->") is then converted into an isl_multi_pw_aff
3362
 * through a call to extract_mpa_from_tuple.
3363
 * The result is converted to an isl_pw_multi_aff and
3364
 * its domain is intersected with the domain.
3365
 */
3366
static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3367
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3368
68
{
3369
68
  isl_multi_pw_aff *tuple;
3370
68
  isl_multi_pw_aff *mpa;
3371
68
  isl_pw_multi_aff *pma;
3372
68
  int n = v->n;
3373
68
3374
68
  tuple = read_tuple(s, v, 0, 0);
3375
68
  if (!tuple)
3376
0
    goto error;
3377
68
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3378
34
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3379
34
    dom = isl_map_domain(map);
3380
34
    tuple = read_tuple(s, v, 0, 0);
3381
34
    if (!tuple)
3382
0
      goto error;
3383
68
  }
3384
68
3385
68
  dom = read_optional_formula(s, dom, v, 0);
3386
68
3387
68
  vars_drop(v, v->n - n);
3388
68
3389
68
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3390
68
  isl_multi_pw_aff_free(tuple);
3391
68
  pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3392
68
  pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3393
68
3394
68
  return pma;
3395
0
error:
3396
0
  isl_set_free(dom);
3397
0
  return NULL;
3398
68
}
3399
3400
/* Read an isl_pw_multi_aff from "s".
3401
 *
3402
 * In particular, first read the parameters and then read a sequence
3403
 * of one or more tuples of affine expressions with optional conditions and
3404
 * add them up.
3405
 */
3406
__isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3407
  __isl_keep isl_stream *s)
3408
60
{
3409
60
  struct vars *v;
3410
60
  isl_set *dom;
3411
60
  isl_pw_multi_aff *pma = NULL;
3412
60
3413
60
  v = vars_new(s->ctx);
3414
60
  if (!v)
3415
0
    return NULL;
3416
60
3417
60
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3418
60
  if (next_is_tuple(s)) {
3419
34
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3420
34
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3421
0
      goto error;
3422
60
  }
3423
60
  if (isl_stream_eat(s, '{'))
3424
0
    goto error;
3425
60
3426
60
  pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3427
60
3428
68
  while (isl_stream_eat_if_available(s, ';')) {
3429
8
    isl_pw_multi_aff *pma2;
3430
8
3431
8
    pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3432
8
    pma = isl_pw_multi_aff_union_add(pma, pma2);
3433
8
    if (!pma)
3434
0
      goto error;
3435
8
  }
3436
60
3437
60
  if (isl_stream_eat(s, '}'))
3438
0
    goto error;
3439
60
3440
60
  isl_set_free(dom);
3441
60
  vars_free(v);
3442
60
  return pma;
3443
0
error:
3444
0
  isl_pw_multi_aff_free(pma);
3445
0
  isl_set_free(dom);
3446
0
  vars_free(v);
3447
0
  return NULL;
3448
60
}
3449
3450
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3451
  const char *str)
3452
60
{
3453
60
  isl_pw_multi_aff *pma;
3454
60
  isl_stream *s = isl_stream_new_str(ctx, str);
3455
60
  if (!s)
3456
0
    return NULL;
3457
60
  pma = isl_stream_read_pw_multi_aff(s);
3458
60
  isl_stream_free(s);
3459
60
  return pma;
3460
60
}
3461
3462
/* Read an isl_union_pw_multi_aff from "s".
3463
 * We currently read a generic object and if it turns out to be a set or
3464
 * a map, we convert that to an isl_union_pw_multi_aff.
3465
 * It would be more efficient if we were to construct
3466
 * the isl_union_pw_multi_aff directly.
3467
 */
3468
__isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3469
  __isl_keep isl_stream *s)
3470
36
{
3471
36
  struct isl_obj obj;
3472
36
3473
36
  obj = obj_read(s);
3474
36
  if (!obj.v)
3475
0
    return NULL;
3476
36
3477
36
  if (obj.type == isl_obj_map || 
obj.type == 7
isl_obj_set7
)
3478
36
    
obj = to_union(s->ctx, obj)29
;
3479
36
  if (obj.type == isl_obj_union_map)
3480
36
    
return isl_union_pw_multi_aff_from_union_map(obj.v)35
;
3481
1
  if (obj.type == isl_obj_union_set)
3482
1
    return isl_union_pw_multi_aff_from_union_set(obj.v);
3483
0
3484
0
  obj.type->free(obj.v);
3485
0
  isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3486
0
    return NULL);
3487
0
}
3488
3489
/* Read an isl_union_pw_multi_aff from "str".
3490
 */
3491
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3492
  isl_ctx *ctx, const char *str)
3493
36
{
3494
36
  isl_union_pw_multi_aff *upma;
3495
36
  isl_stream *s = isl_stream_new_str(ctx, str);
3496
36
  if (!s)
3497
0
    return NULL;
3498
36
  upma = isl_stream_read_union_pw_multi_aff(s);
3499
36
  isl_stream_free(s);
3500
36
  return upma;
3501
36
}
3502
3503
/* Assuming "pa" represents a single affine expression defined on a universe
3504
 * domain, extract this affine expression.
3505
 */
3506
static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3507
134
{
3508
134
  isl_aff *aff;
3509
134
3510
134
  if (!pa)
3511
0
    return NULL;
3512
134
  if (pa->n != 1)
3513
134
    
isl_die0
(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3514
134
      "expecting single affine expression",
3515
134
      goto error);
3516
134
  if (!isl_set_plain_is_universe(pa->p[0].set))
3517
134
    
isl_die0
(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3518
134
      "expecting universe domain",
3519
134
      goto error);
3520
134
3521
134
  aff = isl_aff_copy(pa->p[0].aff);
3522
134
  isl_pw_aff_free(pa);
3523
134
  return aff;
3524
0
error:
3525
0
  isl_pw_aff_free(pa);
3526
0
  return NULL;
3527
134
}
3528
3529
/* This function is called for each element in a tuple inside
3530
 * isl_stream_read_multi_val.
3531
 * Read an isl_val from "s" and add it to *list.
3532
 */
3533
static __isl_give isl_space *read_val_el(__isl_keep isl_stream *s,
3534
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3535
17
{
3536
17
  isl_val_list **list = (isl_val_list **) user;
3537
17
  isl_val *val;
3538
17
3539
17
  val = isl_stream_read_val(s);
3540
17
  *list = isl_val_list_add(*list, val);
3541
17
  if (!*list)
3542
0
    return isl_space_free(space);
3543
17
3544
17
  return space;
3545
17
}
3546
3547
/* Read an isl_multi_val from "s".
3548
 *
3549
 * We first read a tuple space, collecting the element values in a list.
3550
 * Then we create an isl_multi_val from the space and the isl_val_list.
3551
 */
3552
__isl_give isl_multi_val *isl_stream_read_multi_val(__isl_keep isl_stream *s)
3553
7
{
3554
7
  struct vars *v;
3555
7
  isl_set *dom = NULL;
3556
7
  isl_space *space;
3557
7
  isl_multi_val *mv = NULL;
3558
7
  isl_val_list *list;
3559
7
3560
7
  v = vars_new(s->ctx);
3561
7
  if (!v)
3562
0
    return NULL;
3563
7
3564
7
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3565
7
  if (next_is_tuple(s)) {
3566
1
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3567
1
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3568
0
      goto error;
3569
7
  }
3570
7
  if (!isl_set_plain_is_universe(dom))
3571
7
    
isl_die0
(s->ctx, isl_error_invalid,
3572
7
      "expecting universe parameter domain", goto error);
3573
7
  if (isl_stream_eat(s, '{'))
3574
0
    goto error;
3575
7
3576
7
  space = isl_set_get_space(dom);
3577
7
3578
7
  list = isl_val_list_alloc(s->ctx, 0);
3579
7
  space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list);
3580
7
  mv = isl_multi_val_from_val_list(space, list);
3581
7
3582
7
  if (isl_stream_eat(s, '}'))
3583
0
    goto error;
3584
7
3585
7
  vars_free(v);
3586
7
  isl_set_free(dom);
3587
7
  return mv;
3588
0
error:
3589
0
  vars_free(v);
3590
0
  isl_set_free(dom);
3591
0
  isl_multi_val_free(mv);
3592
0
  return NULL;
3593
7
}
3594
3595
/* Read an isl_multi_val from "str".
3596
 */
3597
__isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx,
3598
  const char *str)
3599
7
{
3600
7
  isl_multi_val *mv;
3601
7
  isl_stream *s = isl_stream_new_str(ctx, str);
3602
7
  if (!s)
3603
0
    return NULL;
3604
7
  mv = isl_stream_read_multi_val(s);
3605
7
  isl_stream_free(s);
3606
7
  return mv;
3607
7
}
3608
3609
/* Read a multi-affine expression from "s".
3610
 * If the multi-affine expression has a domain, then the tuple
3611
 * representing this domain cannot involve any affine expressions.
3612
 * The tuple representing the actual expressions needs to consist
3613
 * of only affine expressions.  Moreover, these expressions can
3614
 * only depend on parameters and input dimensions and not on other
3615
 * output dimensions.
3616
 */
3617
__isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3618
124
{
3619
124
  struct vars *v;
3620
124
  isl_set *dom = NULL;
3621
124
  isl_multi_pw_aff *tuple = NULL;
3622
124
  int dim, i, n;
3623
124
  isl_space *space, *dom_space;
3624
124
  isl_multi_aff *ma = NULL;
3625
124
3626
124
  v = vars_new(s->ctx);
3627
124
  if (!v)
3628
0
    return NULL;
3629
124
3630
124
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3631
124
  if (next_is_tuple(s)) {
3632
10
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3633
10
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3634
0
      goto error;
3635
124
  }
3636
124
  if (!isl_set_plain_is_universe(dom))
3637
124
    
isl_die0
(s->ctx, isl_error_invalid,
3638
124
      "expecting universe parameter domain", goto error);
3639
124
  if (isl_stream_eat(s, '{'))
3640
0
    goto error;
3641
124
3642
124
  tuple = read_tuple(s, v, 0, 0);
3643
124
  if (!tuple)
3644
0
    goto error;
3645
124
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3646
116
    isl_set *set;
3647
116
    isl_space *space;
3648
116
    int has_expr;
3649
116
3650
116
    has_expr = tuple_has_expr(tuple);
3651
116
    if (has_expr < 0)
3652
0
      goto error;
3653
116
    if (has_expr)
3654
116
      
isl_die0
(s->ctx, isl_error_invalid,
3655
116
        "expecting universe domain", goto error);
3656
116
    space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3657
116
    set = isl_set_universe(space);
3658
116
    dom = isl_set_intersect_params(set, dom);
3659
116
    isl_multi_pw_aff_free(tuple);
3660
116
    tuple = read_tuple(s, v, 0, 0);
3661
116
    if (!tuple)
3662
0
      goto error;
3663
124
  }
3664
124
3665
124
  if (isl_stream_eat(s, '}'))
3666
0
    goto error;
3667
124
3668
124
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3669
124
  dim = isl_set_dim(dom, isl_dim_all);
3670
124
  dom_space = isl_set_get_space(dom);
3671
124
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3672
124
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3673
124
  if (!isl_space_is_params(dom_space))
3674
116
    space = isl_space_map_from_domain_and_range(
3675
116
        isl_space_copy(dom_space), space);
3676
124
  isl_space_free(dom_space);
3677
124
  ma = isl_multi_aff_alloc(space);
3678
124
3679
258
  for (i = 0; i < n; 
++i134
) {
3680
134
    isl_pw_aff *pa;
3681
134
    isl_aff *aff;
3682
134
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3683
134
    aff = aff_from_pw_aff(pa);
3684
134
    if (!aff)
3685
0
      goto error;
3686
134
    if (isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)) {
3687
0
      isl_aff_free(aff);
3688
0
      isl_die(s->ctx, isl_error_invalid,
3689
0
        "not an affine expression", goto error);
3690
0
    }
3691
134
    aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3692
134
    space = isl_multi_aff_get_domain_space(ma);
3693
134
    aff = isl_aff_reset_domain_space(aff, space);
3694
134
    ma = isl_multi_aff_set_aff(ma, i, aff);
3695
134
  }
3696
124
3697
124
  isl_multi_pw_aff_free(tuple);
3698
124
  vars_free(v);
3699
124
  isl_set_free(dom);
3700
124
  return ma;
3701
0
error:
3702
0
  isl_multi_pw_aff_free(tuple);
3703
0
  vars_free(v);
3704
0
  isl_set_free(dom);
3705
0
  isl_multi_aff_free(ma);
3706
0
  return NULL;
3707
124
}
3708
3709
__isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3710
  const char *str)
3711
61
{
3712
61
  isl_multi_aff *maff;
3713
61
  isl_stream *s = isl_stream_new_str(ctx, str);
3714
61
  if (!s)
3715
0
    return NULL;
3716
61
  maff = isl_stream_read_multi_aff(s);
3717
61
  isl_stream_free(s);
3718
61
  return maff;
3719
61
}
3720
3721
/* Read an isl_multi_pw_aff from "s".
3722
 *
3723
 * The input format is similar to that of map, except that any conditions
3724
 * on the domains should be specified inside the tuple since each
3725
 * piecewise affine expression may have a different domain.
3726
 * However, additional, shared conditions can also be specified.
3727
 * This is especially useful for setting the explicit domain
3728
 * of a zero-dimensional isl_multi_pw_aff.
3729
 *
3730
 * Since we do not know in advance if the isl_multi_pw_aff lives
3731
 * in a set or a map space, we first read the first tuple and check
3732
 * if it is followed by a "->".  If so, we convert the tuple into
3733
 * the domain of the isl_multi_pw_aff and read in the next tuple.
3734
 * This tuple (or the first tuple if it was not followed by a "->")
3735
 * is then converted into the isl_multi_pw_aff through a call
3736
 * to extract_mpa_from_tuple and the domain of the result
3737
 * is intersected with the domain.
3738
 */
3739
__isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3740
  __isl_keep isl_stream *s)
3741
140
{
3742
140
  struct vars *v;
3743
140
  isl_set *dom = NULL;
3744
140
  isl_multi_pw_aff *tuple = NULL;
3745
140
  isl_multi_pw_aff *mpa = NULL;
3746
140
3747
140
  v = vars_new(s->ctx);
3748
140
  if (!v)
3749
0
    return NULL;
3750
140
3751
140
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3752
140
  if (next_is_tuple(s)) {
3753
32
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3754
32
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3755
0
      goto error;
3756
140
  }
3757
140
  if (isl_stream_eat(s, '{'))
3758
0
    goto error;
3759
140
3760
140
  tuple = read_tuple(s, v, 0, 0);
3761
140
  if (!tuple)
3762
0
    goto error;
3763
140
  if (isl_stream_eat_if_available(s, ISL_TOKEN_TO)) {
3764
124
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3765
124
    dom = isl_map_domain(map);
3766
124
    tuple = read_tuple(s, v, 0, 0);
3767
124
    if (!tuple)
3768
0
      goto error;
3769
140
  }
3770
140
3771
140
  if (isl_stream_eat_if_available(s, ':'))
3772
63
    dom = read_formula(s, v, dom, 0);
3773
140
3774
140
  if (isl_stream_eat(s, '}'))
3775
0
    goto error;
3776
140
3777
140
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3778
140
3779
140
  isl_multi_pw_aff_free(tuple);
3780
140
  vars_free(v);
3781
140
  mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3782
140
  return mpa;
3783
0
error:
3784
0
  isl_multi_pw_aff_free(tuple);
3785
0
  vars_free(v);
3786
0
  isl_set_free(dom);
3787
0
  isl_multi_pw_aff_free(mpa);
3788
0
  return NULL;
3789
140
}
3790
3791
/* Read an isl_multi_pw_aff from "str".
3792
 */
3793
__isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3794
  const char *str)
3795
140
{
3796
140
  isl_multi_pw_aff *mpa;
3797
140
  isl_stream *s = isl_stream_new_str(ctx, str);
3798
140
  if (!s)
3799
0
    return NULL;
3800
140
  mpa = isl_stream_read_multi_pw_aff(s);
3801
140
  isl_stream_free(s);
3802
140
  return mpa;
3803
140
}
3804
3805
/* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3806
 */
3807
static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3808
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3809
155
{
3810
155
  isl_pw_aff *pa;
3811
155
  isl_union_pw_aff *upa = NULL;
3812
155
  isl_set *aff_dom;
3813
155
  int n;
3814
155
3815
155
  n = v->n;
3816
155
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3817
155
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3818
155
  vars_drop(v, v->n - n);
3819
155
3820
155
  upa = isl_union_pw_aff_from_pw_aff(pa);
3821
155
3822
229
  while (isl_stream_eat_if_available(s, ';')) {
3823
74
    isl_pw_aff *pa_i;
3824
74
    isl_union_pw_aff *upa_i;
3825
74
3826
74
    n = v->n;
3827
74
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3828
74
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3829
74
    vars_drop(v, v->n - n);
3830
74
3831
74
    upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3832
74
    upa = isl_union_pw_aff_union_add(upa, upa_i);
3833
74
  }
3834
155
3835
155
  isl_set_free(dom);
3836
155
  return upa;
3837
155
}
3838
3839
/* Read an isl_union_pw_aff from "s".
3840
 *
3841
 * First check if there are any paramters, then read in the opening brace
3842
 * and use read_union_pw_aff_with_dom to read in the body of
3843
 * the isl_union_pw_aff.  Finally, read the closing brace.
3844
 */
3845
__isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3846
  __isl_keep isl_stream *s)
3847
11
{
3848
11
  struct vars *v;
3849
11
  isl_set *dom;
3850
11
  isl_union_pw_aff *upa = NULL;
3851
11
3852
11
  v = vars_new(s->ctx);
3853
11
  if (!v)
3854
0
    return NULL;
3855
11
3856
11
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3857
11
  if (next_is_tuple(s)) {
3858
10
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3859
10
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3860
0
      goto error;
3861
11
  }
3862
11
  if (isl_stream_eat(s, '{'))
3863
0
    goto error;
3864
11
3865
11
  upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3866
11
3867
11
  if (isl_stream_eat(s, '}'))
3868
0
    goto error;
3869
11
3870
11
  vars_free(v);
3871
11
  isl_set_free(dom);
3872
11
  return upa;
3873
0
error:
3874
0
  vars_free(v);
3875
0
  isl_set_free(dom);
3876
0
  isl_union_pw_aff_free(upa);
3877
0
  return NULL;
3878
11
}
3879
3880
/* Read an isl_union_pw_aff from "str".
3881
 */
3882
__isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3883
  const char *str)
3884
11
{
3885
11
  isl_union_pw_aff *upa;
3886
11
  isl_stream *s = isl_stream_new_str(ctx, str);
3887
11
  if (!s)
3888
0
    return NULL;
3889
11
  upa = isl_stream_read_union_pw_aff(s);
3890
11
  isl_stream_free(s);
3891
11
  return upa;
3892
11
}
3893
3894
/* This function is called for each element in a tuple inside
3895
 * isl_stream_read_multi_union_pw_aff.
3896
 *
3897
 * Read a '{', the union piecewise affine expression body and a '}' and
3898
 * add the isl_union_pw_aff to *list.
3899
 */
3900
static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3901
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3902
144
{
3903
144
  isl_set *dom;
3904
144
  isl_union_pw_aff *upa;
3905
144
  isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3906
144
3907
144
  dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3908
144
  if (isl_stream_eat(s, '{'))
3909
0
    goto error;
3910
144
  upa = read_union_pw_aff_with_dom(s, dom, v);
3911
144
  *list = isl_union_pw_aff_list_add(*list, upa);
3912
144
  if (isl_stream_eat(s, '}'))
3913
0
    return isl_space_free(space);
3914
144
  if (!*list)
3915
0
    return isl_space_free(space);
3916
144
  return space;
3917
0
error:
3918
0
  isl_set_free(dom);
3919
0
  return isl_space_free(space);
3920
144
}
3921
3922
/* Do the next tokens in "s" correspond to an empty tuple?
3923
 * In particular, does the stream start with a '[', followed by a ']',
3924
 * not followed by a "->"?
3925
 */
3926
static int next_is_empty_tuple(__isl_keep isl_stream *s)
3927
266
{
3928
266
  struct isl_token *tok, *tok2, *tok3;
3929
266
  int is_empty_tuple = 0;
3930
266
3931
266
  tok = isl_stream_next_token(s);
3932
266
  if (!tok)
3933
0
    return 0;
3934
266
  if (tok->type != '[') {
3935
118
    isl_stream_push_token(s, tok);
3936
118
    return 0;
3937
118
  }
3938
148
3939
148
  tok2 = isl_stream_next_token(s);
3940
148
  if (tok2 && tok2->type == ']') {
3941
12
    tok3 = isl_stream_next_token(s);
3942
12
    is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3943
12
    if (tok3)
3944
10
      isl_stream_push_token(s, tok3);
3945
12
  }
3946
148
  if (tok2)
3947
148
    isl_stream_push_token(s, tok2);
3948
148
  isl_stream_push_token(s, tok);
3949
148
3950
148
  return is_empty_tuple;
3951
148
}
3952
3953
/* Do the next tokens in "s" correspond to a tuple of parameters?
3954
 * In particular, does the stream start with a '[' that is not
3955
 * followed by a '{' or a nested tuple?
3956
 */
3957
static int next_is_param_tuple(__isl_keep isl_stream *s)
3958
254
{
3959
254
  struct isl_token *tok, *tok2;
3960
254
  int is_tuple;
3961
254
3962
254
  tok = isl_stream_next_token(s);
3963
254
  if (!tok)
3964
0
    return 0;
3965
254
  if (tok->type != '[' || 
next_is_tuple(s)136
) {
3966
128
    isl_stream_push_token(s, tok);
3967
128
    return 0;
3968
128
  }
3969
126
3970
126
  tok2 = isl_stream_next_token(s);
3971
126
  is_tuple = tok2 && tok2->type != '{';
3972
126
  if (tok2)
3973
126
    isl_stream_push_token(s, tok2);
3974
126
  isl_stream_push_token(s, tok);
3975
126
3976
126
  return is_tuple;
3977
126
}
3978
3979
/* Read the core of a body of an isl_multi_union_pw_aff from "s",
3980
 * i.e., everything except the parameter specification and
3981
 * without shared domain constraints.
3982
 * "v" contains a description of the identifiers parsed so far.
3983
 * The parameters, if any, are specified by "space".
3984
 *
3985
 * The body is of the form
3986
 *
3987
 *  [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3988
 *
3989
 * Read the tuple, collecting the individual isl_union_pw_aff
3990
 * elements in a list and construct the result from the tuple space and
3991
 * the list.
3992
 */
3993
static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body_core(
3994
  __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
3995
254
{
3996
254
  isl_union_pw_aff_list *list;
3997
254
  isl_multi_union_pw_aff *mupa;
3998
254
3999
254
  list = isl_union_pw_aff_list_alloc(s->ctx, 0);
4000
254
  space = read_tuple_space(s, v, space, 1, 0,
4001
254
        &read_union_pw_aff_el, &list);
4002
254
  mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
4003
254
4004
254
  return mupa;
4005
254
}
4006
4007
/* Read the body of an isl_union_set from "s",
4008
 * i.e., everything except the parameter specification.
4009
 * "v" contains a description of the identifiers parsed so far.
4010
 * The parameters, if any, are specified by "space".
4011
 *
4012
 * First read a generic disjunction of object bodies and then try and extract
4013
 * an isl_union_set from that.
4014
 */
4015
static __isl_give isl_union_set *read_union_set_body(__isl_keep isl_stream *s,
4016
  struct vars *v, __isl_take isl_space *space)
4017
71
{
4018
71
  struct isl_obj obj = { isl_obj_set, NULL };
4019
71
  isl_map *map;
4020
71
4021
71
  map = isl_set_universe(space);
4022
71
  if (isl_stream_eat(s, '{') < 0)
4023
0
    goto error;
4024
71
  obj = obj_read_disjuncts(s, v, map);
4025
71
  if (isl_stream_eat(s, '}') < 0)
4026
0
    goto error;
4027
71
  isl_map_free(map);
4028
71
4029
71
  return extract_union_set(s->ctx, obj);
4030
0
error:
4031
0
  obj.type->free(obj.v);
4032
0
  isl_map_free(map);
4033
0
  return NULL;
4034
71
}
4035
4036
/* Read the body of an isl_multi_union_pw_aff from "s",
4037
 * i.e., everything except the parameter specification.
4038
 * "v" contains a description of the identifiers parsed so far.
4039
 * The parameters, if any, are specified by "space".
4040
 *
4041
 * In particular, handle the special case with shared domain constraints.
4042
 * These are specified as
4043
 *
4044
 *  ([...] : ...)
4045
 *
4046
 * and are especially useful for setting the explicit domain
4047
 * of a zero-dimensional isl_multi_union_pw_aff.
4048
 * The core isl_multi_union_pw_aff body ([...]) is read by
4049
 * read_multi_union_pw_aff_body_core.
4050
 */
4051
static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_body(
4052
  __isl_keep isl_stream *s, struct vars *v, __isl_take isl_space *space)
4053
254
{
4054
254
  isl_multi_union_pw_aff *mupa;
4055
254
4056
254
  if (!isl_stream_next_token_is(s, '('))
4057
181
    return read_multi_union_pw_aff_body_core(s, v, space);
4058
73
4059
73
  if (isl_stream_eat(s, '(') < 0)
4060
0
    goto error;
4061
73
  mupa = read_multi_union_pw_aff_body_core(s, v, isl_space_copy(space));
4062
73
  if (isl_stream_eat_if_available(s, ':')) {
4063
71
    isl_union_set *dom;
4064
71
4065
71
    dom = read_union_set_body(s, v, space);
4066
71
    mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4067
71
  } else {
4068
2
    isl_space_free(space);
4069
2
  }
4070
73
  if (isl_stream_eat(s, ')') < 0)
4071
0
    return isl_multi_union_pw_aff_free(mupa);
4072
73
4073
73
  return mupa;
4074
0
error:
4075
0
  isl_space_free(space);
4076
0
  return NULL;
4077
73
}
4078
4079
/* Read an isl_multi_union_pw_aff from "s".
4080
 *
4081
 * The input has the form
4082
 *
4083
 *  [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4084
 *
4085
 * or
4086
 *
4087
 *  [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
4088
 *
4089
 * Additionally, a shared domain may be specified as
4090
 *
4091
 *  ([..] : ...)
4092
 *
4093
 * or
4094
 *
4095
 *  [..] -> ([..] : ...)
4096
 *
4097
 * The first case is handled by the caller, the second case
4098
 * is handled by read_multi_union_pw_aff_body.
4099
 *
4100
 * We first check for the special case of an empty tuple "[]".
4101
 * Then we check if there are any parameters.
4102
 * Finally, read the tuple and construct the result.
4103
 */
4104
static __isl_give isl_multi_union_pw_aff *read_multi_union_pw_aff_core(
4105
  __isl_keep isl_stream *s)
4106
266
{
4107
266
  struct vars *v;
4108
266
  isl_set *dom = NULL;
4109
266
  isl_space *space;
4110
266
  isl_multi_union_pw_aff *mupa = NULL;
4111
266
4112
266
  if (next_is_empty_tuple(s)) {
4113
12
    if (isl_stream_eat(s, '['))
4114
0
      return NULL;
4115
12
    if (isl_stream_eat(s, ']'))
4116
0
      return NULL;
4117
12
    space = isl_space_set_alloc(s->ctx, 0, 0);
4118
12
    return isl_multi_union_pw_aff_zero(space);
4119
12
  }
4120
254
4121
254
  v = vars_new(s->ctx);
4122
254
  if (!v)
4123
0
    return NULL;
4124
254
4125
254
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
4126
254
  if (next_is_param_tuple(s)) {
4127
93
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
4128
93
    if (isl_stream_eat(s, ISL_TOKEN_TO))
4129
0
      goto error;
4130
254
  }
4131
254
  space = isl_set_get_space(dom);
4132
254
  isl_set_free(dom);
4133
254
  mupa = read_multi_union_pw_aff_body(s, v, space);
4134
254
4135
254
  vars_free(v);
4136
254
4137
254
  return mupa;
4138
0
error:
4139
0
  vars_free(v);
4140
0
  isl_set_free(dom);
4141
0
  isl_multi_union_pw_aff_free(mupa);
4142
0
  return NULL;
4143
254
}
4144
4145
/* Read an isl_multi_union_pw_aff from "s".
4146
 *
4147
 * In particular, handle the special case with shared domain constraints.
4148
 * These are specified as
4149
 *
4150
 *  ([...] : ...)
4151
 *
4152
 * and are especially useful for setting the explicit domain
4153
 * of a zero-dimensional isl_multi_union_pw_aff.
4154
 * The core isl_multi_union_pw_aff ([...]) is read by
4155
 * read_multi_union_pw_aff_core.
4156
 */
4157
__isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
4158
  __isl_keep isl_stream *s)
4159
266
{
4160
266
  isl_multi_union_pw_aff *mupa;
4161
266
4162
266
  if (!isl_stream_next_token_is(s, '('))
4163
213
    return read_multi_union_pw_aff_core(s);
4164
53
4165
53
  if (isl_stream_eat(s, '(') < 0)
4166
0
    return NULL;
4167
53
  mupa = read_multi_union_pw_aff_core(s);
4168
53
  if (isl_stream_eat_if_available(s, ':')) {
4169
53
    isl_union_set *dom;
4170
53
4171
53
    dom = isl_stream_read_union_set(s);
4172
53
    mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
4173
53
  }
4174
53
  if (isl_stream_eat(s, ')') < 0)
4175
0
    return isl_multi_union_pw_aff_free(mupa);
4176
53
  return mupa;
4177
53
}
4178
4179
/* Read an isl_multi_union_pw_aff from "str".
4180
 */
4181
__isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4182
  isl_ctx *ctx, const char *str)
4183
266
{
4184
266
  isl_multi_union_pw_aff *mupa;
4185
266
  isl_stream *s = isl_stream_new_str(ctx, str);
4186
266
  if (!s)
4187
0
    return NULL;
4188
266
  mupa = isl_stream_read_multi_union_pw_aff(s);
4189
266
  isl_stream_free(s);
4190
266
  return mupa;
4191
266
}
4192
4193
__isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4194
  __isl_keep isl_stream *s)
4195
2
{
4196
2
  struct isl_obj obj;
4197
2
4198
2
  obj = obj_read(s);
4199
2
  if (obj.type == isl_obj_pw_qpolynomial) {
4200
1
    obj.type = isl_obj_union_pw_qpolynomial;
4201
1
    obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4202
1
  }
4203
2
  if (obj.v)
4204
2
    isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4205
2
         goto error);
4206
2
4207
2
  return obj.v;
4208
0
error:
4209
0
  obj.type->free(obj.v);
4210
0
  return NULL;
4211
2
}
4212
4213
__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4214
  isl_ctx *ctx, const char *str)
4215
2
{
4216
2
  isl_union_pw_qpolynomial *upwqp;
4217
2
  isl_stream *s = isl_stream_new_str(ctx, str);
4218
2
  if (!s)
4219
0
    return NULL;
4220
2
  upwqp = isl_stream_read_union_pw_qpolynomial(s);
4221
2
  isl_stream_free(s);
4222
2
  return upwqp;
4223
2
}