+ isl_aff *aff;
+
+ if (!pa)
+ return -1;
+ if (pa->n != 1)
+ return 1;
+ if (!isl_set_plain_is_universe(pa->p[0].set))
+ return 1;
+
+ aff = pa->p[0].aff;
+ if (isl_int_is_zero(aff->v->el[aff->v->size - n + i]))
+ return 1;
+ return 0;
+}
+
+/* Does the tuple contain any dimensions that are defined
+ * in terms of earlier dimensions?
+ */
+static int tuple_has_expr(__isl_keep isl_multi_pw_aff *tuple)
+{
+ int i, n;
+ int has_expr = 0;
+ isl_pw_aff *pa;
+
+ if (!tuple)
+ return -1;
+ n = isl_multi_pw_aff_dim(tuple, isl_dim_out);
+ for (i = 0; i < n; ++i) {
+ pa = isl_multi_pw_aff_get_pw_aff(tuple, i);
+ has_expr = pw_aff_is_expr(pa, i, n);
+ isl_pw_aff_free(pa);
+ if (has_expr < 0 || has_expr)
+ break;
+ }
+
+ return has_expr;
+}
+
+/* Add a dimension to the given tuple.
+ * The dimension is initially undefined, so it is encoded
+ * as one times itself.
+ */
+static __isl_give isl_multi_pw_aff *tuple_add_dim(
+ __isl_take isl_multi_pw_aff *tuple, struct vars *v)
+{
+ isl_space *space;
+ isl_aff *aff;
+ isl_pw_aff *pa;
+
+ tuple = isl_multi_pw_aff_add_dims(tuple, isl_dim_in, 1);
+ space = isl_multi_pw_aff_get_domain_space(tuple);
+ aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
+ aff = isl_aff_add_coefficient_si(aff, isl_dim_in, v->n, 1);
+ pa = isl_pw_aff_from_aff(aff);
+ tuple = isl_multi_pw_aff_flat_range_product(tuple,
+ isl_multi_pw_aff_from_pw_aff(pa));
+
+ return tuple;
+}
+
+/* Set the name of dimension "pos" in "tuple" to "name".
+ * During printing, we add primes if the same name appears more than once
+ * to distinguish the occurrences. Here, we remove those primes from "name"
+ * before setting the name of the dimension.
+ */
+static __isl_give isl_multi_pw_aff *tuple_set_dim_name(
+ __isl_take isl_multi_pw_aff *tuple, int pos, char *name)
+{
+ char *prime;
+
+ if (!name)
+ return tuple;
+
+ prime = strchr(name, '\'');
+ if (prime)
+ *prime = '\0';
+ tuple = isl_multi_pw_aff_set_dim_name(tuple, isl_dim_set, pos, name);
+ if (prime)
+ *prime = '\'';
+
+ return tuple;
+}
+
+/* Read an affine expression from "s" and replace the definition
+ * of dimension "pos" in "tuple" by this expression.
+ *
+ * accept_extended_affine requires a wrapped space as input.
+ * The domain space of "tuple", on the other hand is an anonymous space,
+ * so we have to adjust the space of the isl_pw_aff before adding it
+ * to "tuple".
+ */
+static __isl_give isl_multi_pw_aff *read_tuple_var_def(struct isl_stream *s,
+ __isl_take isl_multi_pw_aff *tuple, int pos, struct vars *v,
+ int rational)
+{
+ isl_space *space;
+ isl_pw_aff *def;
+
+ space = isl_space_wrap(isl_space_alloc(s->ctx, 0, v->n, 0));
+ def = accept_extended_affine(s, space, v, rational);
+ space = isl_space_set_alloc(s->ctx, 0, v->n);
+ def = isl_pw_aff_reset_domain_space(def, space);
+ tuple = isl_multi_pw_aff_set_pw_aff(tuple, pos, def);
+
+ return tuple;
+}
+
+/* Read a list of variables and/or affine expressions and return the list
+ * as an isl_multi_pw_aff.
+ * The elements in the list are separated by either "," or "][".
+ * If "comma" is set then only "," is allowed.
+ */
+static __isl_give isl_multi_pw_aff *read_tuple_var_list(struct isl_stream *s,
+ struct vars *v, int rational, int comma)
+{
+ int i = 0;
+ struct isl_token *tok;
+ isl_multi_pw_aff *res;
+
+ res = tuple_alloc(v);
+
+ if (isl_stream_next_token_is(s, ']'))
+ return res;
+
+ while ((tok = next_token(s)) != NULL) {
+ int new_name = 0;
+
+ res = tuple_add_dim(res, v);
+
+ if (tok->type == ISL_TOKEN_IDENT) {
+ int n = v->n;
+ int p = vars_pos(v, tok->u.s, -1);
+ if (p < 0)
+ goto error;
+ new_name = p >= n;
+ }
+
+ if (tok->type == '*') {
+ if (vars_add_anon(v) < 0)
+ goto error;
+ isl_token_free(tok);
+ } else if (new_name) {
+ res = tuple_set_dim_name(res, i, v->v->name);
+ isl_token_free(tok);
+ if (isl_stream_eat_if_available(s, '='))
+ res = read_tuple_var_def(s, res, i, v,
+ rational);
+ } else {
+ isl_stream_push_token(s, tok);
+ tok = NULL;
+ if (vars_add_anon(v) < 0)
+ goto error;
+ res = read_tuple_var_def(s, res, i, v, rational);
+ }
+
+ tok = isl_stream_next_token(s);
+ if (!comma && tok && tok->type == ']' &&
+ isl_stream_next_token_is(s, '[')) {
+ isl_token_free(tok);
+ tok = isl_stream_next_token(s);
+ } else if (!tok || tok->type != ',')
+ break;
+
+ isl_token_free(tok);
+ i++;
+ }
+ if (tok)
+ isl_stream_push_token(s, tok);
+
+ return res;