Coverage Report

Created: 2017-08-21 19:50

/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.36k
{
47
2.36k
  struct vars *v;
48
2.36k
  v = isl_alloc_type(ctx, struct vars);
49
2.36k
  if (!v)
50
0
    return NULL;
51
2.36k
  v->ctx = ctx;
52
2.36k
  v->n = 0;
53
2.36k
  v->v = NULL;
54
2.36k
  return v;
55
2.36k
}
56
57
static void variable_free(struct variable *var)
58
2.36k
{
59
3.37k
  while (
var3.37k
)
{1.01k
60
1.01k
    struct variable *next = var->next;
61
1.01k
    free(var->name);
62
1.01k
    free(var);
63
1.01k
    var = next;
64
2.36k
  }
65
2.36k
}
66
67
static void vars_free(struct vars *v)
68
2.36k
{
69
2.36k
  if (!v)
70
0
    return;
71
2.36k
  variable_free(v->v);
72
2.36k
  free(v);
73
2.36k
}
74
75
static void vars_drop(struct vars *v, int n)
76
2.73k
{
77
2.73k
  struct variable *var;
78
2.73k
79
2.73k
  if (
!v || 2.73k
!v->v2.73k
)
80
121
    return;
81
2.73k
82
2.73k
  v->n -= n;
83
2.61k
84
2.61k
  var = v->v;
85
8.95k
  while (
--n >= 08.95k
)
{6.34k
86
6.34k
    struct variable *next = var->next;
87
6.34k
    free(var->name);
88
6.34k
    free(var);
89
6.34k
    var = next;
90
6.34k
  }
91
2.61k
  v->v = var;
92
2.73k
}
93
94
static struct variable *variable_new(struct vars *v, const char *name, int len,
95
        int pos)
96
7.35k
{
97
7.35k
  struct variable *var;
98
7.35k
  var = isl_calloc_type(v->ctx, struct variable);
99
7.35k
  if (!var)
100
0
    goto error;
101
7.35k
  var->name = strdup(name);
102
7.35k
  var->name[len] = '\0';
103
7.35k
  var->pos = pos;
104
7.35k
  var->next = v->v;
105
7.35k
  return var;
106
7.35k
error:
107
0
  variable_free(v->v);
108
7.35k
  return NULL;
109
7.35k
}
110
111
static int vars_pos(struct vars *v, const char *s, int len)
112
13.2k
{
113
13.2k
  int pos;
114
13.2k
  struct variable *q;
115
13.2k
116
13.2k
  if (len == -1)
117
13.2k
    len = strlen(s);
118
43.3k
  for (q = v->v; 
q43.3k
;
q = q->next30.0k
)
{38.8k
119
38.8k
    if (
strncmp(q->name, s, len) == 0 && 38.8k
q->name[len] == '\0'9.11k
)
120
8.79k
      break;
121
38.8k
  }
122
13.2k
  if (q)
123
8.79k
    pos = q->pos;
124
13.2k
  else {
125
4.46k
    pos = v->n;
126
4.46k
    v->v = variable_new(v, s, len, v->n);
127
4.46k
    if (!v->v)
128
0
      return -1;
129
4.46k
    v->n++;
130
13.2k
  }
131
13.2k
  return pos;
132
13.2k
}
133
134
static int vars_add_anon(struct vars *v)
135
2.88k
{
136
2.88k
  v->v = variable_new(v, "", 0, v->n);
137
2.88k
138
2.88k
  if (!v->v)
139
0
    return -1;
140
2.88k
  v->n++;
141
2.88k
142
2.88k
  return 0;
143
2.88k
}
144
145
/* Obtain next token, with some preprocessing.
146
 * In particular, evaluate expressions of the form x^y,
147
 * with x and y values.
148
 */
149
static struct isl_token *next_token(__isl_keep isl_stream *s)
150
46.2k
{
151
46.2k
  struct isl_token *tok, *tok2;
152
46.2k
153
46.2k
  tok = isl_stream_next_token(s);
154
46.2k
  if (
!tok || 46.2k
tok->type != ISL_TOKEN_VALUE46.2k
)
155
37.0k
    return tok;
156
9.15k
  
if (9.15k
!isl_stream_eat_if_available(s, '^')9.15k
)
157
9.15k
    return tok;
158
9.15k
  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
46.2k
}
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
342
  }
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
342
  }
201
292
  
if (292
tok->type == '-' &&292
202
292
      
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
292
  }
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
260
  }
210
212
  
if (212
tok->type != ISL_TOKEN_VALUE212
)
{0
211
0
    isl_stream_error(s, tok, "expecting value");
212
0
    goto error;
213
212
  }
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
53
    }
221
53
    
if (53
tok2->type != ISL_TOKEN_VALUE53
)
{0
222
0
      isl_stream_error(s, tok2, "expecting value");
223
0
      goto error;
224
53
    }
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
212
  } else {
228
159
    val = isl_val_int_from_isl_int(s->ctx, tok->u.v);
229
212
  }
230
212
231
212
  isl_token_free(tok);
232
212
  isl_token_free(tok2);
233
212
  return val;
234
212
error:
235
0
  isl_token_free(tok);
236
0
  isl_token_free(tok2);
237
212
  return NULL;
238
342
}
239
240
/* Read an isl_val from "str".
241
 */
242
struct isl_val *isl_val_read_from_str(struct isl_ctx *ctx,
243
  const char *str)
244
325
{
245
325
  isl_val *val;
246
325
  isl_stream *s = isl_stream_new_str(ctx, str);
247
325
  if (!s)
248
0
    return NULL;
249
325
  val = isl_stream_read_val(s);
250
325
  isl_stream_free(s);
251
325
  return val;
252
325
}
253
254
static int accept_cst_factor(__isl_keep isl_stream *s, isl_int *f)
255
325
{
256
325
  struct isl_token *tok;
257
325
258
325
  tok = next_token(s);
259
325
  if (
!tok || 325
tok->type != ISL_TOKEN_VALUE325
)
{0
260
0
    isl_stream_error(s, tok, "expecting constant value");
261
0
    goto error;
262
325
  }
263
325
264
325
  
isl_int_mul325
(*f, *f, tok->u.v);325
265
325
266
325
  isl_token_free(tok);
267
325
268
325
  if (isl_stream_eat_if_available(s, '*'))
269
0
    return accept_cst_factor(s, f);
270
325
271
325
  return 0;
272
325
error:
273
0
  isl_token_free(tok);
274
325
  return -1;
275
325
}
276
277
/* Given an affine expression aff, return an affine expression
278
 * for aff % d, with d the next token on the stream, which is
279
 * assumed to be a constant.
280
 *
281
 * We introduce an integer division q = [aff/d] and the result
282
 * is set to aff - d q.
283
 */
284
static __isl_give isl_pw_aff *affine_mod(__isl_keep isl_stream *s,
285
  struct vars *v, __isl_take isl_pw_aff *aff)
286
97
{
287
97
  struct isl_token *tok;
288
97
  isl_pw_aff *q;
289
97
290
97
  tok = next_token(s);
291
97
  if (
!tok || 97
tok->type != ISL_TOKEN_VALUE97
)
{0
292
0
    isl_stream_error(s, tok, "expecting constant value");
293
0
    goto error;
294
97
  }
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
97
error:
306
0
  isl_pw_aff_free(aff);
307
0
  isl_token_free(tok);
308
97
  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
6
  return min ? 
isl_pw_aff_list_min(list)5
:
isl_pw_aff_list_max(list)1
;
341
6
error:
342
0
  isl_space_free(dim);
343
0
  isl_pw_aff_list_free(list);
344
6
  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.8k
{
351
13.8k
  if (!tok)
352
0
    return 0;
353
13.8k
  
if (13.8k
tok->type == '['13.8k
)
354
266
    return 1;
355
13.5k
  
if (13.5k
tok->type == ISL_TOKEN_FLOOR13.5k
)
356
216
    return 1;
357
13.3k
  
if (13.3k
tok->type == ISL_TOKEN_CEIL13.3k
)
358
0
    return 1;
359
13.3k
  
if (13.3k
tok->type == ISL_TOKEN_FLOORD13.3k
)
360
2
    return 1;
361
13.3k
  
if (13.3k
tok->type == ISL_TOKEN_CEILD13.3k
)
362
0
    return 1;
363
13.3k
  return 0;
364
13.8k
}
365
366
/* Read an integer division from "s" and return it as an isl_pw_aff.
367
 *
368
 * The integer division can be of the form
369
 *
370
 *  [<affine expression>]
371
 *  floor(<affine expression>)
372
 *  ceil(<affine expression>)
373
 *  floord(<affine expression>,<denominator>)
374
 *  ceild(<affine expression>,<denominator>)
375
 */
376
static __isl_give isl_pw_aff *accept_div(__isl_keep isl_stream *s,
377
  __isl_take isl_space *dim, struct vars *v)
378
299
{
379
299
  struct isl_token *tok;
380
299
  int f = 0;
381
299
  int c = 0;
382
299
  int extra = 0;
383
299
  isl_pw_aff *pwaff = NULL;
384
299
385
299
  if (isl_stream_eat_if_available(s, ISL_TOKEN_FLOORD))
386
1
    extra = f = 1;
387
298
  else 
if (298
isl_stream_eat_if_available(s, ISL_TOKEN_CEILD)298
)
388
0
    extra = c = 1;
389
298
  else 
if (298
isl_stream_eat_if_available(s, ISL_TOKEN_FLOOR)298
)
390
142
    f = 1;
391
156
  else 
if (156
isl_stream_eat_if_available(s, ISL_TOKEN_CEIL)156
)
392
0
    c = 1;
393
299
  if (
f || 299
c156
)
{143
394
143
    if (isl_stream_eat(s, '('))
395
0
      goto error;
396
299
  } else {
397
156
    if (isl_stream_eat(s, '['))
398
0
      goto error;
399
299
  }
400
299
401
299
  pwaff = accept_affine(s, isl_space_copy(dim), v);
402
299
403
299
  if (
extra299
)
{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
1
    }
415
1
    pwaff = isl_pw_aff_scale_down(pwaff,  tok->u.v);
416
1
    isl_token_free(tok);
417
299
  }
418
299
419
299
  
if (299
c299
)
420
0
    pwaff = isl_pw_aff_ceil(pwaff);
421
299
  else
422
299
    pwaff = isl_pw_aff_floor(pwaff);
423
299
424
299
  if (
f || 299
c156
)
{143
425
143
    if (isl_stream_eat(s, ')'))
426
0
      goto error;
427
299
  } else {
428
156
    if (isl_stream_eat(s, ']'))
429
0
      goto error;
430
299
  }
431
299
432
299
  isl_space_free(dim);
433
299
  return pwaff;
434
299
error:
435
0
  isl_space_free(dim);
436
0
  isl_pw_aff_free(pwaff);
437
299
  return NULL;
438
299
}
439
440
static __isl_give isl_pw_aff *accept_affine_factor(__isl_keep isl_stream *s,
441
  __isl_take isl_space *dim, struct vars *v)
442
8.31k
{
443
8.31k
  struct isl_token *tok = NULL;
444
8.31k
  isl_pw_aff *res = NULL;
445
8.31k
446
8.31k
  tok = next_token(s);
447
8.31k
  if (
!tok8.31k
)
{0
448
0
    isl_stream_error(s, NULL, "unexpected EOF");
449
0
    goto error;
450
8.31k
  }
451
8.31k
452
8.31k
  
if (8.31k
tok->type == ISL_TOKEN_AFF8.31k
)
{179
453
179
    res = isl_pw_aff_copy(tok->u.pwaff);
454
179
    isl_token_free(tok);
455
8.31k
  } else 
if (8.13k
tok->type == ISL_TOKEN_IDENT8.13k
)
{7.65k
456
7.65k
    int n = v->n;
457
7.65k
    int pos = vars_pos(v, tok->u.s, -1);
458
7.65k
    isl_aff *aff;
459
7.65k
460
7.65k
    if (pos < 0)
461
0
      goto error;
462
7.65k
    
if (7.65k
pos >= n7.65k
)
{0
463
0
      vars_drop(v, v->n - n);
464
0
      isl_stream_error(s, tok, "unknown identifier");
465
0
      goto error;
466
7.65k
    }
467
7.65k
468
7.65k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(isl_space_copy(dim)));
469
7.65k
    if (!aff)
470
0
      goto error;
471
7.65k
    
isl_int_set_si7.65k
(aff->v->el[2 + pos], 1);7.65k
472
7.65k
    res = isl_pw_aff_from_aff(aff);
473
7.65k
    isl_token_free(tok);
474
8.13k
  } else 
if (487
tok->type == ISL_TOKEN_VALUE487
)
{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
487
  } else 
if (487
tok->type == '('487
)
{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
487
  } else 
if (276
is_start_of_div(tok)276
)
{270
496
270
    isl_stream_push_token(s, tok);
497
270
    tok = NULL;
498
270
    res = accept_div(s, isl_space_copy(dim), v);
499
276
  } 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
6
  } else {
504
0
    isl_stream_error(s, tok, "expecting factor");
505
0
    goto error;
506
8.31k
  }
507
8.31k
  
if (8.31k
isl_stream_eat_if_available(s, '%') ||8.31k
508
8.31k
      
isl_stream_eat_if_available(s, ISL_TOKEN_MOD)8.27k
)
{97
509
97
    isl_space_free(dim);
510
97
    return affine_mod(s, v, res);
511
8.31k
  }
512
8.22k
  
if (8.22k
isl_stream_eat_if_available(s, '*')8.22k
)
{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
7
    }
520
7
    res = isl_pw_aff_scale(res, f);
521
7
    isl_int_clear(f);
522
8.22k
  }
523
8.22k
  
if (8.22k
isl_stream_eat_if_available(s, '/')8.22k
)
{318
524
318
    isl_int f;
525
318
    isl_int_init(f);
526
318
    isl_int_set_si(f, 1);
527
318
    if (
accept_cst_factor(s, &f) < 0318
)
{0
528
0
      isl_int_clear(f);
529
0
      goto error2;
530
318
    }
531
318
    res = isl_pw_aff_scale_down(res, f);
532
318
    isl_int_clear(f);
533
8.22k
  }
534
8.22k
535
8.22k
  isl_space_free(dim);
536
8.22k
  return res;
537
8.22k
error:
538
0
  isl_token_free(tok);
539
0
error2:
540
0
  isl_pw_aff_free(res);
541
0
  isl_space_free(dim);
542
0
  return NULL;
543
8.31k
}
544
545
static __isl_give isl_pw_aff *add_cst(__isl_take isl_pw_aff *pwaff, isl_int v)
546
5.38k
{
547
5.38k
  isl_aff *aff;
548
5.38k
  isl_space *space;
549
5.38k
550
5.38k
  space = isl_pw_aff_get_domain_space(pwaff);
551
5.38k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
552
5.38k
  aff = isl_aff_add_constant(aff, v);
553
5.38k
554
5.38k
  return isl_pw_aff_add(pwaff, isl_pw_aff_from_aff(aff));
555
5.38k
}
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.7k
{
571
11.7k
  struct isl_token *tok = NULL;
572
11.7k
  isl_local_space *ls;
573
11.7k
  isl_pw_aff *res;
574
11.7k
  int sign = 1;
575
11.7k
576
11.7k
  ls = isl_local_space_from_space(isl_space_copy(space));
577
11.7k
  res = isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
578
11.7k
  if (!res)
579
0
    goto error;
580
11.7k
581
11.7k
  
for (;;) 11.7k
{13.8k
582
13.8k
    tok = next_token(s);
583
13.8k
    if (
!tok13.8k
)
{0
584
0
      isl_stream_error(s, NULL, "unexpected EOF");
585
0
      goto error;
586
13.8k
    }
587
13.8k
    
if (13.8k
tok->type == '-'13.8k
)
{73
588
73
      sign = -sign;
589
73
      isl_token_free(tok);
590
73
      continue;
591
13.8k
    }
592
13.7k
    
if (13.7k
tok->type == '(' || 13.7k
is_start_of_div(tok)13.5k
||
593
13.7k
        
tok->type == ISL_TOKEN_MIN13.3k
||
tok->type == ISL_TOKEN_MAX13.3k
||
594
13.3k
        tok->type == ISL_TOKEN_IDENT ||
595
13.7k
        
tok->type == ISL_TOKEN_AFF6.85k
)
{7.06k
596
7.06k
      isl_pw_aff *term;
597
7.06k
      isl_stream_push_token(s, tok);
598
7.06k
      tok = NULL;
599
7.06k
      term = accept_affine_factor(s,
600
7.06k
                isl_space_copy(space), v);
601
7.06k
      if (sign < 0)
602
400
        res = isl_pw_aff_sub(res, term);
603
7.06k
      else
604
6.66k
        res = isl_pw_aff_add(res, term);
605
7.06k
      if (!res)
606
0
        goto error;
607
7.06k
      sign = 1;
608
13.7k
    } else 
if (6.67k
tok->type == ISL_TOKEN_VALUE6.67k
)
{6.64k
609
6.64k
      if (sign < 0)
610
234
        isl_int_neg(tok->u.v, tok->u.v);
611
6.64k
      if (isl_stream_eat_if_available(s, '*') ||
612
6.64k
          
isl_stream_next_token_is(s, ISL_TOKEN_IDENT)6.54k
)
{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
6.64k
      } else {
621
5.38k
        res = add_cst(res, tok->u.v);
622
6.64k
      }
623
6.64k
      sign = 1;
624
6.67k
    } else 
if (31
tok->type == ISL_TOKEN_NAN31
)
{30
625
30
      res = isl_pw_aff_add(res, nan_on_domain(space));
626
31
    } 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
13.7k
    }
633
13.7k
    isl_token_free(tok);
634
13.7k
635
13.7k
    tok = next_token(s);
636
13.7k
    if (
tok && 13.7k
tok->type == '-'13.7k
)
{561
637
561
      sign = -sign;
638
561
      isl_token_free(tok);
639
13.7k
    } else 
if (13.1k
tok && 13.1k
tok->type == '+'13.1k
)
{1.41k
640
1.41k
      /* nothing */
641
1.41k
      isl_token_free(tok);
642
13.1k
    } else 
if (11.7k
tok && 11.7k
tok->type == ISL_TOKEN_VALUE11.7k
&&
643
11.7k
         
isl_int_is_neg37
(tok->u.v))
{37
644
37
      isl_stream_push_token(s, tok);
645
11.7k
    } else {
646
11.7k
      if (tok)
647
11.7k
        isl_stream_push_token(s, tok);
648
11.7k
      break;
649
13.7k
    }
650
13.7k
  }
651
11.7k
652
11.7k
  isl_space_free(space);
653
11.7k
  return res;
654
11.7k
error:
655
0
  isl_space_free(space);
656
0
  isl_token_free(tok);
657
0
  isl_pw_aff_free(res);
658
11.7k
  return NULL;
659
11.7k
}
660
661
/* Is "type" the type of a comparison operator between lists
662
 * of affine expressions?
663
 */
664
static int is_list_comparator_type(int type)
665
19.3k
{
666
19.3k
  switch (type) {
667
19.3k
  case ISL_TOKEN_LEX_LT:
668
12
  case ISL_TOKEN_LEX_GT:
669
12
  case ISL_TOKEN_LEX_LE:
670
12
  case ISL_TOKEN_LEX_GE:
671
12
    return 1;
672
19.3k
  default:
673
19.3k
    return 0;
674
19.3k
  }
675
19.3k
}
676
677
static int is_comparator(struct isl_token *tok)
678
10.8k
{
679
10.8k
  if (!tok)
680
0
    return 0;
681
10.8k
  
if (10.8k
is_list_comparator_type(tok->type)10.8k
)
682
4
    return 1;
683
10.8k
684
10.8k
  switch (tok->type) {
685
10.8k
  case ISL_TOKEN_LT:
686
4.23k
  case ISL_TOKEN_GT:
687
4.23k
  case ISL_TOKEN_LE:
688
4.23k
  case ISL_TOKEN_GE:
689
4.23k
  case ISL_TOKEN_NE:
690
4.23k
  case '=':
691
4.23k
    return 1;
692
6.63k
  default:
693
10.8k
    return 0;
694
10.8k
  }
695
10.8k
}
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
1
error:
732
0
  isl_map_free(cond);
733
0
  isl_pw_aff_free(pwaff1);
734
0
  isl_pw_aff_free(pwaff2);
735
1
  return NULL;
736
1
}
737
738
/* Set *line and *col to those of the next token, if any.
739
 */
740
static void set_current_line_col(__isl_keep isl_stream *s, int *line, int *col)
741
6.00k
{
742
6.00k
  struct isl_token *tok;
743
6.00k
744
6.00k
  tok = isl_stream_next_token(s);
745
6.00k
  if (!tok)
746
0
    return;
747
6.00k
748
6.00k
  *line = tok->line;
749
6.00k
  *col = tok->col;
750
6.00k
  isl_stream_push_token(s, tok);
751
6.00k
}
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
43
error:
770
0
  isl_pw_aff_free(pa);
771
43
  return -1;
772
43
}
773
774
/* Accept an affine expression that may involve ternary operators.
775
 * We first read an affine expression.
776
 * If it is not followed by a comparison operator, we simply return it.
777
 * Otherwise, we assume the affine expression is part of the first
778
 * argument of a ternary operator and try to parse that.
779
 */
780
static __isl_give isl_pw_aff *accept_extended_affine(__isl_keep isl_stream *s,
781
  __isl_take isl_space *dim, struct vars *v, int rational)
782
3.06k
{
783
3.06k
  isl_space *space;
784
3.06k
  isl_map *cond;
785
3.06k
  isl_pw_aff *pwaff;
786
3.06k
  struct isl_token *tok;
787
3.06k
  int line = -1, col = -1;
788
3.06k
  int is_comp;
789
3.06k
790
3.06k
  set_current_line_col(s, &line, &col);
791
3.06k
792
3.06k
  pwaff = accept_affine(s, dim, v);
793
3.06k
  if (rational)
794
4
    pwaff = isl_pw_aff_set_rational(pwaff);
795
3.06k
  if (!pwaff)
796
0
    return NULL;
797
3.06k
798
3.06k
  tok = isl_stream_next_token(s);
799
3.06k
  if (!tok)
800
0
    return isl_pw_aff_free(pwaff);
801
3.06k
802
3.06k
  is_comp = is_comparator(tok);
803
3.06k
  isl_stream_push_token(s, tok);
804
3.06k
  if (!is_comp)
805
3.06k
    return pwaff;
806
3.06k
807
3.06k
  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
3.06k
}
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.81k
{
852
7.81k
  isl_pw_aff *pwaff;
853
7.81k
  isl_pw_aff_list *list;
854
7.81k
  struct isl_token *tok = NULL;
855
7.81k
856
7.81k
  pwaff = accept_affine(s, isl_space_copy(dim), v);
857
7.81k
  list = isl_pw_aff_list_from_pw_aff(pwaff);
858
7.81k
  if (!list)
859
1
    goto error;
860
7.81k
861
7.81k
  
for (;;) 7.81k
{7.95k
862
7.95k
    tok = isl_stream_next_token(s);
863
7.95k
    if (
!tok7.95k
)
{0
864
0
      isl_stream_error(s, NULL, "unexpected EOF");
865
0
      goto error;
866
7.95k
    }
867
7.95k
    
if (7.95k
tok->type != ','7.95k
)
{7.81k
868
7.81k
      isl_stream_push_token(s, tok);
869
7.81k
      break;
870
7.95k
    }
871
7.95k
    isl_token_free(tok);
872
137
873
137
    pwaff = accept_affine(s, isl_space_copy(dim), v);
874
137
    list = isl_pw_aff_list_concat(list,
875
137
        isl_pw_aff_list_from_pw_aff(pwaff));
876
137
    if (!list)
877
0
      goto error;
878
7.81k
  }
879
7.81k
880
7.81k
  isl_space_free(dim);
881
7.81k
  return list;
882
7.81k
error:
883
1
  isl_space_free(dim);
884
1
  isl_pw_aff_list_free(list);
885
7.81k
  return NULL;
886
7.81k
}
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
215
    }
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
215
    }
917
215
918
215
    if (
!tok || 215
tok->type != ','215
)
919
131
      break;
920
215
921
215
    isl_token_free(tok);
922
131
  }
923
131
  
if (131
tok131
)
924
131
    isl_stream_push_token(s, tok);
925
131
926
131
  return map;
927
131
error:
928
0
  isl_token_free(tok);
929
0
  isl_map_free(map);
930
131
  return NULL;
931
131
}
932
933
static int next_is_tuple(__isl_keep isl_stream *s)
934
9.71k
{
935
9.71k
  struct isl_token *tok;
936
9.71k
  int is_tuple;
937
9.71k
938
9.71k
  tok = isl_stream_next_token(s);
939
9.71k
  if (!tok)
940
0
    return 0;
941
9.71k
  
if (9.71k
tok->type == '['9.71k
)
{1.92k
942
1.92k
    isl_stream_push_token(s, tok);
943
1.92k
    return 1;
944
9.71k
  }
945
7.78k
  
if (7.78k
tok->type != ISL_TOKEN_IDENT && 7.78k
!tok->is_keyword2.07k
)
{2.02k
946
2.02k
    isl_stream_push_token(s, tok);
947
2.02k
    return 0;
948
7.78k
  }
949
7.78k
950
7.78k
  is_tuple = isl_stream_next_token_is(s, '[');
951
5.75k
952
5.75k
  isl_stream_push_token(s, tok);
953
5.75k
954
7.78k
  return is_tuple;
955
9.71k
}
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
4.23k
{
1012
4.23k
  char *prime;
1013
4.23k
1014
4.23k
  if (!name)
1015
0
    return space;
1016
4.23k
1017
4.23k
  prime = strchr(name, '\'');
1018
4.23k
  if (prime)
1019
32
    *prime = '\0';
1020
4.23k
  space = isl_space_set_dim_name(space, isl_dim_out, pos, name);
1021
4.23k
  if (prime)
1022
32
    *prime = '\'';
1023
4.23k
1024
4.23k
  return space;
1025
4.23k
}
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.93k
{
1051
2.93k
  isl_pw_aff *res;
1052
2.93k
  isl_space *res_space;
1053
2.93k
1054
2.93k
  res_space = isl_space_from_domain(isl_space_copy(space));
1055
2.93k
  res_space = isl_space_add_dims(res_space, isl_dim_out, 1);
1056
2.93k
  res = isl_pw_aff_empty(res_space);
1057
2.93k
  do {
1058
2.93k
    isl_pw_aff *pa;
1059
2.93k
    int seen_paren;
1060
2.93k
    int line = -1, col = -1;
1061
2.93k
1062
2.93k
    set_current_line_col(s, &line, &col);
1063
2.93k
    seen_paren = isl_stream_eat_if_available(s, '(');
1064
2.93k
    if (seen_paren)
1065
2.93k
      pa = accept_piecewise_affine(s, isl_space_copy(space),
1066
2.93k
              v, rational);
1067
2.93k
    else
1068
2.93k
      pa = accept_extended_affine(s, isl_space_copy(space),
1069
2.93k
              v, rational);
1070
2.93k
    if (
seen_paren && 2.93k
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
2.93k
    }
1077
2.93k
    
if (2.93k
isl_stream_eat_if_available(s, ':')2.93k
)
{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
2.93k
    }
1086
2.93k
1087
2.93k
    res = isl_pw_aff_union_add(res, pa);
1088
2.93k
1089
2.93k
    if (
seen_paren && 2.93k
isl_stream_eat(s, ')')0
)
1090
0
      goto error;
1091
2.93k
  } while (isl_stream_eat_if_available(s, ';'));
1092
2.93k
1093
2.93k
  isl_space_free(space);
1094
2.93k
1095
2.93k
  return res;
1096
2.93k
error:
1097
0
  isl_space_free(space);
1098
2.93k
  return isl_pw_aff_free(res);
1099
2.93k
}
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.89k
{
1111
2.89k
  isl_space *space;
1112
2.89k
  isl_pw_aff *def;
1113
2.89k
1114
2.89k
  space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
1115
2.89k
1116
2.89k
  def = accept_piecewise_affine(s, space, v, rational);
1117
2.89k
1118
2.89k
  space = isl_space_set_alloc(s->ctx, 0, v->n);
1119
2.89k
  def = isl_pw_aff_reset_domain_space(def, space);
1120
2.89k
1121
2.89k
  return def;
1122
2.89k
}
1123
1124
/* Read a list of tuple elements by calling "read_el" on each of them and
1125
 * return a space with the same number of set dimensions derived from
1126
 * the parameter space "space" and possibly updated by "read_el".
1127
 * The elements in the list are separated by either "," or "][".
1128
 * If "comma" is set then only "," is allowed.
1129
 */
1130
static __isl_give isl_space *read_tuple_list(__isl_keep isl_stream *s,
1131
  struct vars *v, __isl_take isl_space *space, int rational, int comma,
1132
  __isl_give isl_space *(*read_el)(__isl_keep isl_stream *s,
1133
    struct vars *v, __isl_take isl_space *space, int rational,
1134
    void *user),
1135
  void *user)
1136
5.18k
{
1137
5.18k
  if (!space)
1138
0
    return NULL;
1139
5.18k
1140
5.18k
  space = isl_space_set_from_params(space);
1141
5.18k
1142
5.18k
  if (isl_stream_next_token_is(s, ']'))
1143
792
    return space;
1144
5.18k
1145
4.39k
  
for (;;) 4.39k
{7.15k
1146
7.15k
    struct isl_token *tok;
1147
7.15k
1148
7.15k
    space = isl_space_add_dims(space, isl_dim_set, 1);
1149
7.15k
1150
7.15k
    space = read_el(s, v, space, rational, user);
1151
7.15k
    if (!space)
1152
0
      return NULL;
1153
7.15k
1154
7.15k
    tok = isl_stream_next_token(s);
1155
7.15k
    if (
!comma && 7.15k
tok7.15k
&&
tok->type == ']'7.15k
&&
1156
7.15k
        
isl_stream_next_token_is(s, '[')4.39k
)
{0
1157
0
      isl_token_free(tok);
1158
0
      tok = isl_stream_next_token(s);
1159
7.15k
    } else 
if (7.15k
!tok || 7.15k
tok->type != ','7.15k
)
{4.39k
1160
4.39k
      if (tok)
1161
4.39k
        isl_stream_push_token(s, tok);
1162
4.39k
      break;
1163
7.15k
    }
1164
7.15k
1165
7.15k
    isl_token_free(tok);
1166
4.39k
  }
1167
4.39k
1168
4.39k
  return space;
1169
5.18k
}
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.40k
{
1181
5.40k
  struct isl_token *tok;
1182
5.40k
  char *name = NULL;
1183
5.40k
  isl_space *res = NULL;
1184
5.40k
1185
5.40k
  tok = isl_stream_next_token(s);
1186
5.40k
  if (!tok)
1187
0
    goto error;
1188
5.40k
  
if (5.40k
tok->type == ISL_TOKEN_IDENT || 5.40k
tok->is_keyword2.74k
)
{2.66k
1189
2.66k
    name = strdup(tok->u.s);
1190
2.66k
    isl_token_free(tok);
1191
2.66k
    if (!name)
1192
0
      goto error;
1193
5.40k
  } else
1194
2.74k
    isl_stream_push_token(s, tok);
1195
5.40k
  
if (5.40k
isl_stream_eat(s, '[')5.40k
)
1196
0
    goto error;
1197
5.40k
  
if (5.40k
next_is_tuple(s)5.40k
)
{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
5.40k
  } else
1207
5.40k
    res = read_tuple_list(s, v, isl_space_copy(space),
1208
5.40k
          rational, comma, read_el, user);
1209
5.40k
  
if (5.40k
isl_stream_eat(s, ']')5.40k
)
1210
2
    goto error;
1211
5.40k
1212
5.40k
  
if (5.40k
name5.40k
)
{2.66k
1213
2.66k
    res = isl_space_set_tuple_name(res, isl_dim_set, name);
1214
2.66k
    free(name);
1215
5.40k
  }
1216
5.40k
1217
5.40k
  isl_space_free(space);
1218
5.40k
  return res;
1219
5.40k
error:
1220
2
  free(name);
1221
2
  isl_space_free(res);
1222
2
  isl_space_free(space);
1223
5.40k
  return NULL;
1224
5.40k
}
1225
1226
/* Construct an isl_pw_aff defined on a space with v->n variables
1227
 * that is equal to the last of those variables.
1228
 */
1229
static __isl_give isl_pw_aff *identity_tuple_el(struct vars *v)
1230
4.22k
{
1231
4.22k
  isl_space *space;
1232
4.22k
  isl_aff *aff;
1233
4.22k
1234
4.22k
  space = isl_space_set_alloc(v->ctx, 0, v->n);
1235
4.22k
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1236
4.22k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n - 1, 1);
1237
4.22k
  return isl_pw_aff_from_aff(aff);
1238
4.22k
}
1239
1240
/* This function is called for each element in a tuple inside read_tuple.
1241
 * Add a new variable to "v" and construct a corresponding isl_pw_aff defined
1242
 * over a space containing all variables in "v" defined so far.
1243
 * The isl_pw_aff expresses the new variable in terms of earlier variables
1244
 * if a definition is provided.  Otherwise, it is represented as being
1245
 * equal to itself.
1246
 * Add the isl_pw_aff to *list.
1247
 * If the new variable was named, then adjust "space" accordingly and
1248
 * return the updated space.
1249
 */
1250
static __isl_give isl_space *read_tuple_pw_aff_el(__isl_keep isl_stream *s,
1251
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
1252
7.11k
{
1253
7.11k
  isl_pw_aff_list **list = (isl_pw_aff_list **) user;
1254
7.11k
  isl_pw_aff *pa;
1255
7.11k
  struct isl_token *tok;
1256
7.11k
  int new_name = 0;
1257
7.11k
1258
7.11k
  tok = next_token(s);
1259
7.11k
  if (
!tok7.11k
)
{0
1260
0
    isl_stream_error(s, NULL, "unexpected EOF");
1261
0
    return isl_space_free(space);
1262
7.11k
  }
1263
7.11k
1264
7.11k
  
if (7.11k
tok->type == ISL_TOKEN_IDENT7.11k
)
{5.35k
1265
5.35k
    int n = v->n;
1266
5.35k
    int p = vars_pos(v, tok->u.s, -1);
1267
5.35k
    if (p < 0)
1268
0
      goto error;
1269
5.35k
    new_name = p >= n;
1270
7.11k
  }
1271
7.11k
1272
7.11k
  
if (7.11k
tok->type == '*'7.11k
)
{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
7.11k
  } else 
if (7.11k
new_name7.11k
)
{4.23k
1278
4.23k
    int pos = isl_space_dim(space, isl_dim_out) - 1;
1279
4.23k
    space = space_set_dim_name(space, pos, v->v->name);
1280
4.23k
    isl_token_free(tok);
1281
4.23k
    if (isl_stream_eat_if_available(s, '='))
1282
10
      pa = read_tuple_var_def(s, v, rational);
1283
4.23k
    else
1284
4.22k
      pa = identity_tuple_el(v);
1285
7.11k
  } else {
1286
2.88k
    isl_stream_push_token(s, tok);
1287
2.88k
    tok = NULL;
1288
2.88k
    if (vars_add_anon(v) < 0)
1289
0
      goto error;
1290
2.88k
    pa = read_tuple_var_def(s, v, rational);
1291
7.11k
  }
1292
7.11k
1293
7.11k
  *list = isl_pw_aff_list_add(*list, pa);
1294
7.11k
  if (!*list)
1295
0
    return isl_space_free(space);
1296
7.11k
1297
7.11k
  return space;
1298
7.11k
error:
1299
0
  isl_token_free(tok);
1300
7.11k
  return isl_space_free(space);
1301
7.11k
}
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.94k
{
1321
4.94k
  int i, n;
1322
4.94k
  isl_space *space;
1323
4.94k
  isl_pw_aff_list *list;
1324
4.94k
1325
4.94k
  space = isl_space_params_alloc(v->ctx, 0);
1326
4.94k
  list = isl_pw_aff_list_alloc(s->ctx, 0);
1327
4.94k
  space = read_tuple_space(s, v, space, rational, comma,
1328
4.94k
        &read_tuple_pw_aff_el, &list);
1329
4.94k
  n = isl_space_dim(space, isl_dim_set);
1330
7.73k
  for (i = 0; 
i + 1 < n7.73k
;
++i2.79k
)
{2.79k
1331
2.79k
    isl_pw_aff *pa;
1332
2.79k
1333
2.79k
    pa = isl_pw_aff_list_get_pw_aff(list, i);
1334
2.79k
    pa = isl_pw_aff_add_dims(pa, isl_dim_in, n - (i + 1));
1335
2.79k
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
1336
4.94k
  }
1337
4.94k
1338
4.94k
  space = isl_space_from_range(space);
1339
4.94k
  space = isl_space_add_dims(space, isl_dim_in, v->n);
1340
4.94k
  return isl_multi_pw_aff_from_pw_aff_list(space, list);
1341
4.94k
}
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.67k
{
1353
4.67k
  int i, n;
1354
4.67k
  isl_ctx *ctx;
1355
4.67k
  isl_space *space = NULL;
1356
4.67k
1357
4.67k
  if (
!map || 4.67k
!tuple4.67k
)
1358
0
    goto error;
1359
4.67k
  ctx = isl_multi_pw_aff_get_ctx(tuple);
1360
4.67k
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
1361
4.67k
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
1362
4.67k
  if (!space)
1363
0
    goto error;
1364
4.67k
1365
4.67k
  
if (4.67k
type == isl_dim_param4.67k
)
{570
1366
570
    if (isl_space_has_tuple_name(space, isl_dim_set) ||
1367
570
        
isl_space_is_wrapping(space)570
)
{0
1368
0
      isl_die(ctx, isl_error_invalid,
1369
0
        "parameter tuples cannot be named or nested",
1370
0
        goto error);
1371
570
    }
1372
570
    map = isl_map_add_dims(map, type, n);
1373
1.32k
    for (i = 0; 
i < n1.32k
;
++i751
)
{751
1374
751
      isl_id *id;
1375
751
      if (!isl_space_has_dim_name(space, isl_dim_set, i))
1376
0
        isl_die(ctx, isl_error_invalid,
1377
751
          "parameters must be named",
1378
751
          goto error);
1379
751
      id = isl_space_get_dim_id(space, isl_dim_set, i);
1380
751
      map = isl_map_set_dim_id(map, isl_dim_param, i, id);
1381
751
    }
1382
4.67k
  } else 
if (4.10k
type == isl_dim_in4.10k
)
{2.49k
1383
2.49k
    isl_set *set;
1384
2.49k
1385
2.49k
    set = isl_set_universe(isl_space_copy(space));
1386
2.49k
    if (rational)
1387
22
      set = isl_set_set_rational(set);
1388
2.49k
    set = isl_set_intersect_params(set, isl_map_params(map));
1389
2.49k
    map = isl_map_from_domain(set);
1390
4.10k
  } else {
1391
1.61k
    isl_set *set;
1392
1.61k
1393
1.61k
    set = isl_set_universe(isl_space_copy(space));
1394
1.61k
    if (rational)
1395
54
      set = isl_set_set_rational(set);
1396
1.61k
    map = isl_map_from_domain_and_range(isl_map_domain(map), set);
1397
4.67k
  }
1398
4.67k
1399
11.4k
  
for (i = 0; 4.67k
i < n11.4k
;
++i6.77k
)
{6.77k
1400
6.77k
    isl_pw_aff *pa;
1401
6.77k
    isl_space *space;
1402
6.77k
    isl_aff *aff;
1403
6.77k
    isl_set *set;
1404
6.77k
    isl_map *map_i;
1405
6.77k
1406
6.77k
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
1407
6.77k
    space = isl_pw_aff_get_domain_space(pa);
1408
6.77k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1409
6.77k
    aff = isl_aff_add_coefficient_si(aff,
1410
6.77k
            isl_dim_in, v->n - n + i, -1);
1411
6.77k
    pa = isl_pw_aff_add(pa, isl_pw_aff_from_aff(aff));
1412
6.77k
    if (rational)
1413
171
      pa = isl_pw_aff_set_rational(pa);
1414
6.77k
    set = isl_pw_aff_zero_set(pa);
1415
6.77k
    map_i = isl_map_from_range(set);
1416
6.77k
    map_i = isl_map_reset_space(map_i, isl_map_get_space(map));
1417
6.77k
    map = isl_map_intersect(map, map_i);
1418
6.77k
  }
1419
4.67k
1420
4.67k
  isl_space_free(space);
1421
4.67k
  isl_multi_pw_aff_free(tuple);
1422
4.67k
  return map;
1423
4.67k
error:
1424
0
  isl_space_free(space);
1425
0
  isl_multi_pw_aff_free(tuple);
1426
0
  isl_map_free(map);
1427
4.67k
  return NULL;
1428
4.67k
}
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.67k
{
1438
4.67k
  isl_multi_pw_aff *tuple;
1439
4.67k
1440
4.67k
  tuple = read_tuple(s, v, rational, comma);
1441
4.67k
  if (!tuple)
1442
2
    return isl_map_free(map);
1443
4.67k
1444
4.66k
  return map_from_tuple(tuple, map, type, v, rational);
1445
4.67k
}
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
4
  case ISL_TOKEN_LEX_LT:
1483
1
    set = isl_map_wrap(isl_map_lex_lt(space));
1484
4
    break;
1485
4
  case ISL_TOKEN_LEX_GT:
1486
1
    set = isl_map_wrap(isl_map_lex_gt(space));
1487
4
    break;
1488
4
  case ISL_TOKEN_LEX_LE:
1489
1
    set = isl_map_wrap(isl_map_lex_le(space));
1490
4
    break;
1491
4
  case ISL_TOKEN_LEX_GE:
1492
1
    set = isl_map_wrap(isl_map_lex_ge(space));
1493
4
    break;
1494
4
  default:
1495
0
    isl_multi_pw_aff_free(mpa1);
1496
0
    isl_space_free(space);
1497
0
    isl_die(isl_set_get_ctx(set), isl_error_internal,
1498
4
      "unhandled list comparison type", return NULL);
1499
4
  }
1500
4
  set = isl_set_preimage_multi_pw_aff(set, mpa1);
1501
4
  return set;
1502
4
error:
1503
0
  isl_pw_aff_list_free(left);
1504
0
  isl_pw_aff_list_free(right);
1505
4
  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.23k
{
1527
4.23k
  isl_set *cond;
1528
4.23k
1529
4.23k
  left = isl_pw_aff_list_copy(left);
1530
4.23k
  right = isl_pw_aff_list_copy(right);
1531
4.23k
  if (
rational4.23k
)
{24
1532
24
    left = isl_pw_aff_list_set_rational(left);
1533
24
    right = isl_pw_aff_list_set_rational(right);
1534
4.23k
  }
1535
4.23k
  if (is_list_comparator_type(type))
1536
4
    cond = list_cmp(set, type, left, right);
1537
4.23k
  else 
if (4.23k
type == ISL_TOKEN_LE4.23k
)
1538
2.20k
    cond = isl_pw_aff_list_le_set(left, right);
1539
2.02k
  else 
if (2.02k
type == ISL_TOKEN_GE2.02k
)
1540
1.12k
    cond = isl_pw_aff_list_ge_set(left, right);
1541
898
  else 
if (898
type == ISL_TOKEN_LT898
)
1542
338
    cond = isl_pw_aff_list_lt_set(left, right);
1543
560
  else 
if (560
type == ISL_TOKEN_GT560
)
1544
53
    cond = isl_pw_aff_list_gt_set(left, right);
1545
507
  else 
if (507
type == ISL_TOKEN_NE507
)
1546
12
    cond = isl_pw_aff_list_ne_set(left, right);
1547
507
  else
1548
495
    cond = isl_pw_aff_list_eq_set(left, right);
1549
4.23k
1550
4.23k
  return isl_set_intersect(set, cond);
1551
4.23k
}
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.57k
{
1575
3.57k
  struct isl_token *tok;
1576
3.57k
  int type;
1577
3.57k
  isl_pw_aff_list *list1 = NULL, *list2 = NULL;
1578
3.57k
  int n1, n2;
1579
3.57k
  isl_set *set;
1580
3.57k
1581
3.57k
  set = isl_map_wrap(map);
1582
3.57k
  list1 = accept_affine_list(s, isl_set_get_space(set), v);
1583
3.57k
  if (!list1)
1584
1
    goto error;
1585
3.57k
  tok = isl_stream_next_token(s);
1586
3.57k
  if (
!is_comparator(tok)3.57k
)
{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
3.57k
  }
1592
3.57k
  type = tok->type;
1593
3.57k
  isl_token_free(tok);
1594
4.23k
  for (;;) {
1595
4.23k
    list2 = accept_affine_list(s, isl_set_get_space(set), v);
1596
4.23k
    if (!list2)
1597
0
      goto error;
1598
4.23k
    n1 = isl_pw_aff_list_n_pw_aff(list1);
1599
4.23k
    n2 = isl_pw_aff_list_n_pw_aff(list2);
1600
4.23k
    if (
is_list_comparator_type(type) && 4.23k
n1 != n24
)
{0
1601
0
      isl_stream_error(s, NULL,
1602
0
          "list arguments not of same size");
1603
0
      goto error;
1604
4.23k
    }
1605
4.23k
1606
4.23k
    set = construct_constraints(set, type, list1, list2, rational);
1607
4.23k
    isl_pw_aff_list_free(list1);
1608
4.23k
    list1 = list2;
1609
4.23k
1610
4.23k
    tok = isl_stream_next_token(s);
1611
4.23k
    if (
!is_comparator(tok)4.23k
)
{3.57k
1612
3.57k
      if (tok)
1613
3.57k
        isl_stream_push_token(s, tok);
1614
3.57k
      break;
1615
4.23k
    }
1616
4.23k
    type = tok->type;
1617
665
    isl_token_free(tok);
1618
3.57k
  }
1619
3.57k
  isl_pw_aff_list_free(list1);
1620
3.57k
1621
3.57k
  return isl_set_unwrap(set);
1622
3.57k
error:
1623
1
  isl_pw_aff_list_free(list1);
1624
1
  isl_pw_aff_list_free(list2);
1625
1
  isl_set_free(set);
1626
3.57k
  return NULL;
1627
3.57k
}
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
131
error:
1650
0
  isl_map_free(map);
1651
131
  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
175
      
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
175
  }
1697
175
1698
175
  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
136
  }
1721
136
1722
136
  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
124
error2:
1734
0
  isl_pw_aff_free(pwaff);
1735
0
error:
1736
0
  isl_token_free(tok);
1737
0
  isl_map_free(map);
1738
0
  return -1;
1739
175
}
1740
1741
static __isl_give isl_map *read_conjunct(__isl_keep isl_stream *s,
1742
  struct vars *v, __isl_take isl_map *map, int rational)
1743
3.87k
{
1744
3.87k
  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.87k
1748
3.87k
  
if (3.87k
isl_stream_next_token_is(s, ISL_TOKEN_MAP)3.87k
)
{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
3.87k
  }
1758
3.87k
1759
3.71k
  
if (3.71k
isl_stream_eat_if_available(s, ISL_TOKEN_EXISTS)3.71k
)
1760
131
    return read_exists(s, v, map, rational);
1761
3.71k
1762
3.58k
  
if (3.58k
isl_stream_eat_if_available(s, ISL_TOKEN_TRUE)3.58k
)
1763
1
    return map;
1764
3.58k
1765
3.57k
  
if (3.57k
isl_stream_eat_if_available(s, ISL_TOKEN_FALSE)3.57k
)
{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
3.57k
  }
1770
3.57k
    
1771
3.57k
  return add_constraint(s, v, map, rational);
1772
3.57k
error:
1773
0
  isl_map_free(map);
1774
3.57k
  return NULL;
1775
3.87k
}
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.80k
{
1780
1.80k
  isl_map *res;
1781
1.80k
  int negate;
1782
1.80k
1783
1.80k
  negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1784
1.80k
  res = read_conjunct(s, v, isl_map_copy(map), rational);
1785
1.80k
  if (negate)
1786
1
    res = isl_map_subtract(isl_map_copy(map), res);
1787
1.80k
1788
3.87k
  while (
res && 3.87k
isl_stream_eat_if_available(s, ISL_TOKEN_AND)3.87k
)
{2.06k
1789
2.06k
    isl_map *res_i;
1790
2.06k
1791
2.06k
    negate = isl_stream_eat_if_available(s, ISL_TOKEN_NOT);
1792
2.06k
    res_i = read_conjunct(s, v, isl_map_copy(map), rational);
1793
2.06k
    if (negate)
1794
0
      res = isl_map_subtract(res, res_i);
1795
2.06k
    else
1796
2.06k
      res = isl_map_intersect(res, res_i);
1797
2.06k
  }
1798
1.80k
1799
1.80k
  isl_map_free(map);
1800
1.80k
  return res;
1801
1.80k
}
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.72k
{
1806
1.72k
  isl_map *res;
1807
1.72k
1808
1.72k
  if (isl_stream_next_token_is(s, '}'))
1809
65
    return map;
1810
1.72k
1811
1.72k
  res = read_conjuncts(s, v, isl_map_copy(map), rational);
1812
1.80k
  while (
isl_stream_eat_if_available(s, ISL_TOKEN_OR)1.80k
)
{152
1813
152
    isl_map *res_i;
1814
152
1815
152
    res_i = read_conjuncts(s, v, isl_map_copy(map), rational);
1816
152
    res = isl_map_union(res, res_i);
1817
1.65k
  }
1818
1.65k
1819
1.65k
  isl_map_free(map);
1820
1.72k
  return res;
1821
1.72k
}
1822
1823
/* Read a first order formula from "s", add the corresponding
1824
 * constraints to "map" and return the result.
1825
 *
1826
 * In particular, read a formula of the form
1827
 *
1828
 *  a
1829
 *
1830
 * or
1831
 *
1832
 *  a implies b
1833
 *
1834
 * where a and b are disjunctions.
1835
 *
1836
 * In the first case, map is replaced by
1837
 *
1838
 *  map \cap { [..] : a }
1839
 *
1840
 * In the second case, it is replaced by
1841
 *
1842
 *  (map \setminus { [..] : a}) \cup (map \cap { [..] : b })
1843
 */
1844
static __isl_give isl_map *read_formula(__isl_keep isl_stream *s,
1845
  struct vars *v, __isl_take isl_map *map, int rational)
1846
1.72k
{
1847
1.72k
  isl_map *res;
1848
1.72k
1849
1.72k
  res = read_disjuncts(s, v, isl_map_copy(map), rational);
1850
1.72k
1851
1.72k
  if (
isl_stream_eat_if_available(s, ISL_TOKEN_IMPLIES)1.72k
)
{1
1852
1
    isl_map *res2;
1853
1
1854
1
    res = isl_map_subtract(isl_map_copy(map), res);
1855
1
    res2 = read_disjuncts(s, v, map, rational);
1856
1
    res = isl_map_union(res, res2);
1857
1.72k
  } else
1858
1.71k
    isl_map_free(map);
1859
1.72k
1860
1.72k
  return res;
1861
1.72k
}
1862
1863
static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
1864
1.88k
{
1865
1.88k
  if (pos < isl_basic_map_dim(bmap, isl_dim_out))
1866
1.88k
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1867
1.88k
         isl_basic_map_dim(bmap, isl_dim_in) + pos;
1868
1.88k
  pos -= isl_basic_map_dim(bmap, isl_dim_out);
1869
429
1870
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_in))
1871
0
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
1872
429
  pos -= isl_basic_map_dim(bmap, isl_dim_in);
1873
429
1874
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_div))
1875
429
    return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
1876
0
         isl_basic_map_dim(bmap, isl_dim_in) +
1877
429
         isl_basic_map_dim(bmap, isl_dim_out) + pos;
1878
429
  pos -= isl_basic_map_dim(bmap, isl_dim_div);
1879
429
1880
429
  if (pos < isl_basic_map_dim(bmap, isl_dim_param))
1881
0
    return 1 + pos;
1882
429
1883
429
  return 0;
1884
1.88k
}
1885
1886
static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
1887
  __isl_keep isl_stream *s, __isl_take isl_basic_map *bmap)
1888
429
{
1889
429
  int j;
1890
429
  struct isl_token *tok;
1891
429
  int type;
1892
429
  int k;
1893
429
  isl_int *c;
1894
429
1895
429
  if (!bmap)
1896
0
    return NULL;
1897
429
1898
429
  tok = isl_stream_next_token(s);
1899
429
  if (
!tok || 429
tok->type != ISL_TOKEN_VALUE429
)
{0
1900
0
    isl_stream_error(s, tok, "expecting coefficient");
1901
0
    if (tok)
1902
0
      isl_stream_push_token(s, tok);
1903
0
    goto error;
1904
429
  }
1905
429
  
if (429
!tok->on_new_line429
)
{0
1906
0
    isl_stream_error(s, tok, "coefficient should appear on new line");
1907
0
    isl_stream_push_token(s, tok);
1908
0
    goto error;
1909
429
  }
1910
429
1911
429
  
type = 429
isl_int_get_si429
(tok->u.v);
1912
429
  isl_token_free(tok);
1913
429
1914
429
  isl_assert(s->ctx, type == 0 || type == 1, goto error);
1915
429
  
if (429
type == 0429
)
{30
1916
30
    k = isl_basic_map_alloc_equality(bmap);
1917
30
    c = bmap->eq[k];
1918
429
  } else {
1919
399
    k = isl_basic_map_alloc_inequality(bmap);
1920
399
    c = bmap->ineq[k];
1921
429
  }
1922
429
  if (k < 0)
1923
0
    goto error;
1924
429
1925
2.31k
  
for (j = 0; 429
j < 1 + isl_basic_map_total_dim(bmap)2.31k
;
++j1.88k
)
{1.88k
1926
1.88k
    int pos;
1927
1.88k
    tok = isl_stream_next_token(s);
1928
1.88k
    if (
!tok || 1.88k
tok->type != ISL_TOKEN_VALUE1.88k
)
{0
1929
0
      isl_stream_error(s, tok, "expecting coefficient");
1930
0
      if (tok)
1931
0
        isl_stream_push_token(s, tok);
1932
0
      goto error;
1933
1.88k
    }
1934
1.88k
    
if (1.88k
tok->on_new_line1.88k
)
{0
1935
0
      isl_stream_error(s, tok,
1936
0
        "coefficient should not appear on new line");
1937
0
      isl_stream_push_token(s, tok);
1938
0
      goto error;
1939
1.88k
    }
1940
1.88k
    pos = polylib_pos_to_isl_pos(bmap, j);
1941
1.88k
    isl_int_set(c[pos], tok->u.v);
1942
1.88k
    isl_token_free(tok);
1943
1.88k
  }
1944
429
1945
429
  return bmap;
1946
429
error:
1947
0
  isl_basic_map_free(bmap);
1948
429
  return NULL;
1949
429
}
1950
1951
static __isl_give isl_basic_map *basic_map_read_polylib(
1952
  __isl_keep isl_stream *s)
1953
105
{
1954
105
  int i;
1955
105
  struct isl_token *tok;
1956
105
  struct isl_token *tok2;
1957
105
  int n_row, n_col;
1958
105
  int on_new_line;
1959
105
  unsigned in = 0, out, local = 0;
1960
105
  struct isl_basic_map *bmap = NULL;
1961
105
  int nparam = 0;
1962
105
1963
105
  tok = isl_stream_next_token(s);
1964
105
  if (
!tok105
)
{0
1965
0
    isl_stream_error(s, NULL, "unexpected EOF");
1966
0
    return NULL;
1967
105
  }
1968
105
  tok2 = isl_stream_next_token(s);
1969
105
  if (
!tok2105
)
{0
1970
0
    isl_token_free(tok);
1971
0
    isl_stream_error(s, NULL, "unexpected EOF");
1972
0
    return NULL;
1973
105
  }
1974
105
  
if (105
tok->type != ISL_TOKEN_VALUE || 105
tok2->type != ISL_TOKEN_VALUE105
)
{0
1975
0
    isl_stream_push_token(s, tok2);
1976
0
    isl_stream_push_token(s, tok);
1977
0
    isl_stream_error(s, NULL,
1978
0
         "expecting constraint matrix dimensions");
1979
0
    return NULL;
1980
105
  }
1981
105
  
n_row = 105
isl_int_get_si105
(tok->u.v);
1982
105
  n_col = isl_int_get_si(tok2->u.v);
1983
105
  on_new_line = tok2->on_new_line;
1984
105
  isl_token_free(tok2);
1985
105
  isl_token_free(tok);
1986
105
  isl_assert(s->ctx, !on_new_line, return NULL);
1987
105
  
isl_assert105
(s->ctx, n_row >= 0, return NULL);105
1988
105
  
isl_assert105
(s->ctx, n_col >= 2 + nparam, return NULL);105
1989
105
  tok = isl_stream_next_token_on_same_line(s);
1990
105
  if (
tok105
)
{0
1991
0
    if (
tok->type != ISL_TOKEN_VALUE0
)
{0
1992
0
      isl_stream_error(s, tok,
1993
0
            "expecting number of output dimensions");
1994
0
      isl_stream_push_token(s, tok);
1995
0
      goto error;
1996
0
    }
1997
0
    
out = 0
isl_int_get_si0
(tok->u.v);
1998
0
    isl_token_free(tok);
1999
0
2000
0
    tok = isl_stream_next_token_on_same_line(s);
2001
0
    if (
!tok || 0
tok->type != ISL_TOKEN_VALUE0
)
{0
2002
0
      isl_stream_error(s, tok,
2003
0
            "expecting number of input dimensions");
2004
0
      if (tok)
2005
0
        isl_stream_push_token(s, tok);
2006
0
      goto error;
2007
0
    }
2008
0
    
in = 0
isl_int_get_si0
(tok->u.v);
2009
0
    isl_token_free(tok);
2010
0
2011
0
    tok = isl_stream_next_token_on_same_line(s);
2012
0
    if (
!tok || 0
tok->type != ISL_TOKEN_VALUE0
)
{0
2013
0
      isl_stream_error(s, tok,
2014
0
            "expecting number of existentials");
2015
0
      if (tok)
2016
0
        isl_stream_push_token(s, tok);
2017
0
      goto error;
2018
0
    }
2019
0
    
local = 0
isl_int_get_si0
(tok->u.v);
2020
0
    isl_token_free(tok);
2021
0
2022
0
    tok = isl_stream_next_token_on_same_line(s);
2023
0
    if (
!tok || 0
tok->type != ISL_TOKEN_VALUE0
)
{0
2024
0
      isl_stream_error(s, tok,
2025
0
            "expecting number of parameters");
2026
0
      if (tok)
2027
0
        isl_stream_push_token(s, tok);
2028
0
      goto error;
2029
0
    }
2030
0
    
nparam = 0
isl_int_get_si0
(tok->u.v);
2031
0
    isl_token_free(tok);
2032
0
    if (
n_col != 1 + out + in + local + nparam + 10
)
{0
2033
0
      isl_stream_error(s, NULL,
2034
0
            "dimensions don't match");
2035
0
      goto error;
2036
0
    }
2037
105
  } else
2038
105
    out = n_col - 2 - nparam;
2039
105
  bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
2040
105
  if (!bmap)
2041
0
    return NULL;
2042
105
2043
105
  
for (i = 0; 105
i < local105
;
++i0
)
{0
2044
0
    int k = isl_basic_map_alloc_div(bmap);
2045
0
    if (k < 0)
2046
0
      goto error;
2047
0
    isl_seq_clr(bmap->div[k], 1 + 1 + nparam + in + out + local);
2048
105
  }
2049
105
2050
534
  
for (i = 0; 105
i < n_row534
;
++i429
)
2051
429
    bmap = basic_map_read_polylib_constraint(s, bmap);
2052
105
2053
105
  tok = isl_stream_next_token_on_same_line(s);
2054
105
  if (
tok105
)
{0
2055
0
    isl_stream_error(s, tok, "unexpected extra token on line");
2056
0
    isl_stream_push_token(s, tok);
2057
0
    goto error;
2058
105
  }
2059
105
2060
105
  bmap = isl_basic_map_simplify(bmap);
2061
105
  bmap = isl_basic_map_finalize(bmap);
2062
105
  return bmap;
2063
105
error:
2064
0
  isl_basic_map_free(bmap);
2065
105
  return NULL;
2066
105
}
2067
2068
static struct isl_map *map_read_polylib(__isl_keep isl_stream *s)
2069
105
{
2070
105
  struct isl_token *tok;
2071
105
  struct isl_token *tok2;
2072
105
  int i, n;
2073
105
  struct isl_map *map;
2074
105
2075
105
  tok = isl_stream_next_token(s);
2076
105
  if (
!tok105
)
{0
2077
0
    isl_stream_error(s, NULL, "unexpected EOF");
2078
0
    return NULL;
2079
105
  }
2080
105
  tok2 = isl_stream_next_token_on_same_line(s);
2081
105
  if (
tok2 && 105
tok2->type == ISL_TOKEN_VALUE105
)
{105
2082
105
    isl_stream_push_token(s, tok2);
2083
105
    isl_stream_push_token(s, tok);
2084
105
    return isl_map_from_basic_map(basic_map_read_polylib(s));
2085
105
  }
2086
0
  
if (0
tok20
)
{0
2087
0
    isl_stream_error(s, tok2, "unexpected token");
2088
0
    isl_stream_push_token(s, tok2);
2089
0
    isl_stream_push_token(s, tok);
2090
0
    return NULL;
2091
0
  }
2092
0
  
n = 0
isl_int_get_si0
(tok->u.v);
2093
0
  isl_token_free(tok);
2094
0
2095
0
  isl_assert(s->ctx, n >= 1, return NULL);
2096
0
2097
0
  map = isl_map_from_basic_map(basic_map_read_polylib(s));
2098
0
2099
0
  for (i = 1; 
map && 0
i < n0
;
++i0
)
2100
0
    map = isl_map_union(map,
2101
0
      isl_map_from_basic_map(basic_map_read_polylib(s)));
2102
0
2103
0
  return map;
2104
105
}
2105
2106
static int optional_power(__isl_keep isl_stream *s)
2107
66
{
2108
66
  int pow;
2109
66
  struct isl_token *tok;
2110
66
2111
66
  tok = isl_stream_next_token(s);
2112
66
  if (!tok)
2113
0
    return 1;
2114
66
  
if (66
tok->type != '^'66
)
{60
2115
60
    isl_stream_push_token(s, tok);
2116
60
    return 1;
2117
66
  }
2118
66
  isl_token_free(tok);
2119
6
  tok = isl_stream_next_token(s);
2120
6
  if (
!tok || 6
tok->type != ISL_TOKEN_VALUE6
)
{0
2121
0
    isl_stream_error(s, tok, "expecting exponent");
2122
0
    if (tok)
2123
0
      isl_stream_push_token(s, tok);
2124
0
    return 1;
2125
6
  }
2126
6
  
pow = 6
isl_int_get_si6
(tok->u.v);
2127
6
  isl_token_free(tok);
2128
6
  return pow;
2129
66
}
2130
2131
static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2132
  __isl_keep isl_map *map, struct vars *v);
2133
2134
static __isl_give isl_pw_qpolynomial *read_factor(__isl_keep isl_stream *s,
2135
  __isl_keep isl_map *map, struct vars *v)
2136
84
{
2137
84
  isl_pw_qpolynomial *pwqp;
2138
84
  struct isl_token *tok;
2139
84
2140
84
  tok = next_token(s);
2141
84
  if (
!tok84
)
{0
2142
0
    isl_stream_error(s, NULL, "unexpected EOF");
2143
0
    return NULL;
2144
84
  }
2145
84
  
if (84
tok->type == '('84
)
{21
2146
21
    int pow;
2147
21
2148
21
    isl_token_free(tok);
2149
21
    pwqp = read_term(s, map, v);
2150
21
    if (!pwqp)
2151
0
      return NULL;
2152
21
    
if (21
isl_stream_eat(s, ')')21
)
2153
0
      goto error;
2154
21
    pow = optional_power(s);
2155
21
    pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2156
84
  } else 
if (63
tok->type == ISL_TOKEN_VALUE63
)
{15
2157
15
    struct isl_token *tok2;
2158
15
    isl_qpolynomial *qp;
2159
15
2160
15
    tok2 = isl_stream_next_token(s);
2161
15
    if (
tok2 && 15
tok2->type == '/'15
)
{4
2162
4
      isl_token_free(tok2);
2163
4
      tok2 = next_token(s);
2164
4
      if (
!tok2 || 4
tok2->type != ISL_TOKEN_VALUE4
)
{0
2165
0
        isl_stream_error(s, tok2, "expected denominator");
2166
0
        isl_token_free(tok);
2167
0
        isl_token_free(tok2);
2168
0
        return NULL;
2169
4
      }
2170
4
      qp = isl_qpolynomial_rat_cst_on_domain(isl_map_get_space(map),
2171
4
                tok->u.v, tok2->u.v);
2172
4
      isl_token_free(tok2);
2173
15
    } else {
2174
11
      isl_stream_push_token(s, tok2);
2175
11
      qp = isl_qpolynomial_cst_on_domain(isl_map_get_space(map),
2176
11
            tok->u.v);
2177
15
    }
2178
15
    isl_token_free(tok);
2179
15
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2180
63
  } else 
if (48
tok->type == ISL_TOKEN_INFTY48
)
{1
2181
1
    isl_qpolynomial *qp;
2182
1
    isl_token_free(tok);
2183
1
    qp = isl_qpolynomial_infty_on_domain(isl_map_get_space(map));
2184
1
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2185
48
  } else 
if (47
tok->type == ISL_TOKEN_NAN47
)
{0
2186
0
    isl_qpolynomial *qp;
2187
0
    isl_token_free(tok);
2188
0
    qp = isl_qpolynomial_nan_on_domain(isl_map_get_space(map));
2189
0
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2190
47
  } else 
if (47
tok->type == ISL_TOKEN_IDENT47
)
{16
2191
16
    int n = v->n;
2192
16
    int pos = vars_pos(v, tok->u.s, -1);
2193
16
    int pow;
2194
16
    isl_qpolynomial *qp;
2195
16
    if (
pos < 016
)
{0
2196
0
      isl_token_free(tok);
2197
0
      return NULL;
2198
16
    }
2199
16
    
if (16
pos >= n16
)
{0
2200
0
      vars_drop(v, v->n - n);
2201
0
      isl_stream_error(s, tok, "unknown identifier");
2202
0
      isl_token_free(tok);
2203
0
      return NULL;
2204
16
    }
2205
16
    isl_token_free(tok);
2206
16
    pow = optional_power(s);
2207
16
    qp = isl_qpolynomial_var_pow_on_domain(isl_map_get_space(map), pos, pow);
2208
16
    pwqp = isl_pw_qpolynomial_from_qpolynomial(qp);
2209
47
  } else 
if (31
is_start_of_div(tok)31
)
{29
2210
29
    isl_pw_aff *pwaff;
2211
29
    int pow;
2212
29
2213
29
    isl_stream_push_token(s, tok);
2214
29
    pwaff = accept_div(s, isl_map_get_space(map), v);
2215
29
    pow = optional_power(s);
2216
29
    pwqp = isl_pw_qpolynomial_from_pw_aff(pwaff);
2217
29
    pwqp = isl_pw_qpolynomial_pow(pwqp, pow);
2218
31
  } else 
if (2
tok->type == '-'2
)
{2
2219
2
    isl_token_free(tok);
2220
2
    pwqp = read_factor(s, map, v);
2221
2
    pwqp = isl_pw_qpolynomial_neg(pwqp);
2222
2
  } else {
2223
0
    isl_stream_error(s, tok, "unexpected isl_token");
2224
0
    isl_stream_push_token(s, tok);
2225
0
    return NULL;
2226
84
  }
2227
84
2228
84
  
if (84
isl_stream_eat_if_available(s, '*') ||84
2229
84
      
isl_stream_next_token_is(s, ISL_TOKEN_IDENT)69
)
{15
2230
15
    isl_pw_qpolynomial *pwqp2;
2231
15
2232
15
    pwqp2 = read_factor(s, map, v);
2233
15
    pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp2);
2234
84
  }
2235
84
2236
84
  return pwqp;
2237
84
error:
2238
0
  isl_pw_qpolynomial_free(pwqp);
2239
84
  return NULL;
2240
84
}
2241
2242
static __isl_give isl_pw_qpolynomial *read_term(__isl_keep isl_stream *s,
2243
  __isl_keep isl_map *map, struct vars *v)
2244
54
{
2245
54
  struct isl_token *tok;
2246
54
  isl_pw_qpolynomial *pwqp;
2247
54
2248
54
  pwqp = read_factor(s, map, v);
2249
54
2250
67
  for (;;) {
2251
67
    tok = next_token(s);
2252
67
    if (!tok)
2253
0
      return pwqp;
2254
67
2255
67
    
if (67
tok->type == '+'67
)
{12
2256
12
      isl_pw_qpolynomial *pwqp2;
2257
12
2258
12
      isl_token_free(tok);
2259
12
      pwqp2 = read_factor(s, map, v);
2260
12
      pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2261
67
    } else 
if (55
tok->type == '-'55
)
{1
2262
1
      isl_pw_qpolynomial *pwqp2;
2263
1
2264
1
      isl_token_free(tok);
2265
1
      pwqp2 = read_factor(s, map, v);
2266
1
      pwqp = isl_pw_qpolynomial_sub(pwqp, pwqp2);
2267
55
    } else 
if (54
tok->type == ISL_TOKEN_VALUE &&54
2268
54
          
isl_int_is_neg0
(tok->u.v))
{0
2269
0
      isl_pw_qpolynomial *pwqp2;
2270
0
2271
0
      isl_stream_push_token(s, tok);
2272
0
      pwqp2 = read_factor(s, map, v);
2273
0
      pwqp = isl_pw_qpolynomial_add(pwqp, pwqp2);
2274
54
    } else {
2275
54
      isl_stream_push_token(s, tok);
2276
54
      break;
2277
67
    }
2278
67
  }
2279
54
2280
54
  return pwqp;
2281
54
}
2282
2283
static __isl_give isl_map *read_optional_formula(__isl_keep isl_stream *s,
2284
  __isl_take isl_map *map, struct vars *v, int rational)
2285
2.70k
{
2286
2.70k
  struct isl_token *tok;
2287
2.70k
2288
2.70k
  tok = isl_stream_next_token(s);
2289
2.70k
  if (
!tok2.70k
)
{0
2290
0
    isl_stream_error(s, NULL, "unexpected EOF");
2291
0
    goto error;
2292
2.70k
  }
2293
2.70k
  
if (2.70k
tok->type == ':' ||2.70k
2294
2.70k
      
(tok->type == ISL_TOKEN_OR && 1.29k
!strcmp(tok->u.s, "|")0
))
{1.41k
2295
1.41k
    isl_token_free(tok);
2296
1.41k
    map = read_formula(s, v, map, rational);
2297
2.70k
  } else
2298
1.29k
    isl_stream_push_token(s, tok);
2299
2.70k
2300
2.70k
  return map;
2301
2.70k
error:
2302
0
  isl_map_free(map);
2303
2.70k
  return NULL;
2304
2.70k
}
2305
2306
static struct isl_obj obj_read_poly(__isl_keep isl_stream *s,
2307
  __isl_take isl_map *map, struct vars *v, int n)
2308
33
{
2309
33
  struct isl_obj obj = { isl_obj_pw_qpolynomial, NULL };
2310
33
  isl_pw_qpolynomial *pwqp;
2311
33
  struct isl_set *set;
2312
33
2313
33
  pwqp = read_term(s, map, v);
2314
33
  map = read_optional_formula(s, map, v, 0);
2315
33
  set = isl_map_range(map);
2316
33
2317
33
  pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, set);
2318
33
2319
33
  vars_drop(v, v->n - n);
2320
33
2321
33
  obj.v = pwqp;
2322
33
  return obj;
2323
33
}
2324
2325
static struct isl_obj obj_read_poly_or_fold(__isl_keep isl_stream *s,
2326
  __isl_take isl_set *set, struct vars *v, int n)
2327
33
{
2328
33
  struct isl_obj obj = { isl_obj_pw_qpolynomial_fold, NULL };
2329
33
  isl_pw_qpolynomial *pwqp;
2330
33
  isl_pw_qpolynomial_fold *pwf = NULL;
2331
33
2332
33
  if (!isl_stream_eat_if_available(s, ISL_TOKEN_MAX))
2333
33
    return obj_read_poly(s, set, v, n);
2334
33
2335
0
  
if (0
isl_stream_eat(s, '(')0
)
2336
0
    goto error;
2337
0
2338
0
  pwqp = read_term(s, set, v);
2339
0
  pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max, pwqp);
2340
0
2341
0
  while (
isl_stream_eat_if_available(s, ',')0
)
{0
2342
0
    isl_pw_qpolynomial_fold *pwf_i;
2343
0
    pwqp = read_term(s, set, v);
2344
0
    pwf_i = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
2345
0
                  pwqp);
2346
0
    pwf = isl_pw_qpolynomial_fold_fold(pwf, pwf_i);
2347
0
  }
2348
0
2349
0
  if (isl_stream_eat(s, ')'))
2350
0
    goto error;
2351
0
2352
0
  set = read_optional_formula(s, set, v, 0);
2353
0
  pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, set);
2354
0
2355
0
  vars_drop(v, v->n - n);
2356
0
2357
0
  obj.v = pwf;
2358
0
  return obj;
2359
0
error:
2360
0
  isl_set_free(set);
2361
0
  isl_pw_qpolynomial_fold_free(pwf);
2362
0
  obj.type = isl_obj_none;
2363
0
  return obj;
2364
33
}
2365
2366
static int is_rational(__isl_keep isl_stream *s)
2367
2.61k
{
2368
2.61k
  struct isl_token *tok;
2369
2.61k
2370
2.61k
  tok = isl_stream_next_token(s);
2371
2.61k
  if (!tok)
2372
0
    return 0;
2373
2.61k
  
if (2.61k
tok->type == ISL_TOKEN_RAT && 2.61k
isl_stream_next_token_is(s, ':')22
)
{22
2374
22
    isl_token_free(tok);
2375
22
    isl_stream_eat(s, ':');
2376
22
    return 1;
2377
2.61k
  }
2378
2.61k
2379
2.61k
  isl_stream_push_token(s, tok);
2380
2.58k
2381
2.61k
  return 0;
2382
2.61k
}
2383
2384
static struct isl_obj obj_read_body(__isl_keep isl_stream *s,
2385
  __isl_take isl_map *map, struct vars *v)
2386
2.61k
{
2387
2.61k
  struct isl_token *tok;
2388
2.61k
  struct isl_obj obj = { isl_obj_set, NULL };
2389
2.61k
  int n = v->n;
2390
2.61k
  int rational;
2391
2.61k
2392
2.61k
  rational = is_rational(s);
2393
2.61k
  if (rational)
2394
22
    map = isl_map_set_rational(map);
2395
2.61k
2396
2.61k
  if (
isl_stream_next_token_is(s, ':')2.61k
)
{126
2397
126
    obj.type = isl_obj_set;
2398
126
    obj.v = read_optional_formula(s, map, v, rational);
2399
126
    return obj;
2400
2.61k
  }
2401
2.61k
2402
2.48k
  
if (2.48k
!next_is_tuple(s)2.48k
)
2403
1
    return obj_read_poly_or_fold(s, map, v, n);
2404
2.48k
2405
2.48k
  map = read_map_tuple(s, map, isl_dim_in, v, rational, 0);
2406
2.48k
  if (!map)
2407
0
    goto error;
2408
2.48k
  tok = isl_stream_next_token(s);
2409
2.48k
  if (!tok)
2410
0
    goto error;
2411
2.48k
  
if (2.48k
tok->type == ISL_TOKEN_TO2.48k
)
{1.59k
2412
1.59k
    obj.type = isl_obj_map;
2413
1.59k
    isl_token_free(tok);
2414
1.59k
    if (
!next_is_tuple(s)1.59k
)
{32
2415
32
      isl_set *set = isl_map_domain(map);
2416
32
      return obj_read_poly_or_fold(s, set, v, n);
2417
1.59k
    }
2418
1.59k
    map = read_map_tuple(s, map, isl_dim_out, v, rational, 0);
2419
1.56k
    if (!map)
2420
1
      goto error;
2421
2.48k
  } else {
2422
889
    map = isl_map_domain(map);
2423
889
    isl_stream_push_token(s, tok);
2424
2.48k
  }
2425
2.48k
2426
2.48k
  map = read_optional_formula(s, map, v, rational);
2427
2.45k
2428
2.45k
  vars_drop(v, v->n - n);
2429
2.45k
2430
2.45k
  obj.v = map;
2431
2.48k
  return obj;
2432
2.48k
error:
2433
1
  isl_map_free(map);
2434
1
  obj.type = isl_obj_none;
2435
2.48k
  return obj;
2436
2.61k
}
2437
2438
static struct isl_obj to_union(isl_ctx *ctx, struct isl_obj obj)
2439
692
{
2440
692
  if (
obj.type == 692
isl_obj_map692
)
{552
2441
552
    obj.v = isl_union_map_from_map(obj.v);
2442
552
    obj.type = isl_obj_union_map;
2443
692
  } else 
if (140
obj.type == 140
isl_obj_set140
)
{138
2444
138
    obj.v = isl_union_set_from_set(obj.v);
2445
138
    obj.type = isl_obj_union_set;
2446
140
  } else 
if (2
obj.type == 2
isl_obj_pw_qpolynomial2
)
{2
2447
2
    obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
2448
2
    obj.type = isl_obj_union_pw_qpolynomial;
2449
2
  } else 
if (0
obj.type == 0
isl_obj_pw_qpolynomial_fold0
)
{0
2450
0
    obj.v = isl_union_pw_qpolynomial_fold_from_pw_qpolynomial_fold(obj.v);
2451
0
    obj.type = isl_obj_union_pw_qpolynomial_fold;
2452
0
  } else
2453
0
    isl_assert(ctx, 0, goto error);
2454
692
  return obj;
2455
692
error:
2456
0
  obj.type->free(obj.v);
2457
0
  obj.type = isl_obj_none;
2458
692
  return obj;
2459
692
}
2460
2461
static struct isl_obj obj_add(__isl_keep isl_stream *s,
2462
  struct isl_obj obj1, struct isl_obj obj2)
2463
625
{
2464
625
  if (
obj1.type == 625
isl_obj_set625
&&
obj2.type == 196
isl_obj_union_set196
)
2465
0
    obj1 = to_union(s->ctx, obj1);
2466
625
  if (
obj1.type == 625
isl_obj_union_set625
&&
obj2.type == 22
isl_obj_set22
)
2467
22
    obj2 = to_union(s->ctx, obj2);
2468
625
  if (
obj1.type == 625
isl_obj_map625
&&
obj2.type == 319
isl_obj_union_map319
)
2469
0
    obj1 = to_union(s->ctx, obj1);
2470
625
  if (
obj1.type == 625
isl_obj_union_map625
&&
obj2.type == 84
isl_obj_map84
)
2471
84
    obj2 = to_union(s->ctx, obj2);
2472
625
  if (
obj1.type == 625
isl_obj_pw_qpolynomial625
&&
2473
4
      
obj2.type == 4
isl_obj_union_pw_qpolynomial4
)
2474
0
    obj1 = to_union(s->ctx, obj1);
2475
625
  if (
obj1.type == 625
isl_obj_union_pw_qpolynomial625
&&
2476
0
      
obj2.type == 0
isl_obj_pw_qpolynomial0
)
2477
0
    obj2 = to_union(s->ctx, obj2);
2478
625
  if (
obj1.type == 625
isl_obj_pw_qpolynomial_fold625
&&
2479
0
      
obj2.type == 0
isl_obj_union_pw_qpolynomial_fold0
)
2480
0
    obj1 = to_union(s->ctx, obj1);
2481
625
  if (
obj1.type == 625
isl_obj_union_pw_qpolynomial_fold625
&&
2482
0
      
obj2.type == 0
isl_obj_pw_qpolynomial_fold0
)
2483
0
    obj2 = to_union(s->ctx, obj2);
2484
625
  if (
obj1.type != obj2.type625
)
{0
2485
0
    isl_stream_error(s, NULL,
2486
0
        "attempt to combine incompatible objects");
2487
0
    goto error;
2488
625
  }
2489
625
  
if (625
!obj1.type->add625
)
2490
0
    isl_die(s->ctx, isl_error_internal,
2491
625
      "combination not supported on object type", goto error);
2492
625
  
if (625
obj1.type == 625
isl_obj_map625
&&
!isl_map_has_equal_space(obj1.v, obj2.v)319
)
{226
2493
226
    obj1 = to_union(s->ctx, obj1);
2494
226
    obj2 = to_union(s->ctx, obj2);
2495
625
  }
2496
625
  if (
obj1.type == 625
isl_obj_set625
&&
!isl_set_has_equal_space(obj1.v, obj2.v)196
)
{58
2497
58
    obj1 = to_union(s->ctx, obj1);
2498
58
    obj2 = to_union(s->ctx, obj2);
2499
625
  }
2500
625
  if (
obj1.type == 625
isl_obj_pw_qpolynomial625
&&
2501
625
      
!isl_pw_qpolynomial_has_equal_space(obj1.v, obj2.v)4
)
{1
2502
1
    obj1 = to_union(s->ctx, obj1);
2503
1
    obj2 = to_union(s->ctx, obj2);
2504
625
  }
2505
625
  if (
obj1.type == 625
isl_obj_pw_qpolynomial_fold625
&&
2506
625
      
!isl_pw_qpolynomial_fold_has_equal_space(obj1.v, obj2.v)0
)
{0
2507
0
    obj1 = to_union(s->ctx, obj1);
2508
0
    obj2 = to_union(s->ctx, obj2);
2509
625
  }
2510
625
  obj1.v = obj1.type->add(obj1.v, obj2.v);
2511
625
  return obj1;
2512
625
error:
2513
0
  obj1.type->free(obj1.v);
2514
0
  obj2.type->free(obj2.v);
2515
0
  obj1.type = isl_obj_none;
2516
0
  obj1.v = NULL;
2517
625
  return obj1;
2518
625
}
2519
2520
/* Are the first two tokens on "s", "domain" (either as a string
2521
 * or as an identifier) followed by ":"?
2522
 */
2523
static int next_is_domain_colon(__isl_keep isl_stream *s)
2524
2.27k
{
2525
2.27k
  struct isl_token *tok;
2526
2.27k
  char *name;
2527
2.27k
  int res;
2528
2.27k
2529
2.27k
  tok = isl_stream_next_token(s);
2530
2.27k
  if (!tok)
2531
0
    return 0;
2532
2.27k
  
if (2.27k
tok->type != ISL_TOKEN_IDENT && 2.27k
tok->type != ISL_TOKEN_STRING1.56k
)
{1.56k
2533
1.56k
    isl_stream_push_token(s, tok);
2534
1.56k
    return 0;
2535
2.27k
  }
2536
2.27k
2537
2.27k
  name = isl_token_get_str(s->ctx, tok);
2538
0
  res = !strcmp(name, "domain") && isl_stream_next_token_is(s, ':');
2539
714
  free(name);
2540
714
2541
714
  isl_stream_push_token(s, tok);
2542
714
2543
2.27k
  return res;
2544
2.27k
}
2545
2546
/* Do the first tokens on "s" look like a schedule?
2547
 *
2548
 * The root of a schedule is always a domain node, so the first thing
2549
 * we expect in the stream is a domain key, i.e., "domain" followed
2550
 * by ":".  If the schedule was printed in YAML flow style, then
2551
 * we additionally expect a "{" to open the outer mapping.
2552
 */
2553
static int next_is_schedule(__isl_keep isl_stream *s)
2554
2.27k
{
2555
2.27k
  struct isl_token *tok;
2556
2.27k
  int is_schedule;
2557
2.27k
2558
2.27k
  tok = isl_stream_next_token(s);
2559
2.27k
  if (!tok)
2560
0
    return 0;
2561
2.27k
  
if (2.27k
tok->type != '{'2.27k
)
{639
2562
639
    isl_stream_push_token(s, tok);
2563
639
    return next_is_domain_colon(s);
2564
2.27k
  }
2565
2.27k
2566
2.27k
  is_schedule = next_is_domain_colon(s);
2567
1.63k
  isl_stream_push_token(s, tok);
2568
1.63k
2569
2.27k
  return is_schedule;
2570
2.27k
}
2571
2572
/* Read an isl_schedule from "s" and store it in an isl_obj.
2573
 */
2574
static struct isl_obj schedule_read(__isl_keep isl_stream *s)
2575
0
{
2576
0
  struct isl_obj obj;
2577
0
2578
0
  obj.type = isl_obj_schedule;
2579
0
  obj.v = isl_stream_read_schedule(s);
2580
0
2581
0
  return obj;
2582
0
}
2583
2584
static struct isl_obj obj_read(__isl_keep isl_stream *s)
2585
2.27k
{
2586
2.27k
  isl_map *map = NULL;
2587
2.27k
  struct isl_token *tok;
2588
2.27k
  struct vars *v = NULL;
2589
2.27k
  struct isl_obj obj = { isl_obj_set, NULL };
2590
2.27k
2591
2.27k
  if (next_is_schedule(s))
2592
0
    return schedule_read(s);
2593
2.27k
2594
2.27k
  tok = next_token(s);
2595
2.27k
  if (
!tok2.27k
)
{0
2596
0
    isl_stream_error(s, NULL, "unexpected EOF");
2597
0
    goto error;
2598
2.27k
  }
2599
2.27k
  
if (2.27k
tok->type == ISL_TOKEN_VALUE2.27k
)
{105
2600
105
    struct isl_token *tok2;
2601
105
    struct isl_map *map;
2602
105
2603
105
    tok2 = isl_stream_next_token(s);
2604
105
    if (
!tok2 || 105
tok2->type != ISL_TOKEN_VALUE105
||
2605
105
        
isl_int_is_neg105
(tok2->u.v))
{0
2606
0
      if (tok2)
2607
0
        isl_stream_push_token(s, tok2);
2608
0
      obj.type = isl_obj_val;
2609
0
      obj.v = isl_val_int_from_isl_int(s->ctx, tok->u.v);
2610
0
      isl_token_free(tok);
2611
0
      return obj;
2612
105
    }
2613
105
    isl_stream_push_token(s, tok2);
2614
105
    isl_stream_push_token(s, tok);
2615
105
    map = map_read_polylib(s);
2616
105
    if (!map)
2617
0
      goto error;
2618
105
    
if (105
isl_map_may_be_set(map)105
)
2619
105
      obj.v = isl_map_range(map);
2620
105
    else {
2621
0
      obj.type = isl_obj_map;
2622
0
      obj.v = map;
2623
105
    }
2624
105
    return obj;
2625
2.27k
  }
2626
2.27k
  v = vars_new(s->ctx);
2627
2.16k
  if (
!v2.16k
)
{0
2628
0
    isl_stream_push_token(s, tok);
2629
0
    goto error;
2630
2.16k
  }
2631
2.16k
  map = isl_map_universe(isl_space_params_alloc(s->ctx, 0));
2632
2.16k
  if (
tok->type == '['2.16k
)
{534
2633
534
    isl_stream_push_token(s, tok);
2634
534
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 0);
2635
534
    if (!map)
2636
1
      goto error;
2637
534
    tok = isl_stream_next_token(s);
2638
533
    if (
!tok || 533
tok->type != ISL_TOKEN_TO533
)
{0
2639
0
      isl_stream_error(s, tok, "expecting '->'");
2640
0
      if (tok)
2641
0
        isl_stream_push_token(s, tok);
2642
0
      goto error;
2643
533
    }
2644
533
    isl_token_free(tok);
2645
533
    tok = isl_stream_next_token(s);
2646
2.16k
  }
2647
2.16k
  
if (2.16k
!tok || 2.16k
tok->type != '{'2.16k
)
{0
2648
0
    isl_stream_error(s, tok, "expecting '{'");
2649
0
    if (tok)
2650
0
      isl_stream_push_token(s, tok);
2651
0
    goto error;
2652
2.16k
  }
2653
2.16k
  isl_token_free(tok);
2654
2.16k
2655
2.16k
  tok = isl_stream_next_token(s);
2656
2.16k
  if (!tok)
2657
0
    ;
2658
2.16k
  else 
if (2.16k
tok->type == ISL_TOKEN_IDENT && 2.16k
!strcmp(tok->u.s, "Sym")1.03k
)
{1
2659
1
    isl_token_free(tok);
2660
1
    if (isl_stream_eat(s, '='))
2661
0
      goto error;
2662
1
    map = read_map_tuple(s, map, isl_dim_param, v, 0, 1);
2663
1
    if (!map)
2664
0
      goto error;
2665
2.16k
  } else 
if (2.16k
tok->type == '}'2.16k
)
{183
2666
183
    obj.type = isl_obj_union_set;
2667
183
    obj.v = isl_union_set_empty(isl_map_get_space(map));
2668
183
    isl_token_free(tok);
2669
183
    goto done;
2670
2.16k
  } else
2671
1.98k
    isl_stream_push_token(s, tok);
2672
2.16k
2673
1.98k
  
for (;;) 1.98k
{2.61k
2674
2.61k
    struct isl_obj o;
2675
2.61k
    tok = NULL;
2676
2.61k
    o = obj_read_body(s, isl_map_copy(map), v);
2677
2.61k
    if (
o.type == 2.61k
isl_obj_none2.61k
||
!o.v2.60k
)
2678
2
      goto error;
2679
2.60k
    
if (2.60k
!obj.v2.60k
)
2680
1.98k
      obj = o;
2681
2.60k
    else {
2682
625
      obj = obj_add(s, obj, o);
2683
625
      if (
obj.type == 625
isl_obj_none625
||
!obj.v625
)
2684
0
        goto error;
2685
2.60k
    }
2686
2.60k
    tok = isl_stream_next_token(s);
2687
2.60k
    if (
!tok || 2.60k
tok->type != ';'2.60k
)
2688
1.98k
      break;
2689
2.60k
    isl_token_free(tok);
2690
625
    if (
isl_stream_next_token_is(s, '}')625
)
{0
2691
0
      tok = isl_stream_next_token(s);
2692
0
      break;
2693
625
    }
2694
1.98k
  }
2695
1.98k
2696
1.98k
  
if (1.98k
tok && 1.98k
tok->type == '}'1.98k
)
{1.98k
2697
1.98k
    isl_token_free(tok);
2698
1.98k
  } else {
2699
0
    isl_stream_error(s, tok, "unexpected isl_token");
2700
0
    if (tok)
2701
0
      isl_token_free(tok);
2702
0
    goto error;
2703
1.98k
  }
2704
2.16k
done:
2705
2.16k
  vars_free(v);
2706
2.16k
  isl_map_free(map);
2707
2.16k
2708
2.16k
  return obj;
2709
1.98k
error:
2710
3
  isl_map_free(map);
2711
3
  obj.type->free(obj.v);
2712
3
  if (v)
2713
3
    vars_free(v);
2714
3
  obj.v = NULL;
2715
1.98k
  return obj;
2716
2.27k
}
2717
2718
struct isl_obj isl_stream_read_obj(__isl_keep isl_stream *s)
2719
16
{
2720
16
  return obj_read(s);
2721
16
}
2722
2723
__isl_give isl_map *isl_stream_read_map(__isl_keep isl_stream *s)
2724
591
{
2725
591
  struct isl_obj obj;
2726
591
2727
591
  obj = obj_read(s);
2728
591
  if (obj.v)
2729
591
    isl_assert(s->ctx, obj.type == isl_obj_map ||
2730
591
           obj.type == isl_obj_set, goto error);
2731
591
  
2732
591
  
if (591
obj.type == 591
isl_obj_set591
)
2733
32
    obj.v = isl_map_from_range(obj.v);
2734
591
2735
591
  return obj.v;
2736
591
error:
2737
0
  obj.type->free(obj.v);
2738
591
  return NULL;
2739
591
}
2740
2741
__isl_give isl_set *isl_stream_read_set(__isl_keep isl_stream *s)
2742
464
{
2743
464
  struct isl_obj obj;
2744
464
2745
464
  obj = obj_read(s);
2746
464
  if (
obj.v464
)
{463
2747
463
    if (
obj.type == 463
isl_obj_map463
&&
isl_map_may_be_set(obj.v)0
)
{0
2748
0
      obj.v = isl_map_range(obj.v);
2749
0
      obj.type = isl_obj_set;
2750
463
    }
2751
463
    isl_assert(s->ctx, obj.type == isl_obj_set, goto error);
2752
464
  }
2753
464
2754
464
  return obj.v;
2755
464
error:
2756
0
  obj.type->free(obj.v);
2757
464
  return NULL;
2758
464
}
2759
2760
__isl_give isl_union_map *isl_stream_read_union_map(__isl_keep isl_stream *s)
2761
671
{
2762
671
  struct isl_obj obj;
2763
671
2764
671
  obj = obj_read(s);
2765
671
  if (
obj.type == 671
isl_obj_map671
)
{343
2766
343
    obj.type = isl_obj_union_map;
2767
343
    obj.v = isl_union_map_from_map(obj.v);
2768
671
  }
2769
671
  if (
obj.type == 671
isl_obj_set671
)
{0
2770
0
    obj.type = isl_obj_union_set;
2771
0
    obj.v = isl_union_set_from_set(obj.v);
2772
671
  }
2773
671
  if (
obj.v && 671
obj.type == 671
isl_obj_union_set671
&&
2774
108
      isl_union_set_is_empty(obj.v))
2775
108
    
obj.type = 108
isl_obj_union_map108
;
2776
671
  if (
obj.v && 671
obj.type != 671
isl_obj_union_map671
)
2777
0
    isl_die(s->ctx, isl_error_invalid, "invalid input", goto error);
2778
671
2779
671
  return obj.v;
2780
671
error:
2781
0
  obj.type->free(obj.v);
2782
671
  return NULL;
2783
671
}
2784
2785
__isl_give isl_union_set *isl_stream_read_union_set(__isl_keep isl_stream *s)
2786
303
{
2787
303
  struct isl_obj obj;
2788
303
2789
303
  obj = obj_read(s);
2790
303
  if (
obj.type == 303
isl_obj_set303
)
{171
2791
171
    obj.type = isl_obj_union_set;
2792
171
    obj.v = isl_union_set_from_set(obj.v);
2793
303
  }
2794
303
  if (obj.v)
2795
303
    isl_assert(s->ctx, obj.type == isl_obj_union_set, goto error);
2796
303
2797
303
  return obj.v;
2798
303
error:
2799
0
  obj.type->free(obj.v);
2800
303
  return NULL;
2801
303
}
2802
2803
static __isl_give isl_basic_map *basic_map_read(__isl_keep isl_stream *s)
2804
177
{
2805
177
  struct isl_obj obj;
2806
177
  struct isl_map *map;
2807
177
  struct isl_basic_map *bmap;
2808
177
2809
177
  obj = obj_read(s);
2810
177
  if (
obj.v && 177
(obj.type != 177
isl_obj_map177
&&
obj.type != 167
isl_obj_set167
))
2811
0
    isl_die(s->ctx, isl_error_invalid, "not a (basic) set or map",
2812
177
      goto error);
2813
177
  map = obj.v;
2814
177
  if (!map)
2815
0
    return NULL;
2816
177
2817
177
  
if (177
map->n > 1177
)
2818
0
    isl_die(s->ctx, isl_error_invalid,
2819
177
      "set or map description involves "
2820
177
      "more than one disjunct", goto error);
2821
177
2822
177
  
if (177
map->n == 0177
)
2823
4
    bmap = isl_basic_map_empty(isl_map_get_space(map));
2824
177
  else
2825
173
    bmap = isl_basic_map_copy(map->p[0]);
2826
177
2827
177
  isl_map_free(map);
2828
177
2829
177
  return bmap;
2830
177
error:
2831
0
  obj.type->free(obj.v);
2832
177
  return NULL;
2833
177
}
2834
2835
static __isl_give isl_basic_set *basic_set_read(__isl_keep isl_stream *s)
2836
167
{
2837
167
  isl_basic_map *bmap;
2838
167
  bmap = basic_map_read(s);
2839
167
  if (!bmap)
2840
0
    return NULL;
2841
167
  
if (167
!isl_basic_map_may_be_set(bmap)167
)
2842
0
    isl_die(s->ctx, isl_error_invalid,
2843
167
      "input is not a set", goto error);
2844
167
  return isl_basic_map_range(bmap);
2845
167
error:
2846
0
  isl_basic_map_free(bmap);
2847
167
  return NULL;
2848
167
}
2849
2850
__isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
2851
  FILE *input)
2852
2
{
2853
2
  struct isl_basic_map *bmap;
2854
2
  isl_stream *s = isl_stream_new_file(ctx, input);
2855
2
  if (!s)
2856
0
    return NULL;
2857
2
  bmap = basic_map_read(s);
2858
2
  isl_stream_free(s);
2859
2
  return bmap;
2860
2
}
2861
2862
__isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
2863
  FILE *input)
2864
110
{
2865
110
  isl_basic_set *bset;
2866
110
  isl_stream *s = isl_stream_new_file(ctx, input);
2867
110
  if (!s)
2868
0
    return NULL;
2869
110
  bset = basic_set_read(s);
2870
110
  isl_stream_free(s);
2871
110
  return bset;
2872
110
}
2873
2874
struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
2875
  const char *str)
2876
8
{
2877
8
  struct isl_basic_map *bmap;
2878
8
  isl_stream *s = isl_stream_new_str(ctx, str);
2879
8
  if (!s)
2880
0
    return NULL;
2881
8
  bmap = basic_map_read(s);
2882
8
  isl_stream_free(s);
2883
8
  return bmap;
2884
8
}
2885
2886
struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
2887
  const char *str)
2888
57
{
2889
57
  isl_basic_set *bset;
2890
57
  isl_stream *s = isl_stream_new_str(ctx, str);
2891
57
  if (!s)
2892
0
    return NULL;
2893
57
  bset = basic_set_read(s);
2894
57
  isl_stream_free(s);
2895
57
  return bset;
2896
57
}
2897
2898
__isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
2899
  FILE *input)
2900
0
{
2901
0
  struct isl_map *map;
2902
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2903
0
  if (!s)
2904
0
    return NULL;
2905
0
  map = isl_stream_read_map(s);
2906
0
  isl_stream_free(s);
2907
0
  return map;
2908
0
}
2909
2910
__isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
2911
  const char *str)
2912
591
{
2913
591
  struct isl_map *map;
2914
591
  isl_stream *s = isl_stream_new_str(ctx, str);
2915
591
  if (!s)
2916
0
    return NULL;
2917
591
  map = isl_stream_read_map(s);
2918
591
  isl_stream_free(s);
2919
591
  return map;
2920
591
}
2921
2922
__isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
2923
  FILE *input)
2924
0
{
2925
0
  isl_set *set;
2926
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2927
0
  if (!s)
2928
0
    return NULL;
2929
0
  set = isl_stream_read_set(s);
2930
0
  isl_stream_free(s);
2931
0
  return set;
2932
0
}
2933
2934
struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
2935
  const char *str)
2936
464
{
2937
464
  isl_set *set;
2938
464
  isl_stream *s = isl_stream_new_str(ctx, str);
2939
464
  if (!s)
2940
0
    return NULL;
2941
464
  set = isl_stream_read_set(s);
2942
464
  isl_stream_free(s);
2943
464
  return set;
2944
464
}
2945
2946
__isl_give isl_union_map *isl_union_map_read_from_file(isl_ctx *ctx,
2947
  FILE *input)
2948
0
{
2949
0
  isl_union_map *umap;
2950
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2951
0
  if (!s)
2952
0
    return NULL;
2953
0
  umap = isl_stream_read_union_map(s);
2954
0
  isl_stream_free(s);
2955
0
  return umap;
2956
0
}
2957
2958
__isl_give isl_union_map *isl_union_map_read_from_str(struct isl_ctx *ctx,
2959
    const char *str)
2960
671
{
2961
671
  isl_union_map *umap;
2962
671
  isl_stream *s = isl_stream_new_str(ctx, str);
2963
671
  if (!s)
2964
0
    return NULL;
2965
671
  umap = isl_stream_read_union_map(s);
2966
671
  isl_stream_free(s);
2967
671
  return umap;
2968
671
}
2969
2970
__isl_give isl_union_set *isl_union_set_read_from_file(isl_ctx *ctx,
2971
  FILE *input)
2972
0
{
2973
0
  isl_union_set *uset;
2974
0
  isl_stream *s = isl_stream_new_file(ctx, input);
2975
0
  if (!s)
2976
0
    return NULL;
2977
0
  uset = isl_stream_read_union_set(s);
2978
0
  isl_stream_free(s);
2979
0
  return uset;
2980
0
}
2981
2982
__isl_give isl_union_set *isl_union_set_read_from_str(struct isl_ctx *ctx,
2983
    const char *str)
2984
303
{
2985
303
  isl_union_set *uset;
2986
303
  isl_stream *s = isl_stream_new_str(ctx, str);
2987
303
  if (!s)
2988
0
    return NULL;
2989
303
  uset = isl_stream_read_union_set(s);
2990
303
  isl_stream_free(s);
2991
303
  return uset;
2992
303
}
2993
2994
static __isl_give isl_vec *isl_vec_read_polylib(__isl_keep isl_stream *s)
2995
0
{
2996
0
  struct isl_vec *vec = NULL;
2997
0
  struct isl_token *tok;
2998
0
  unsigned size;
2999
0
  int j;
3000
0
3001
0
  tok = isl_stream_next_token(s);
3002
0
  if (
!tok || 0
tok->type != ISL_TOKEN_VALUE0
)
{0
3003
0
    isl_stream_error(s, tok, "expecting vector length");
3004
0
    goto error;
3005
0
  }
3006
0
3007
0
  
size = 0
isl_int_get_si0
(tok->u.v);
3008
0
  isl_token_free(tok);
3009
0
3010
0
  vec = isl_vec_alloc(s->ctx, size);
3011
0
3012
0
  for (j = 0; 
j < size0
;
++j0
)
{0
3013
0
    tok = isl_stream_next_token(s);
3014
0
    if (
!tok || 0
tok->type != ISL_TOKEN_VALUE0
)
{0
3015
0
      isl_stream_error(s, tok, "expecting constant value");
3016
0
      goto error;
3017
0
    }
3018
0
    
isl_int_set0
(vec->el[j], tok->u.v);0
3019
0
    isl_token_free(tok);
3020
0
  }
3021
0
3022
0
  return vec;
3023
0
error:
3024
0
  isl_token_free(tok);
3025
0
  isl_vec_free(vec);
3026
0
  return NULL;
3027
0
}
3028
3029
static __isl_give isl_vec *vec_read(__isl_keep isl_stream *s)
3030
0
{
3031
0
  return isl_vec_read_polylib(s);
3032
0
}
3033
3034
__isl_give isl_vec *isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
3035
0
{
3036
0
  isl_vec *v;
3037
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3038
0
  if (!s)
3039
0
    return NULL;
3040
0
  v = vec_read(s);
3041
0
  isl_stream_free(s);
3042
0
  return v;
3043
0
}
3044
3045
__isl_give isl_pw_qpolynomial *isl_stream_read_pw_qpolynomial(
3046
  __isl_keep isl_stream *s)
3047
27
{
3048
27
  struct isl_obj obj;
3049
27
3050
27
  obj = obj_read(s);
3051
27
  if (obj.v)
3052
27
    isl_assert(s->ctx, obj.type == isl_obj_pw_qpolynomial,
3053
27
         goto error);
3054
27
3055
27
  return obj.v;
3056
27
error:
3057
0
  obj.type->free(obj.v);
3058
27
  return NULL;
3059
27
}
3060
3061
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_str(isl_ctx *ctx,
3062
    const char *str)
3063
27
{
3064
27
  isl_pw_qpolynomial *pwqp;
3065
27
  isl_stream *s = isl_stream_new_str(ctx, str);
3066
27
  if (!s)
3067
0
    return NULL;
3068
27
  pwqp = isl_stream_read_pw_qpolynomial(s);
3069
27
  isl_stream_free(s);
3070
27
  return pwqp;
3071
27
}
3072
3073
__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_read_from_file(isl_ctx *ctx,
3074
    FILE *input)
3075
0
{
3076
0
  isl_pw_qpolynomial *pwqp;
3077
0
  isl_stream *s = isl_stream_new_file(ctx, input);
3078
0
  if (!s)
3079
0
    return NULL;
3080
0
  pwqp = isl_stream_read_pw_qpolynomial(s);
3081
0
  isl_stream_free(s);
3082
0
  return pwqp;
3083
0
}
3084
3085
/* Is the next token an identifer not in "v"?
3086
 */
3087
static int next_is_fresh_ident(__isl_keep isl_stream *s, struct vars *v)
3088
26
{
3089
26
  int n = v->n;
3090
26
  int fresh;
3091
26
  struct isl_token *tok;
3092
26
3093
26
  tok = isl_stream_next_token(s);
3094
26
  if (!tok)
3095
0
    return 0;
3096
26
  
fresh = tok->type == ISL_TOKEN_IDENT && 26
vars_pos(v, tok->u.s, -1) >= n23
;
3097
26
  isl_stream_push_token(s, tok);
3098
26
3099
26
  vars_drop(v, v->n - n);
3100
26
3101
26
  return fresh;
3102
26
}
3103
3104
/* First read the domain of the affine expression, which may be
3105
 * a parameter space or a set.
3106
 * The tricky part is that we don't know if the domain is a set or not,
3107
 * so when we are trying to read the domain, we may actually be reading
3108
 * the affine expression itself (defined on a parameter domains)
3109
 * If the tuple we are reading is named, we assume it's the domain.
3110
 * Also, if inside the tuple, the first thing we find is a nested tuple
3111
 * or a new identifier, we again assume it's the domain.
3112
 * Finally, if the tuple is empty, then it must be the domain
3113
 * since it does not contain an affine expression.
3114
 * Otherwise, we assume we are reading an affine expression.
3115
 */
3116
static __isl_give isl_set *read_aff_domain(__isl_keep isl_stream *s,
3117
  __isl_take isl_set *dom, struct vars *v)
3118
58
{
3119
58
  struct isl_token *tok, *tok2;
3120
58
  int is_empty;
3121
58
3122
58
  tok = isl_stream_next_token(s);
3123
58
  if (
tok && 58
(tok->type == ISL_TOKEN_IDENT || 58
tok->is_keyword27
))
{31
3124
31
    isl_stream_push_token(s, tok);
3125
31
    return read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
3126
58
  }
3127
27
  
if (27
!tok || 27
tok->type != '['27
)
{0
3128
0
    isl_stream_error(s, tok, "expecting '['");
3129
0
    goto error;
3130
27
  }
3131
27
  tok2 = isl_stream_next_token(s);
3132
27
  is_empty = tok2 && tok2->type == ']';
3133
27
  if (tok2)
3134
27
    isl_stream_push_token(s, tok2);
3135
27
  if (
is_empty || 27
next_is_tuple(s)26
||
next_is_fresh_ident(s, v)26
)
{23
3136
23
    isl_stream_push_token(s, tok);
3137
23
    dom = read_map_tuple(s, dom, isl_dim_set, v, 1, 0);
3138
27
  } else
3139
4
    isl_stream_push_token(s, tok);
3140
27
3141
27
  return dom;
3142
27
error:
3143
0
  if (tok)
3144
0
    isl_stream_push_token(s, tok);
3145
0
  isl_set_free(dom);
3146
27
  return NULL;
3147
58
}
3148
3149
/* Read an affine expression from "s".
3150
 */
3151
__isl_give isl_aff *isl_stream_read_aff(__isl_keep isl_stream *s)
3152
62
{
3153
62
  isl_aff *aff;
3154
62
  isl_multi_aff *ma;
3155
62
3156
62
  ma = isl_stream_read_multi_aff(s);
3157
62
  if (!ma)
3158
0
    return NULL;
3159
62
  
if (62
isl_multi_aff_dim(ma, isl_dim_out) != 162
)
3160
0
    isl_die(s->ctx, isl_error_invalid,
3161
62
      "expecting single affine expression",
3162
62
      goto error);
3163
62
3164
62
  aff = isl_multi_aff_get_aff(ma, 0);
3165
62
  isl_multi_aff_free(ma);
3166
62
  return aff;
3167
62
error:
3168
0
  isl_multi_aff_free(ma);
3169
62
  return NULL;
3170
62
}
3171
3172
/* Read a piecewise affine expression from "s" with domain (space) "dom".
3173
 */
3174
static __isl_give isl_pw_aff *read_pw_aff_with_dom(__isl_keep isl_stream *s,
3175
  __isl_take isl_set *dom, struct vars *v)
3176
58
{
3177
58
  isl_pw_aff *pwaff = NULL;
3178
58
3179
58
  if (
!isl_set_is_params(dom) && 58
isl_stream_eat(s, ISL_TOKEN_TO)54
)
3180
0
    goto error;
3181
58
3182
58
  
if (58
isl_stream_eat(s, '[')58
)
3183
0
    goto error;
3184
58
3185
58
  pwaff = accept_affine(s, isl_set_get_space(dom), v);
3186
58
3187
58
  if (isl_stream_eat(s, ']'))
3188
0
    goto error;
3189
58
3190
58
  dom = read_optional_formula(s, dom, v, 0);
3191
58
  pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
3192
58
3193
58
  return pwaff;
3194
58
error:
3195
0
  isl_set_free(dom);
3196
0
  isl_pw_aff_free(pwaff);
3197
58
  return NULL;
3198
58
}
3199
3200
__isl_give isl_pw_aff *isl_stream_read_pw_aff(__isl_keep isl_stream *s)
3201
22
{
3202
22
  struct vars *v;
3203
22
  isl_set *dom = NULL;
3204
22
  isl_set *aff_dom;
3205
22
  isl_pw_aff *pa = NULL;
3206
22
  int n;
3207
22
3208
22
  v = vars_new(s->ctx);
3209
22
  if (!v)
3210
0
    return NULL;
3211
22
3212
22
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3213
22
  if (
next_is_tuple(s)22
)
{2
3214
2
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3215
2
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3216
0
      goto error;
3217
22
  }
3218
22
  
if (22
isl_stream_eat(s, '{')22
)
3219
0
    goto error;
3220
22
3221
22
  n = v->n;
3222
22
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3223
22
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3224
22
  vars_drop(v, v->n - n);
3225
22
3226
27
  while (
isl_stream_eat_if_available(s, ';')27
)
{5
3227
5
    isl_pw_aff *pa_i;
3228
5
3229
5
    n = v->n;
3230
5
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3231
5
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3232
5
    vars_drop(v, v->n - n);
3233
5
3234
5
    pa = isl_pw_aff_union_add(pa, pa_i);
3235
22
  }
3236
22
3237
22
  if (isl_stream_eat(s, '}'))
3238
0
    goto error;
3239
22
3240
22
  vars_free(v);
3241
22
  isl_set_free(dom);
3242
22
  return pa;
3243
22
error:
3244
0
  vars_free(v);
3245
0
  isl_set_free(dom);
3246
0
  isl_pw_aff_free(pa);
3247
22
  return NULL;
3248
22
}
3249
3250
__isl_give isl_aff *isl_aff_read_from_str(isl_ctx *ctx, const char *str)
3251
62
{
3252
62
  isl_aff *aff;
3253
62
  isl_stream *s = isl_stream_new_str(ctx, str);
3254
62
  if (!s)
3255
0
    return NULL;
3256
62
  aff = isl_stream_read_aff(s);
3257
62
  isl_stream_free(s);
3258
62
  return aff;
3259
62
}
3260
3261
__isl_give isl_pw_aff *isl_pw_aff_read_from_str(isl_ctx *ctx, const char *str)
3262
22
{
3263
22
  isl_pw_aff *pa;
3264
22
  isl_stream *s = isl_stream_new_str(ctx, str);
3265
22
  if (!s)
3266
0
    return NULL;
3267
22
  pa = isl_stream_read_pw_aff(s);
3268
22
  isl_stream_free(s);
3269
22
  return pa;
3270
22
}
3271
3272
/* Extract an isl_multi_pw_aff with domain space "dom_space"
3273
 * from a tuple "tuple" read by read_tuple.
3274
 *
3275
 * Note that the function read_tuple accepts tuples where some output or
3276
 * set dimensions are defined in terms of other output or set dimensions
3277
 * since this function is also used to read maps.  As a special case,
3278
 * read_tuple also accept dimensions that are defined in terms of themselves
3279
 * (i.e., that are not defined).
3280
 * These cases are not allowed when extracting an isl_multi_pw_aff so check
3281
 * that the definitions of the output/set dimensions do not involve any
3282
 * output/set dimensions.
3283
 * Finally, drop the output dimensions from the domain of the result
3284
 * of read_tuple (which is of the form [input, output] -> [output],
3285
 * with anonymous domain) and reset the space.
3286
 */
3287
static __isl_give isl_multi_pw_aff *extract_mpa_from_tuple(
3288
  __isl_take isl_space *dom_space, __isl_keep isl_multi_pw_aff *tuple)
3289
44
{
3290
44
  int dim, i, n;
3291
44
  isl_space *space;
3292
44
  isl_multi_pw_aff *mpa;
3293
44
3294
44
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3295
44
  dim = isl_space_dim(dom_space, isl_dim_all);
3296
44
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3297
44
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3298
44
  if (!isl_space_is_params(dom_space))
3299
44
    space = isl_space_map_from_domain_and_range(
3300
44
        isl_space_copy(dom_space), space);
3301
44
  isl_space_free(dom_space);
3302
44
  mpa = isl_multi_pw_aff_alloc(space);
3303
44
3304
135
  for (i = 0; 
i < n135
;
++i91
)
{91
3305
91
    isl_pw_aff *pa;
3306
91
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3307
91
    if (!pa)
3308
0
      return isl_multi_pw_aff_free(mpa);
3309
91
    
if (91
isl_pw_aff_involves_dims(pa, isl_dim_in, dim, i + 1)91
)
{0
3310
0
      isl_ctx *ctx = isl_pw_aff_get_ctx(pa);
3311
0
      isl_pw_aff_free(pa);
3312
0
      isl_die(ctx, isl_error_invalid,
3313
0
        "not an affine expression",
3314
0
        return isl_multi_pw_aff_free(mpa));
3315
91
    }
3316
91
    pa = isl_pw_aff_drop_dims(pa, isl_dim_in, dim, n);
3317
91
    space = isl_multi_pw_aff_get_domain_space(mpa);
3318
91
    pa = isl_pw_aff_reset_domain_space(pa, space);
3319
91
    mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
3320
91
  }
3321
44
3322
44
  return mpa;
3323
44
}
3324
3325
/* Read a tuple of affine expressions, together with optional constraints
3326
 * on the domain from "s".  "dom" represents the initial constraints
3327
 * on the domain.
3328
 *
3329
 * The isl_multi_aff may live in either a set or a map space.
3330
 * First read the first tuple and check if it is followed by a "->".
3331
 * If so, convert the tuple into the domain of the isl_multi_pw_aff and
3332
 * read in the next tuple.  This tuple (or the first tuple if it was
3333
 * not followed by a "->") is then converted into an isl_multi_pw_aff
3334
 * through a call to extract_mpa_from_tuple.
3335
 * The result is converted to an isl_pw_multi_aff and
3336
 * its domain is intersected with the domain.
3337
 */
3338
static __isl_give isl_pw_multi_aff *read_conditional_multi_aff(
3339
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3340
41
{
3341
41
  isl_multi_pw_aff *tuple;
3342
41
  isl_multi_pw_aff *mpa;
3343
41
  isl_pw_multi_aff *pma;
3344
41
  int n = v->n;
3345
41
3346
41
  tuple = read_tuple(s, v, 0, 0);
3347
41
  if (!tuple)
3348
0
    goto error;
3349
41
  
if (41
isl_stream_eat_if_available(s, ISL_TOKEN_TO)41
)
{7
3350
7
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3351
7
    dom = isl_map_domain(map);
3352
7
    tuple = read_tuple(s, v, 0, 0);
3353
7
    if (!tuple)
3354
0
      goto error;
3355
41
  }
3356
41
3357
41
  dom = read_optional_formula(s, dom, v, 0);
3358
41
3359
41
  vars_drop(v, v->n - n);
3360
41
3361
41
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3362
41
  isl_multi_pw_aff_free(tuple);
3363
41
  pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
3364
41
  pma = isl_pw_multi_aff_intersect_domain(pma, dom);
3365
41
3366
41
  return pma;
3367
41
error:
3368
0
  isl_set_free(dom);
3369
41
  return NULL;
3370
41
}
3371
3372
/* Read an isl_pw_multi_aff from "s".
3373
 *
3374
 * In particular, first read the parameters and then read a sequence
3375
 * of one or more tuples of affine expressions with optional conditions and
3376
 * add them up.
3377
 */
3378
__isl_give isl_pw_multi_aff *isl_stream_read_pw_multi_aff(
3379
  __isl_keep isl_stream *s)
3380
38
{
3381
38
  struct vars *v;
3382
38
  isl_set *dom;
3383
38
  isl_pw_multi_aff *pma = NULL;
3384
38
3385
38
  v = vars_new(s->ctx);
3386
38
  if (!v)
3387
0
    return NULL;
3388
38
3389
38
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3390
38
  if (
next_is_tuple(s)38
)
{27
3391
27
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3392
27
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3393
0
      goto error;
3394
38
  }
3395
38
  
if (38
isl_stream_eat(s, '{')38
)
3396
0
    goto error;
3397
38
3398
38
  pma = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3399
38
3400
41
  while (
isl_stream_eat_if_available(s, ';')41
)
{3
3401
3
    isl_pw_multi_aff *pma2;
3402
3
3403
3
    pma2 = read_conditional_multi_aff(s, isl_set_copy(dom), v);
3404
3
    pma = isl_pw_multi_aff_union_add(pma, pma2);
3405
3
    if (!pma)
3406
0
      goto error;
3407
38
  }
3408
38
3409
38
  
if (38
isl_stream_eat(s, '}')38
)
3410
0
    goto error;
3411
38
3412
38
  isl_set_free(dom);
3413
38
  vars_free(v);
3414
38
  return pma;
3415
38
error:
3416
0
  isl_pw_multi_aff_free(pma);
3417
0
  isl_set_free(dom);
3418
0
  vars_free(v);
3419
38
  return NULL;
3420
38
}
3421
3422
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_read_from_str(isl_ctx *ctx,
3423
  const char *str)
3424
38
{
3425
38
  isl_pw_multi_aff *pma;
3426
38
  isl_stream *s = isl_stream_new_str(ctx, str);
3427
38
  if (!s)
3428
0
    return NULL;
3429
38
  pma = isl_stream_read_pw_multi_aff(s);
3430
38
  isl_stream_free(s);
3431
38
  return pma;
3432
38
}
3433
3434
/* Read an isl_union_pw_multi_aff from "s".
3435
 * We currently read a generic object and if it turns out to be a set or
3436
 * a map, we convert that to an isl_union_pw_multi_aff.
3437
 * It would be more efficient if we were to construct
3438
 * the isl_union_pw_multi_aff directly.
3439
 */
3440
__isl_give isl_union_pw_multi_aff *isl_stream_read_union_pw_multi_aff(
3441
  __isl_keep isl_stream *s)
3442
23
{
3443
23
  struct isl_obj obj;
3444
23
3445
23
  obj = obj_read(s);
3446
23
  if (!obj.v)
3447
0
    return NULL;
3448
23
3449
23
  
if (23
obj.type == 23
isl_obj_map23
||
obj.type == 7
isl_obj_set7
)
3450
16
    obj = to_union(s->ctx, obj);
3451
23
  if (
obj.type == 23
isl_obj_union_map23
)
3452
22
    return isl_union_pw_multi_aff_from_union_map(obj.v);
3453
1
  
if (1
obj.type == 1
isl_obj_union_set1
)
3454
1
    return isl_union_pw_multi_aff_from_union_set(obj.v);
3455
1
3456
1
  obj.type->free(obj.v);
3457
0
  isl_die(s->ctx, isl_error_invalid, "unexpected object type",
3458
0
    return NULL);
3459
23
}
3460
3461
/* Read an isl_union_pw_multi_aff from "str".
3462
 */
3463
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_read_from_str(
3464
  isl_ctx *ctx, const char *str)
3465
23
{
3466
23
  isl_union_pw_multi_aff *upma;
3467
23
  isl_stream *s = isl_stream_new_str(ctx, str);
3468
23
  if (!s)
3469
0
    return NULL;
3470
23
  upma = isl_stream_read_union_pw_multi_aff(s);
3471
23
  isl_stream_free(s);
3472
23
  return upma;
3473
23
}
3474
3475
/* Assuming "pa" represents a single affine expression defined on a universe
3476
 * domain, extract this affine expression.
3477
 */
3478
static __isl_give isl_aff *aff_from_pw_aff(__isl_take isl_pw_aff *pa)
3479
126
{
3480
126
  isl_aff *aff;
3481
126
3482
126
  if (!pa)
3483
0
    return NULL;
3484
126
  
if (126
pa->n != 1126
)
3485
0
    isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3486
126
      "expecting single affine expression",
3487
126
      goto error);
3488
126
  
if (126
!isl_set_plain_is_universe(pa->p[0].set)126
)
3489
0
    isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
3490
126
      "expecting universe domain",
3491
126
      goto error);
3492
126
3493
126
  aff = isl_aff_copy(pa->p[0].aff);
3494
126
  isl_pw_aff_free(pa);
3495
126
  return aff;
3496
126
error:
3497
0
  isl_pw_aff_free(pa);
3498
126
  return NULL;
3499
126
}
3500
3501
/* This function is called for each element in a tuple inside
3502
 * isl_stream_read_multi_val.
3503
 * Read an isl_val from "s" and add it to *list.
3504
 */
3505
static __isl_give isl_space *read_val_el(__isl_keep isl_stream *s,
3506
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3507
17
{
3508
17
  isl_val_list **list = (isl_val_list **) user;
3509
17
  isl_val *val;
3510
17
3511
17
  val = isl_stream_read_val(s);
3512
17
  *list = isl_val_list_add(*list, val);
3513
17
  if (!*list)
3514
0
    return isl_space_free(space);
3515
17
3516
17
  return space;
3517
17
}
3518
3519
/* Read an isl_multi_val from "s".
3520
 *
3521
 * We first read a tuple space, collecting the element values in a list.
3522
 * Then we create an isl_multi_val from the space and the isl_val_list.
3523
 */
3524
__isl_give isl_multi_val *isl_stream_read_multi_val(__isl_keep isl_stream *s)
3525
7
{
3526
7
  struct vars *v;
3527
7
  isl_set *dom = NULL;
3528
7
  isl_space *space;
3529
7
  isl_multi_val *mv = NULL;
3530
7
  isl_val_list *list;
3531
7
3532
7
  v = vars_new(s->ctx);
3533
7
  if (!v)
3534
0
    return NULL;
3535
7
3536
7
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3537
7
  if (
next_is_tuple(s)7
)
{1
3538
1
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3539
1
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3540
0
      goto error;
3541
7
  }
3542
7
  
if (7
!isl_set_plain_is_universe(dom)7
)
3543
0
    isl_die(s->ctx, isl_error_invalid,
3544
7
      "expecting universe parameter domain", goto error);
3545
7
  
if (7
isl_stream_eat(s, '{')7
)
3546
0
    goto error;
3547
7
3548
7
  space = isl_set_get_space(dom);
3549
7
3550
7
  list = isl_val_list_alloc(s->ctx, 0);
3551
7
  space = read_tuple_space(s, v, space, 1, 0, &read_val_el, &list);
3552
7
  mv = isl_multi_val_from_val_list(space, list);
3553
7
3554
7
  if (isl_stream_eat(s, '}'))
3555
0
    goto error;
3556
7
3557
7
  vars_free(v);
3558
7
  isl_set_free(dom);
3559
7
  return mv;
3560
7
error:
3561
0
  vars_free(v);
3562
0
  isl_set_free(dom);
3563
0
  isl_multi_val_free(mv);
3564
7
  return NULL;
3565
7
}
3566
3567
/* Read an isl_multi_val from "str".
3568
 */
3569
__isl_give isl_multi_val *isl_multi_val_read_from_str(isl_ctx *ctx,
3570
  const char *str)
3571
7
{
3572
7
  isl_multi_val *mv;
3573
7
  isl_stream *s = isl_stream_new_str(ctx, str);
3574
7
  if (!s)
3575
0
    return NULL;
3576
7
  mv = isl_stream_read_multi_val(s);
3577
7
  isl_stream_free(s);
3578
7
  return mv;
3579
7
}
3580
3581
/* Read a multi-affine expression from "s".
3582
 * If the multi-affine expression has a domain, then the tuple
3583
 * representing this domain cannot involve any affine expressions.
3584
 * The tuple representing the actual expressions needs to consist
3585
 * of only affine expressions.  Moreover, these expressions can
3586
 * only depend on parameters and input dimensions and not on other
3587
 * output dimensions.
3588
 */
3589
__isl_give isl_multi_aff *isl_stream_read_multi_aff(__isl_keep isl_stream *s)
3590
114
{
3591
114
  struct vars *v;
3592
114
  isl_set *dom = NULL;
3593
114
  isl_multi_pw_aff *tuple = NULL;
3594
114
  int dim, i, n;
3595
114
  isl_space *space, *dom_space;
3596
114
  isl_multi_aff *ma = NULL;
3597
114
3598
114
  v = vars_new(s->ctx);
3599
114
  if (!v)
3600
0
    return NULL;
3601
114
3602
114
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3603
114
  if (
next_is_tuple(s)114
)
{6
3604
6
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3605
6
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3606
0
      goto error;
3607
114
  }
3608
114
  
if (114
!isl_set_plain_is_universe(dom)114
)
3609
0
    isl_die(s->ctx, isl_error_invalid,
3610
114
      "expecting universe parameter domain", goto error);
3611
114
  
if (114
isl_stream_eat(s, '{')114
)
3612
0
    goto error;
3613
114
3614
114
  tuple = read_tuple(s, v, 0, 0);
3615
114
  if (!tuple)
3616
0
    goto error;
3617
114
  
if (114
isl_stream_eat_if_available(s, ISL_TOKEN_TO)114
)
{107
3618
107
    isl_set *set;
3619
107
    isl_space *space;
3620
107
    int has_expr;
3621
107
3622
107
    has_expr = tuple_has_expr(tuple);
3623
107
    if (has_expr < 0)
3624
0
      goto error;
3625
107
    
if (107
has_expr107
)
3626
0
      isl_die(s->ctx, isl_error_invalid,
3627
107
        "expecting universe domain", goto error);
3628
107
    space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3629
107
    set = isl_set_universe(space);
3630
107
    dom = isl_set_intersect_params(set, dom);
3631
107
    isl_multi_pw_aff_free(tuple);
3632
107
    tuple = read_tuple(s, v, 0, 0);
3633
107
    if (!tuple)
3634
0
      goto error;
3635
114
  }
3636
114
3637
114
  
if (114
isl_stream_eat(s, '}')114
)
3638
0
    goto error;
3639
114
3640
114
  n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
3641
114
  dim = isl_set_dim(dom, isl_dim_all);
3642
114
  dom_space = isl_set_get_space(dom);
3643
114
  space = isl_space_range(isl_multi_pw_aff_get_space(tuple));
3644
114
  space = isl_space_align_params(space, isl_space_copy(dom_space));
3645
114
  if (!isl_space_is_params(dom_space))
3646
114
    space = isl_space_map_from_domain_and_range(
3647
114
        isl_space_copy(dom_space), space);
3648
114
  isl_space_free(dom_space);
3649
114
  ma = isl_multi_aff_alloc(space);
3650
114
3651
240
  for (i = 0; 
i < n240
;
++i126
)
{126
3652
126
    isl_pw_aff *pa;
3653
126
    isl_aff *aff;
3654
126
    pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
3655
126
    aff = aff_from_pw_aff(pa);
3656
126
    if (!aff)
3657
0
      goto error;
3658
126
    
if (126
isl_aff_involves_dims(aff, isl_dim_in, dim, i + 1)126
)
{0
3659
0
      isl_aff_free(aff);
3660
0
      isl_die(s->ctx, isl_error_invalid,
3661
0
        "not an affine expression", goto error);
3662
126
    }
3663
126
    aff = isl_aff_drop_dims(aff, isl_dim_in, dim, n);
3664
126
    space = isl_multi_aff_get_domain_space(ma);
3665
126
    aff = isl_aff_reset_domain_space(aff, space);
3666
126
    ma = isl_multi_aff_set_aff(ma, i, aff);
3667
126
  }
3668
114
3669
114
  isl_multi_pw_aff_free(tuple);
3670
114
  vars_free(v);
3671
114
  isl_set_free(dom);
3672
114
  return ma;
3673
114
error:
3674
0
  isl_multi_pw_aff_free(tuple);
3675
0
  vars_free(v);
3676
0
  isl_set_free(dom);
3677
0
  isl_multi_aff_free(ma);
3678
114
  return NULL;
3679
114
}
3680
3681
__isl_give isl_multi_aff *isl_multi_aff_read_from_str(isl_ctx *ctx,
3682
  const char *str)
3683
52
{
3684
52
  isl_multi_aff *maff;
3685
52
  isl_stream *s = isl_stream_new_str(ctx, str);
3686
52
  if (!s)
3687
0
    return NULL;
3688
52
  maff = isl_stream_read_multi_aff(s);
3689
52
  isl_stream_free(s);
3690
52
  return maff;
3691
52
}
3692
3693
/* Read an isl_multi_pw_aff from "s".
3694
 *
3695
 * The input format is similar to that of map, except that any conditions
3696
 * on the domains should be specified inside the tuple since each
3697
 * piecewise affine expression may have a different domain.
3698
 *
3699
 * Since we do not know in advance if the isl_multi_pw_aff lives
3700
 * in a set or a map space, we first read the first tuple and check
3701
 * if it is followed by a "->".  If so, we convert the tuple into
3702
 * the domain of the isl_multi_pw_aff and read in the next tuple.
3703
 * This tuple (or the first tuple if it was not followed by a "->")
3704
 * is then converted into the isl_multi_pw_aff through a call
3705
 * to extract_mpa_from_tuple and the domain of the result
3706
 * is intersected with the domain.
3707
 */
3708
__isl_give isl_multi_pw_aff *isl_stream_read_multi_pw_aff(
3709
  __isl_keep isl_stream *s)
3710
3
{
3711
3
  struct vars *v;
3712
3
  isl_set *dom = NULL;
3713
3
  isl_multi_pw_aff *tuple = NULL;
3714
3
  isl_multi_pw_aff *mpa = NULL;
3715
3
3716
3
  v = vars_new(s->ctx);
3717
3
  if (!v)
3718
0
    return NULL;
3719
3
3720
3
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3721
3
  if (
next_is_tuple(s)3
)
{0
3722
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3723
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3724
0
      goto error;
3725
3
  }
3726
3
  
if (3
isl_stream_eat(s, '{')3
)
3727
0
    goto error;
3728
3
3729
3
  tuple = read_tuple(s, v, 0, 0);
3730
3
  if (!tuple)
3731
0
    goto error;
3732
3
  
if (3
isl_stream_eat_if_available(s, ISL_TOKEN_TO)3
)
{3
3733
3
    isl_map *map = map_from_tuple(tuple, dom, isl_dim_in, v, 0);
3734
3
    dom = isl_map_domain(map);
3735
3
    tuple = read_tuple(s, v, 0, 0);
3736
3
    if (!tuple)
3737
0
      goto error;
3738
3
  }
3739
3
3740
3
  
if (3
isl_stream_eat(s, '}')3
)
3741
0
    goto error;
3742
3
3743
3
  mpa = extract_mpa_from_tuple(isl_set_get_space(dom), tuple);
3744
3
3745
3
  isl_multi_pw_aff_free(tuple);
3746
3
  vars_free(v);
3747
3
  mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
3748
3
  return mpa;
3749
3
error:
3750
0
  isl_multi_pw_aff_free(tuple);
3751
0
  vars_free(v);
3752
0
  isl_set_free(dom);
3753
0
  isl_multi_pw_aff_free(mpa);
3754
3
  return NULL;
3755
3
}
3756
3757
/* Read an isl_multi_pw_aff from "str".
3758
 */
3759
__isl_give isl_multi_pw_aff *isl_multi_pw_aff_read_from_str(isl_ctx *ctx,
3760
  const char *str)
3761
3
{
3762
3
  isl_multi_pw_aff *mpa;
3763
3
  isl_stream *s = isl_stream_new_str(ctx, str);
3764
3
  if (!s)
3765
0
    return NULL;
3766
3
  mpa = isl_stream_read_multi_pw_aff(s);
3767
3
  isl_stream_free(s);
3768
3
  return mpa;
3769
3
}
3770
3771
/* Read the body of an isl_union_pw_aff from "s" with parameter domain "dom".
3772
 */
3773
static __isl_give isl_union_pw_aff *read_union_pw_aff_with_dom(
3774
  __isl_keep isl_stream *s, __isl_take isl_set *dom, struct vars *v)
3775
27
{
3776
27
  isl_pw_aff *pa;
3777
27
  isl_union_pw_aff *upa = NULL;
3778
27
  isl_set *aff_dom;
3779
27
  int n;
3780
27
3781
27
  n = v->n;
3782
27
  aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3783
27
  pa = read_pw_aff_with_dom(s, aff_dom, v);
3784
27
  vars_drop(v, v->n - n);
3785
27
3786
27
  upa = isl_union_pw_aff_from_pw_aff(pa);
3787
27
3788
31
  while (
isl_stream_eat_if_available(s, ';')31
)
{4
3789
4
    isl_pw_aff *pa_i;
3790
4
    isl_union_pw_aff *upa_i;
3791
4
3792
4
    n = v->n;
3793
4
    aff_dom = read_aff_domain(s, isl_set_copy(dom), v);
3794
4
    pa_i = read_pw_aff_with_dom(s, aff_dom, v);
3795
4
    vars_drop(v, v->n - n);
3796
4
3797
4
    upa_i = isl_union_pw_aff_from_pw_aff(pa_i);
3798
4
    upa = isl_union_pw_aff_union_add(upa, upa_i);
3799
27
  }
3800
27
3801
27
  isl_set_free(dom);
3802
27
  return upa;
3803
27
}
3804
3805
/* Read an isl_union_pw_aff from "s".
3806
 *
3807
 * First check if there are any paramters, then read in the opening brace
3808
 * and use read_union_pw_aff_with_dom to read in the body of
3809
 * the isl_union_pw_aff.  Finally, read the closing brace.
3810
 */
3811
__isl_give isl_union_pw_aff *isl_stream_read_union_pw_aff(
3812
  __isl_keep isl_stream *s)
3813
0
{
3814
0
  struct vars *v;
3815
0
  isl_set *dom;
3816
0
  isl_union_pw_aff *upa = NULL;
3817
0
3818
0
  v = vars_new(s->ctx);
3819
0
  if (!v)
3820
0
    return NULL;
3821
0
3822
0
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3823
0
  if (
next_is_tuple(s)0
)
{0
3824
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3825
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3826
0
      goto error;
3827
0
  }
3828
0
  
if (0
isl_stream_eat(s, '{')0
)
3829
0
    goto error;
3830
0
3831
0
  upa = read_union_pw_aff_with_dom(s, isl_set_copy(dom), v);
3832
0
3833
0
  if (isl_stream_eat(s, '}'))
3834
0
    goto error;
3835
0
3836
0
  vars_free(v);
3837
0
  isl_set_free(dom);
3838
0
  return upa;
3839
0
error:
3840
0
  vars_free(v);
3841
0
  isl_set_free(dom);
3842
0
  isl_union_pw_aff_free(upa);
3843
0
  return NULL;
3844
0
}
3845
3846
/* Read an isl_union_pw_aff from "str".
3847
 */
3848
__isl_give isl_union_pw_aff *isl_union_pw_aff_read_from_str(isl_ctx *ctx,
3849
  const char *str)
3850
0
{
3851
0
  isl_union_pw_aff *upa;
3852
0
  isl_stream *s = isl_stream_new_str(ctx, str);
3853
0
  if (!s)
3854
0
    return NULL;
3855
0
  upa = isl_stream_read_union_pw_aff(s);
3856
0
  isl_stream_free(s);
3857
0
  return upa;
3858
0
}
3859
3860
/* This function is called for each element in a tuple inside
3861
 * isl_stream_read_multi_union_pw_aff.
3862
 *
3863
 * Read a '{', the union piecewise affine expression body and a '}' and
3864
 * add the isl_union_pw_aff to *list.
3865
 */
3866
static __isl_give isl_space *read_union_pw_aff_el(__isl_keep isl_stream *s,
3867
  struct vars *v, __isl_take isl_space *space, int rational, void *user)
3868
27
{
3869
27
  isl_set *dom;
3870
27
  isl_union_pw_aff *upa;
3871
27
  isl_union_pw_aff_list **list = (isl_union_pw_aff_list **) user;
3872
27
3873
27
  dom = isl_set_universe(isl_space_params(isl_space_copy(space)));
3874
27
  if (isl_stream_eat(s, '{'))
3875
0
    goto error;
3876
27
  upa = read_union_pw_aff_with_dom(s, dom, v);
3877
27
  *list = isl_union_pw_aff_list_add(*list, upa);
3878
27
  if (isl_stream_eat(s, '}'))
3879
0
    return isl_space_free(space);
3880
27
  
if (27
!*list27
)
3881
0
    return isl_space_free(space);
3882
27
  return space;
3883
27
error:
3884
0
  isl_set_free(dom);
3885
27
  return isl_space_free(space);
3886
27
}
3887
3888
/* Do the next tokens in "s" correspond to an empty tuple?
3889
 * In particular, does the stream start with a '[', followed by a ']',
3890
 * not followed by a "->"?
3891
 */
3892
static int next_is_empty_tuple(__isl_keep isl_stream *s)
3893
15
{
3894
15
  struct isl_token *tok, *tok2, *tok3;
3895
15
  int is_empty_tuple = 0;
3896
15
3897
15
  tok = isl_stream_next_token(s);
3898
15
  if (!tok)
3899
0
    return 0;
3900
15
  
if (15
tok->type != '['15
)
{0
3901
0
    isl_stream_push_token(s, tok);
3902
0
    return 0;
3903
15
  }
3904
15
3905
15
  tok2 = isl_stream_next_token(s);
3906
15
  if (
tok2 && 15
tok2->type == ']'15
)
{0
3907
0
    tok3 = isl_stream_next_token(s);
3908
0
    is_empty_tuple = !tok || tok->type != ISL_TOKEN_TO;
3909
0
    if (tok3)
3910
0
      isl_stream_push_token(s, tok3);
3911
15
  }
3912
15
  if (tok2)
3913
15
    isl_stream_push_token(s, tok2);
3914
15
  isl_stream_push_token(s, tok);
3915
15
3916
15
  return is_empty_tuple;
3917
15
}
3918
3919
/* Do the next tokens in "s" correspond to a tuple of parameters?
3920
 * In particular, does the stream start with a '[' that is not
3921
 * followed by a '{' or a nested tuple?
3922
 */
3923
static int next_is_param_tuple(__isl_keep isl_stream *s)
3924
15
{
3925
15
  struct isl_token *tok, *tok2;
3926
15
  int is_tuple;
3927
15
3928
15
  tok = isl_stream_next_token(s);
3929
15
  if (!tok)
3930
0
    return 0;
3931
15
  
if (15
tok->type != '[' || 15
next_is_tuple(s)15
)
{0
3932
0
    isl_stream_push_token(s, tok);
3933
0
    return 0;
3934
15
  }
3935
15
3936
15
  tok2 = isl_stream_next_token(s);
3937
15
  is_tuple = tok2 && tok2->type != '{';
3938
15
  if (tok2)
3939
15
    isl_stream_push_token(s, tok2);
3940
15
  isl_stream_push_token(s, tok);
3941
15
3942
15
  return is_tuple;
3943
15
}
3944
3945
/* Read an isl_multi_union_pw_aff from "s".
3946
 *
3947
 * The input has the form
3948
 *
3949
 *  [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3950
 *
3951
 * or
3952
 *
3953
 *  [..] -> [{ [..] : ... ; [..] : ... }, { [..] : ... ; [..] : ... }]
3954
 *
3955
 * We first check for the special case of an empty tuple "[]".
3956
 * Then we check if there are any parameters.
3957
 * Finally, we read the tuple, collecting the individual isl_union_pw_aff
3958
 * elements in a list and construct the result from the tuple space and
3959
 * the list.
3960
 */
3961
__isl_give isl_multi_union_pw_aff *isl_stream_read_multi_union_pw_aff(
3962
  __isl_keep isl_stream *s)
3963
15
{
3964
15
  struct vars *v;
3965
15
  isl_set *dom = NULL;
3966
15
  isl_space *space;
3967
15
  isl_multi_union_pw_aff *mupa = NULL;
3968
15
  isl_union_pw_aff_list *list;
3969
15
3970
15
  if (
next_is_empty_tuple(s)15
)
{0
3971
0
    if (isl_stream_eat(s, '['))
3972
0
      return NULL;
3973
0
    
if (0
isl_stream_eat(s, ']')0
)
3974
0
      return NULL;
3975
0
    space = isl_space_set_alloc(s->ctx, 0, 0);
3976
0
    return isl_multi_union_pw_aff_zero(space);
3977
15
  }
3978
15
3979
15
  v = vars_new(s->ctx);
3980
15
  if (!v)
3981
0
    return NULL;
3982
15
3983
15
  dom = isl_set_universe(isl_space_params_alloc(s->ctx, 0));
3984
15
  if (
next_is_param_tuple(s)15
)
{0
3985
0
    dom = read_map_tuple(s, dom, isl_dim_param, v, 1, 0);
3986
0
    if (isl_stream_eat(s, ISL_TOKEN_TO))
3987
0
      goto error;
3988
15
  }
3989
15
  space = isl_set_get_space(dom);
3990
15
  isl_set_free(dom);
3991
15
  list = isl_union_pw_aff_list_alloc(s->ctx, 0);
3992
15
  space = read_tuple_space(s, v, space, 1, 0,
3993
15
        &read_union_pw_aff_el, &list);
3994
15
  mupa = isl_multi_union_pw_aff_from_union_pw_aff_list(space, list);
3995
15
3996
15
  vars_free(v);
3997
15
3998
15
  return mupa;
3999
15
error:
4000
0
  vars_free(v);
4001
0
  isl_set_free(dom);
4002
0
  isl_multi_union_pw_aff_free(mupa);
4003
15
  return NULL;
4004
15
}
4005
4006
/* Read an isl_multi_union_pw_aff from "str".
4007
 */
4008
__isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_read_from_str(
4009
  isl_ctx *ctx, const char *str)
4010
15
{
4011
15
  isl_multi_union_pw_aff *mupa;
4012
15
  isl_stream *s = isl_stream_new_str(ctx, str);
4013
15
  if (!s)
4014
0
    return NULL;
4015
15
  mupa = isl_stream_read_multi_union_pw_aff(s);
4016
15
  isl_stream_free(s);
4017
15
  return mupa;
4018
15
}
4019
4020
__isl_give isl_union_pw_qpolynomial *isl_stream_read_union_pw_qpolynomial(
4021
  __isl_keep isl_stream *s)
4022
2
{
4023
2
  struct isl_obj obj;
4024
2
4025
2
  obj = obj_read(s);
4026
2
  if (
obj.type == 2
isl_obj_pw_qpolynomial2
)
{1
4027
1
    obj.type = isl_obj_union_pw_qpolynomial;
4028
1
    obj.v = isl_union_pw_qpolynomial_from_pw_qpolynomial(obj.v);
4029
2
  }
4030
2
  if (obj.v)
4031
2
    isl_assert(s->ctx, obj.type == isl_obj_union_pw_qpolynomial,
4032
2
         goto error);
4033
2
4034
2
  return obj.v;
4035
2
error:
4036
0
  obj.type->free(obj.v);
4037
2
  return NULL;
4038
2
}
4039
4040
__isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_read_from_str(
4041
  isl_ctx *ctx, const char *str)
4042
2
{
4043
2
  isl_union_pw_qpolynomial *upwqp;
4044
2
  isl_stream *s = isl_stream_new_str(ctx, str);
4045
2
  if (!s)
4046
0
    return NULL;
4047
2
  upwqp = isl_stream_read_union_pw_qpolynomial(s);
4048
2
  isl_stream_free(s);
4049
2
  return upwqp;
4050
2
}