Coverage Report

Created: 2017-06-23 12:40

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