add pet_union_map_move_dims
[pet.git] / parse.c
blob35f803945632328a9a346403d8232225ca985e12
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <stdlib.h>
36 #include <yaml.h>
38 #include "expr.h"
39 #include "loc.h"
40 #include "scop.h"
41 #include "scop_yaml.h"
42 #include "tree.h"
44 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
45 yaml_node_t *node)
47 if (node->type != YAML_SCALAR_NODE)
48 isl_die(ctx, isl_error_invalid, "expecting scalar node",
49 return NULL);
51 return strdup((char *) node->data.scalar.value);
54 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
55 yaml_node_t *node)
57 if (node->type != YAML_SCALAR_NODE)
58 isl_die(ctx, isl_error_invalid, "expecting scalar node",
59 return -1);
61 return atoi((char *) node->data.scalar.value);
64 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
65 yaml_node_t *node)
67 if (node->type != YAML_SCALAR_NODE)
68 isl_die(ctx, isl_error_invalid, "expecting scalar node",
69 return -1);
71 return strtod((char *) node->data.scalar.value, NULL);
74 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
75 yaml_document_t *document, yaml_node_t *node)
77 if (node->type != YAML_SCALAR_NODE)
78 isl_die(ctx, isl_error_invalid, "expecting scalar node",
79 return -1);
81 return pet_str_type((char *) node->data.scalar.value);
84 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
85 yaml_node_t *node)
87 if (node->type != YAML_SCALAR_NODE)
88 isl_die(ctx, isl_error_invalid, "expecting scalar node",
89 return -1);
91 return pet_str_op((char *) node->data.scalar.value);
94 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
95 yaml_node_t *node)
97 if (node->type != YAML_SCALAR_NODE)
98 isl_die(ctx, isl_error_invalid, "expecting scalar node",
99 return NULL);
101 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
104 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
105 yaml_node_t *node)
107 if (node->type != YAML_SCALAR_NODE)
108 isl_die(ctx, isl_error_invalid, "expecting scalar node",
109 return NULL);
111 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
114 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
115 yaml_node_t *node)
117 if (node->type != YAML_SCALAR_NODE)
118 isl_die(ctx, isl_error_invalid, "expecting scalar node",
119 return NULL);
121 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
124 /* Extract an isl_union_set from "node".
126 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
127 yaml_document_t *document, yaml_node_t *node)
129 if (node->type != YAML_SCALAR_NODE)
130 isl_die(ctx, isl_error_invalid, "expecting scalar node",
131 return NULL);
133 return isl_union_set_read_from_str(ctx,
134 (char *) node->data.scalar.value);
137 /* Extract an isl_union_map from "node".
139 static __isl_give isl_union_map *extract_union_map(isl_ctx *ctx,
140 yaml_document_t *document, yaml_node_t *node)
142 if (node->type != YAML_SCALAR_NODE)
143 isl_die(ctx, isl_error_invalid, "expecting scalar node",
144 return NULL);
146 return isl_union_map_read_from_str(ctx,
147 (char *) node->data.scalar.value);
150 /* Extract an isl_val from "node".
152 static __isl_give isl_val *extract_val(isl_ctx *ctx, yaml_document_t *document,
153 yaml_node_t *node)
155 if (node->type != YAML_SCALAR_NODE)
156 isl_die(ctx, isl_error_invalid, "expecting scalar node",
157 return NULL);
159 return isl_val_read_from_str(ctx, (char *) node->data.scalar.value);
162 /* Extract an isl_multi_pw_aff from "node".
164 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
165 yaml_document_t *document, yaml_node_t *node)
167 if (node->type != YAML_SCALAR_NODE)
168 isl_die(ctx, isl_error_invalid, "expecting scalar node",
169 return NULL);
171 return isl_multi_pw_aff_read_from_str(ctx,
172 (char *) node->data.scalar.value);
175 /* Extract a pet_type from "node".
177 static struct pet_type *extract_type(isl_ctx *ctx,
178 yaml_document_t *document, yaml_node_t *node)
180 struct pet_type *type;
181 yaml_node_pair_t * pair;
183 if (node->type != YAML_MAPPING_NODE)
184 isl_die(ctx, isl_error_invalid, "expecting mapping",
185 return NULL);
187 type = isl_calloc_type(ctx, struct pet_type);
188 if (!type)
189 return NULL;
191 for (pair = node->data.mapping.pairs.start;
192 pair < node->data.mapping.pairs.top; ++pair) {
193 yaml_node_t *key, *value;
195 key = yaml_document_get_node(document, pair->key);
196 value = yaml_document_get_node(document, pair->value);
198 if (key->type != YAML_SCALAR_NODE)
199 isl_die(ctx, isl_error_invalid, "expecting scalar key",
200 return pet_type_free(type));
202 if (!strcmp((char *) key->data.scalar.value, "name"))
203 type->name = extract_string(ctx, document, value);
204 if (!strcmp((char *) key->data.scalar.value, "definition"))
205 type->definition = extract_string(ctx, document, value);
208 return type;
211 /* Extract a sequence of types from "node" and store them in scop->types.
213 static struct pet_scop *extract_types(isl_ctx *ctx,
214 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
216 int i;
217 yaml_node_item_t *item;
219 if (node->type != YAML_SEQUENCE_NODE)
220 isl_die(ctx, isl_error_invalid, "expecting sequence",
221 return NULL);
223 scop->n_type = node->data.sequence.items.top
224 - node->data.sequence.items.start;
225 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
226 if (!scop->types)
227 return pet_scop_free(scop);
229 for (item = node->data.sequence.items.start, i = 0;
230 item < node->data.sequence.items.top; ++item, ++i) {
231 yaml_node_t *n;
233 n = yaml_document_get_node(document, *item);
234 scop->types[i] = extract_type(ctx, document, n);
235 if (!scop->types[i])
236 return pet_scop_free(scop);
239 return scop;
242 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
243 yaml_node_t *node)
245 struct pet_array *array;
246 yaml_node_pair_t * pair;
248 if (node->type != YAML_MAPPING_NODE)
249 isl_die(ctx, isl_error_invalid, "expecting mapping",
250 return NULL);
252 array = isl_calloc_type(ctx, struct pet_array);
253 if (!array)
254 return NULL;
256 for (pair = node->data.mapping.pairs.start;
257 pair < node->data.mapping.pairs.top; ++pair) {
258 yaml_node_t *key, *value;
260 key = yaml_document_get_node(document, pair->key);
261 value = yaml_document_get_node(document, pair->value);
263 if (key->type != YAML_SCALAR_NODE)
264 isl_die(ctx, isl_error_invalid, "expecting scalar key",
265 return pet_array_free(array));
267 if (!strcmp((char *) key->data.scalar.value, "context"))
268 array->context = extract_set(ctx, document, value);
269 if (!strcmp((char *) key->data.scalar.value, "extent"))
270 array->extent = extract_set(ctx, document, value);
271 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
272 array->value_bounds = extract_set(ctx, document, value);
273 if (!strcmp((char *) key->data.scalar.value, "element_type"))
274 array->element_type =
275 extract_string(ctx, document, value);
276 if (!strcmp((char *) key->data.scalar.value, "element_size"))
277 array->element_size = extract_int(ctx, document, value);
278 if (!strcmp((char *) key->data.scalar.value,
279 "element_is_record"))
280 array->element_is_record =
281 extract_int(ctx, document, value);
282 if (!strcmp((char *) key->data.scalar.value, "live_out"))
283 array->live_out = extract_int(ctx, document, value);
284 if (!strcmp((char *) key->data.scalar.value,
285 "uniquely_defined"))
286 array->uniquely_defined =
287 extract_int(ctx, document, value);
288 if (!strcmp((char *) key->data.scalar.value, "declared"))
289 array->declared = extract_int(ctx, document, value);
290 if (!strcmp((char *) key->data.scalar.value, "exposed"))
291 array->exposed = extract_int(ctx, document, value);
294 return array;
297 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
298 yaml_node_t *node, struct pet_scop *scop)
300 int i;
301 yaml_node_item_t *item;
303 if (node->type != YAML_SEQUENCE_NODE)
304 isl_die(ctx, isl_error_invalid, "expecting sequence",
305 return NULL);
307 scop->n_array = node->data.sequence.items.top
308 - node->data.sequence.items.start;
309 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
310 if (!scop->arrays)
311 return pet_scop_free(scop);
313 for (item = node->data.sequence.items.start, i = 0;
314 item < node->data.sequence.items.top; ++item, ++i) {
315 yaml_node_t *n;
317 n = yaml_document_get_node(document, *item);
318 scop->arrays[i] = extract_array(ctx, document, n);
319 if (!scop->arrays[i])
320 return pet_scop_free(scop);
323 return scop;
326 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
327 yaml_document_t *document, yaml_node_t *node);
329 static __isl_give pet_expr *extract_arguments(isl_ctx *ctx,
330 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
332 int i, n;
333 yaml_node_item_t *item;
335 if (node->type != YAML_SEQUENCE_NODE)
336 isl_die(ctx, isl_error_invalid, "expecting sequence",
337 return pet_expr_free(expr));
339 n = node->data.sequence.items.top - node->data.sequence.items.start;
340 expr = pet_expr_set_n_arg(expr, n);
342 for (item = node->data.sequence.items.start, i = 0;
343 item < node->data.sequence.items.top; ++item, ++i) {
344 yaml_node_t *n;
345 pet_expr *arg;
347 n = yaml_document_get_node(document, *item);
348 arg = extract_expr(ctx, document, n);
349 expr = pet_expr_set_arg(expr, i, arg);
352 return expr;
355 /* Extract pet_expr_double specific fields from "node" and
356 * update "expr" accordingly.
358 static __isl_give pet_expr *extract_expr_double(isl_ctx *ctx,
359 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
361 yaml_node_pair_t *pair;
362 double d = 0;
363 char *s = NULL;
365 for (pair = node->data.mapping.pairs.start;
366 pair < node->data.mapping.pairs.top; ++pair) {
367 yaml_node_t *key, *value;
369 key = yaml_document_get_node(document, pair->key);
370 value = yaml_document_get_node(document, pair->value);
372 if (key->type != YAML_SCALAR_NODE)
373 isl_die(ctx, isl_error_invalid, "expecting scalar key",
374 return pet_expr_free(expr));
376 if (!strcmp((char *) key->data.scalar.value, "value"))
377 d = extract_double(ctx, document, value);
378 if (!strcmp((char *) key->data.scalar.value, "string"))
379 s = extract_string(ctx, document, value);
382 expr = pet_expr_double_set(expr, d, s);
383 free(s);
385 return expr;
388 /* Extract pet_expr_access specific fields from "node" and
389 * update "expr" accordingly.
391 * The depth of the access is initialized by pet_expr_access_set_index.
392 * Any explicitly specified depth therefore needs to be set after
393 * setting the index expression. Similiarly, the access relation (if any)
394 * needs to be set after setting the depth.
396 static __isl_give pet_expr *extract_expr_access(isl_ctx *ctx,
397 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
399 yaml_node_pair_t *pair;
400 int depth = -1;
401 isl_multi_pw_aff *index = NULL;
403 for (pair = node->data.mapping.pairs.start;
404 pair < node->data.mapping.pairs.top; ++pair) {
405 yaml_node_t *key, *value;
407 key = yaml_document_get_node(document, pair->key);
408 value = yaml_document_get_node(document, pair->value);
410 if (key->type != YAML_SCALAR_NODE)
411 isl_die(ctx, isl_error_invalid, "expecting scalar key",
412 return pet_expr_free(expr));
414 if (!strcmp((char *) key->data.scalar.value, "index"))
415 index = extract_multi_pw_aff(ctx, document, value);
416 if (!strcmp((char *) key->data.scalar.value, "depth"))
417 depth = extract_int(ctx, document, value);
420 expr = pet_expr_access_set_index(expr, index);
421 if (depth >= 0)
422 expr = pet_expr_access_set_depth(expr, depth);
424 for (pair = node->data.mapping.pairs.start;
425 pair < node->data.mapping.pairs.top; ++pair) {
426 yaml_node_t *key, *value;
428 key = yaml_document_get_node(document, pair->key);
429 value = yaml_document_get_node(document, pair->value);
431 if (key->type != YAML_SCALAR_NODE)
432 isl_die(ctx, isl_error_invalid, "expecting scalar key",
433 return pet_expr_free(expr));
435 if (!strcmp((char *) key->data.scalar.value, "relation"))
436 expr = pet_expr_access_set_access(expr,
437 extract_map(ctx, document, value));
438 if (!strcmp((char *) key->data.scalar.value, "reference"))
439 expr = pet_expr_access_set_ref_id(expr,
440 extract_id(ctx, document, value));
441 if (!strcmp((char *) key->data.scalar.value, "read"))
442 expr = pet_expr_access_set_read(expr,
443 extract_int(ctx, document, value));
444 if (!strcmp((char *) key->data.scalar.value, "write"))
445 expr = pet_expr_access_set_write(expr,
446 extract_int(ctx, document, value));
447 if (!strcmp((char *) key->data.scalar.value, "kill"))
448 expr = pet_expr_access_set_kill(expr,
449 extract_int(ctx, document, value));
452 return expr;
455 /* Extract operation expression specific fields from "node" and
456 * update "expr" accordingly.
458 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
459 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
461 yaml_node_pair_t *pair;
463 for (pair = node->data.mapping.pairs.start;
464 pair < node->data.mapping.pairs.top; ++pair) {
465 yaml_node_t *key, *value;
467 key = yaml_document_get_node(document, pair->key);
468 value = yaml_document_get_node(document, pair->value);
470 if (key->type != YAML_SCALAR_NODE)
471 isl_die(ctx, isl_error_invalid, "expecting scalar key",
472 return pet_expr_free(expr));
474 if (!strcmp((char *) key->data.scalar.value, "operation"))
475 expr = pet_expr_op_set_type(expr,
476 extract_op(ctx, document, value));
479 return expr;
482 /* Extract pet_expr_call specific fields from "node" and
483 * update "expr" accordingly.
485 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
486 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
488 yaml_node_pair_t *pair;
490 for (pair = node->data.mapping.pairs.start;
491 pair < node->data.mapping.pairs.top; ++pair) {
492 yaml_node_t *key, *value;
494 key = yaml_document_get_node(document, pair->key);
495 value = yaml_document_get_node(document, pair->value);
497 if (key->type != YAML_SCALAR_NODE)
498 isl_die(ctx, isl_error_invalid, "expecting scalar key",
499 return pet_expr_free(expr));
501 if (!strcmp((char *) key->data.scalar.value, "name"))
502 expr = pet_expr_call_set_name(expr,
503 extract_string(ctx, document, value));
506 return expr;
509 /* Extract pet_expr_cast specific fields from "node" and
510 * update "expr" accordingly.
512 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
513 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
515 yaml_node_pair_t *pair;
517 for (pair = node->data.mapping.pairs.start;
518 pair < node->data.mapping.pairs.top; ++pair) {
519 yaml_node_t *key, *value;
521 key = yaml_document_get_node(document, pair->key);
522 value = yaml_document_get_node(document, pair->value);
524 if (key->type != YAML_SCALAR_NODE)
525 isl_die(ctx, isl_error_invalid, "expecting scalar key",
526 return pet_expr_free(expr));
528 if (!strcmp((char *) key->data.scalar.value, "type_name"))
529 expr = pet_expr_cast_set_type_name(expr,
530 extract_string(ctx, document, value));
533 return expr;
536 /* Extract pet_expr_int specific fields from "node" and
537 * update "expr" accordingly.
539 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
540 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
542 yaml_node_pair_t * pair;
544 for (pair = node->data.mapping.pairs.start;
545 pair < node->data.mapping.pairs.top; ++pair) {
546 yaml_node_t *key, *value;
548 key = yaml_document_get_node(document, pair->key);
549 value = yaml_document_get_node(document, pair->value);
551 if (key->type != YAML_SCALAR_NODE)
552 isl_die(ctx, isl_error_invalid, "expecting scalar key",
553 return pet_expr_free(expr));
555 if (!strcmp((char *) key->data.scalar.value, "value"))
556 expr = pet_expr_int_set_val(expr,
557 extract_val(ctx, document, value));
560 return expr;
563 /* Extract a pet_expr from "node".
565 * We first extract the type and arguments of the expression and
566 * then extract additional fields depending on the type.
568 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
569 yaml_document_t *document, yaml_node_t *node)
571 enum pet_expr_type type = pet_expr_error;
572 pet_expr *expr;
573 yaml_node_pair_t *pair;
575 if (node->type != YAML_MAPPING_NODE)
576 isl_die(ctx, isl_error_invalid, "expecting mapping",
577 return NULL);
579 for (pair = node->data.mapping.pairs.start;
580 pair < node->data.mapping.pairs.top; ++pair) {
581 yaml_node_t *key, *value;
583 key = yaml_document_get_node(document, pair->key);
584 value = yaml_document_get_node(document, pair->value);
586 if (key->type != YAML_SCALAR_NODE)
587 isl_die(ctx, isl_error_invalid, "expecting scalar key",
588 return pet_expr_free(expr));
590 if (!strcmp((char *) key->data.scalar.value, "type"))
591 type = extract_expr_type(ctx, document, value);
594 if (type == pet_expr_error)
595 isl_die(ctx, isl_error_invalid, "cannot determine type",
596 return NULL);
598 expr = pet_expr_alloc(ctx, type);
599 if (!expr)
600 return NULL;
602 for (pair = node->data.mapping.pairs.start;
603 pair < node->data.mapping.pairs.top; ++pair) {
604 yaml_node_t *key, *value;
606 key = yaml_document_get_node(document, pair->key);
607 value = yaml_document_get_node(document, pair->value);
609 if (!strcmp((char *) key->data.scalar.value, "arguments"))
610 expr = extract_arguments(ctx, document, value, expr);
611 if (!expr)
612 return NULL;
615 switch (type) {
616 case pet_expr_error:
617 isl_die(ctx, isl_error_internal, "unreachable code",
618 return NULL);
619 case pet_expr_access:
620 expr = extract_expr_access(ctx, document, node, expr);
621 break;
622 case pet_expr_double:
623 expr = extract_expr_double(ctx, document, node, expr);
624 break;
625 case pet_expr_call:
626 expr = extract_expr_call(ctx, document, node, expr);
627 break;
628 case pet_expr_cast:
629 expr = extract_expr_cast(ctx, document, node, expr);
630 break;
631 case pet_expr_int:
632 expr = extract_expr_int(ctx, document, node, expr);
633 break;
634 case pet_expr_op:
635 expr = extract_expr_op(ctx, document, node, expr);
636 break;
639 return expr;
642 /* Extract a pet_tree_type from "node".
644 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
645 yaml_document_t *document, yaml_node_t *node)
647 if (node->type != YAML_SCALAR_NODE)
648 isl_die(ctx, isl_error_invalid, "expecting scalar node",
649 return -1);
651 return pet_tree_str_type((char *) node->data.scalar.value);
654 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
655 yaml_document_t *document, yaml_node_t *node);
657 /* Extract a pet_tree of type pet_tree_block from "node".
659 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
660 yaml_document_t *document, yaml_node_t *node)
662 int block = 0;
663 int i, n;
664 yaml_node_pair_t *pair;
665 yaml_node_item_t *item;
666 yaml_node_t *children = NULL;
667 pet_tree *tree;
669 for (pair = node->data.mapping.pairs.start;
670 pair < node->data.mapping.pairs.top; ++pair) {
671 yaml_node_t *key, *value;
673 key = yaml_document_get_node(document, pair->key);
674 value = yaml_document_get_node(document, pair->value);
676 if (key->type != YAML_SCALAR_NODE)
677 isl_die(ctx, isl_error_invalid, "expecting scalar key",
678 return NULL);
680 if (!strcmp((char *) key->data.scalar.value, "block"))
681 block = extract_int(ctx, document, value);
682 if (!strcmp((char *) key->data.scalar.value, "children"))
683 children = value;
686 if (!children)
687 n = 0;
688 else
689 n = children->data.sequence.items.top -
690 children->data.sequence.items.start;
692 tree = pet_tree_new_block(ctx, block, n);
693 if (!children)
694 return tree;
696 for (item = children->data.sequence.items.start, i = 0;
697 item < children->data.sequence.items.top; ++item, ++i) {
698 yaml_node_t *n;
699 pet_tree *child;
701 n = yaml_document_get_node(document, *item);
702 child = extract_tree(ctx, document, n);
703 tree = pet_tree_block_add_child(tree, child);
706 return tree;
709 /* Extract a pet_tree of type pet_tree_decl from "node".
711 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
712 yaml_document_t *document, yaml_node_t *node)
714 yaml_node_pair_t *pair;
715 pet_expr *var = NULL;
717 for (pair = node->data.mapping.pairs.start;
718 pair < node->data.mapping.pairs.top; ++pair) {
719 yaml_node_t *key, *value;
721 key = yaml_document_get_node(document, pair->key);
722 value = yaml_document_get_node(document, pair->value);
724 if (key->type != YAML_SCALAR_NODE)
725 isl_die(ctx, isl_error_invalid, "expecting scalar key",
726 return NULL);
728 if (!strcmp((char *) key->data.scalar.value, "variable")) {
729 var = extract_expr(ctx, document, value);
730 if (!var)
731 return NULL;
735 if (!var)
736 isl_die(ctx, isl_error_invalid,
737 "no variable field", return NULL);
739 return pet_tree_new_decl(var);
742 /* Extract a pet_tree of type pet_tree_decl_init from "node".
744 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
745 yaml_document_t *document, yaml_node_t *node)
747 yaml_node_pair_t *pair;
748 pet_expr *var = NULL;
749 pet_expr *init = NULL;
751 for (pair = node->data.mapping.pairs.start;
752 pair < node->data.mapping.pairs.top; ++pair) {
753 yaml_node_t *key, *value;
755 key = yaml_document_get_node(document, pair->key);
756 value = yaml_document_get_node(document, pair->value);
758 if (key->type != YAML_SCALAR_NODE)
759 isl_die(ctx, isl_error_invalid, "expecting scalar key",
760 return NULL);
762 if (!strcmp((char *) key->data.scalar.value, "variable")) {
763 var = extract_expr(ctx, document, value);
764 if (!var)
765 goto error;
767 if (!strcmp((char *) key->data.scalar.value,
768 "initialization")) {
769 init = extract_expr(ctx, document, value);
770 if (!init)
771 goto error;
775 if (!var)
776 isl_die(ctx, isl_error_invalid,
777 "no variable field", goto error);
778 if (!init)
779 isl_die(ctx, isl_error_invalid,
780 "no initialization field", goto error);
782 return pet_tree_new_decl_init(var, init);
783 error:
784 pet_expr_free(var);
785 pet_expr_free(init);
786 return NULL;
789 /* Extract a pet_tree of type pet_tree_expr from "node".
791 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
792 yaml_document_t *document, yaml_node_t *node)
794 yaml_node_pair_t *pair;
795 pet_expr *expr = NULL;
797 for (pair = node->data.mapping.pairs.start;
798 pair < node->data.mapping.pairs.top; ++pair) {
799 yaml_node_t *key, *value;
801 key = yaml_document_get_node(document, pair->key);
802 value = yaml_document_get_node(document, pair->value);
804 if (key->type != YAML_SCALAR_NODE)
805 isl_die(ctx, isl_error_invalid, "expecting scalar key",
806 return NULL);
808 if (!strcmp((char *) key->data.scalar.value, "expr")) {
809 expr = extract_expr(ctx, document, value);
810 if (!expr)
811 return NULL;
815 if (!expr)
816 isl_die(ctx, isl_error_invalid,
817 "no expr field", return NULL);
819 return pet_tree_new_expr(expr);
822 /* Extract a pet_tree of type pet_tree_while from "node".
824 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
825 yaml_document_t *document, yaml_node_t *node)
827 yaml_node_pair_t *pair;
828 pet_expr *cond = NULL;
829 pet_tree *body = NULL;
831 for (pair = node->data.mapping.pairs.start;
832 pair < node->data.mapping.pairs.top; ++pair) {
833 yaml_node_t *key, *value;
835 key = yaml_document_get_node(document, pair->key);
836 value = yaml_document_get_node(document, pair->value);
838 if (key->type != YAML_SCALAR_NODE)
839 isl_die(ctx, isl_error_invalid, "expecting scalar key",
840 return NULL);
842 if (!strcmp((char *) key->data.scalar.value, "condition")) {
843 cond = extract_expr(ctx, document, value);
844 if (!cond)
845 goto error;
847 if (!strcmp((char *) key->data.scalar.value, "body")) {
848 body = extract_tree(ctx, document, value);
849 if (!body)
850 goto error;
854 if (!cond)
855 isl_die(ctx, isl_error_invalid,
856 "no condition field", goto error);
857 if (!body)
858 isl_die(ctx, isl_error_invalid,
859 "no body field", goto error);
861 return pet_tree_new_while(cond, body);
862 error:
863 pet_expr_free(cond);
864 pet_tree_free(body);
865 return NULL;
868 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
870 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
871 yaml_document_t *document, yaml_node_t *node)
873 yaml_node_pair_t *pair;
874 pet_tree *body;
876 for (pair = node->data.mapping.pairs.start;
877 pair < node->data.mapping.pairs.top; ++pair) {
878 yaml_node_t *key, *value;
880 key = yaml_document_get_node(document, pair->key);
881 value = yaml_document_get_node(document, pair->value);
883 if (key->type != YAML_SCALAR_NODE)
884 isl_die(ctx, isl_error_invalid, "expecting scalar key",
885 return NULL);
887 if (!strcmp((char *) key->data.scalar.value, "body")) {
888 body = extract_tree(ctx, document, value);
889 if (!body)
890 return NULL;
894 if (!body)
895 isl_die(ctx, isl_error_invalid,
896 "no body field", return NULL);
898 return pet_tree_new_infinite_loop(body);
901 /* Extract a pet_tree of type pet_tree_if from "node".
903 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
904 yaml_document_t *document, yaml_node_t *node)
906 yaml_node_pair_t *pair;
907 pet_expr *cond = NULL;
908 pet_tree *then_body = NULL;
910 for (pair = node->data.mapping.pairs.start;
911 pair < node->data.mapping.pairs.top; ++pair) {
912 yaml_node_t *key, *value;
914 key = yaml_document_get_node(document, pair->key);
915 value = yaml_document_get_node(document, pair->value);
917 if (key->type != YAML_SCALAR_NODE)
918 isl_die(ctx, isl_error_invalid, "expecting scalar key",
919 return NULL);
921 if (!strcmp((char *) key->data.scalar.value, "condition")) {
922 cond = extract_expr(ctx, document, value);
923 if (!cond)
924 goto error;
926 if (!strcmp((char *) key->data.scalar.value, "then")) {
927 then_body = extract_tree(ctx, document, value);
928 if (!then_body)
929 goto error;
933 if (!cond)
934 isl_die(ctx, isl_error_invalid,
935 "no condition field", goto error);
936 if (!then_body)
937 isl_die(ctx, isl_error_invalid,
938 "no then body", goto error);
940 return pet_tree_new_if(cond, then_body);
941 error:
942 pet_expr_free(cond);
943 pet_tree_free(then_body);
944 return NULL;
947 /* Extract a pet_tree of type pet_tree_if_else from "node".
949 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
950 yaml_document_t *document, yaml_node_t *node)
952 yaml_node_pair_t *pair;
953 pet_expr *cond = NULL;
954 pet_tree *then_body = NULL;
955 pet_tree *else_body = NULL;
957 for (pair = node->data.mapping.pairs.start;
958 pair < node->data.mapping.pairs.top; ++pair) {
959 yaml_node_t *key, *value;
961 key = yaml_document_get_node(document, pair->key);
962 value = yaml_document_get_node(document, pair->value);
964 if (key->type != YAML_SCALAR_NODE)
965 isl_die(ctx, isl_error_invalid, "expecting scalar key",
966 return NULL);
968 if (!strcmp((char *) key->data.scalar.value, "condition")) {
969 cond = extract_expr(ctx, document, value);
970 if (!cond)
971 goto error;
973 if (!strcmp((char *) key->data.scalar.value, "then")) {
974 then_body = extract_tree(ctx, document, value);
975 if (!then_body)
976 goto error;
978 if (!strcmp((char *) key->data.scalar.value, "else")) {
979 else_body = extract_tree(ctx, document, value);
980 if (!else_body)
981 goto error;
985 if (!cond)
986 isl_die(ctx, isl_error_invalid,
987 "no condition field", goto error);
988 if (!then_body)
989 isl_die(ctx, isl_error_invalid,
990 "no then body", goto error);
991 if (!else_body)
992 isl_die(ctx, isl_error_invalid,
993 "no else body", goto error);
995 return pet_tree_new_if_else(cond, then_body, else_body);
996 error:
997 pet_expr_free(cond);
998 pet_tree_free(then_body);
999 pet_tree_free(else_body);
1000 return NULL;
1003 /* Extract a pet_tree of type pet_tree_for from "node".
1005 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1006 yaml_document_t *document, yaml_node_t *node)
1008 yaml_node_pair_t *pair;
1009 int declared = 0;
1010 int independent = 0;
1011 pet_expr *iv = NULL;
1012 pet_expr *init = NULL;
1013 pet_expr *cond = NULL;
1014 pet_expr *inc = NULL;
1015 pet_tree *body = NULL;
1017 for (pair = node->data.mapping.pairs.start;
1018 pair < node->data.mapping.pairs.top; ++pair) {
1019 yaml_node_t *key, *value;
1021 key = yaml_document_get_node(document, pair->key);
1022 value = yaml_document_get_node(document, pair->value);
1024 if (key->type != YAML_SCALAR_NODE)
1025 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1026 return NULL);
1028 if (!strcmp((char *) key->data.scalar.value, "declared"))
1029 declared = extract_int(ctx, document, value);
1030 if (!strcmp((char *) key->data.scalar.value, "independent"))
1031 independent = extract_int(ctx, document, value);
1032 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1033 iv = extract_expr(ctx, document, value);
1034 if (!iv)
1035 goto error;
1037 if (!strcmp((char *) key->data.scalar.value,
1038 "initialization")) {
1039 init = extract_expr(ctx, document, value);
1040 if (!init)
1041 goto error;
1043 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1044 cond = extract_expr(ctx, document, value);
1045 if (!cond)
1046 goto error;
1048 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1049 inc = extract_expr(ctx, document, value);
1050 if (!inc)
1051 goto error;
1053 if (!strcmp((char *) key->data.scalar.value, "body")) {
1054 body = extract_tree(ctx, document, value);
1055 if (!body)
1056 goto error;
1060 if (!iv)
1061 isl_die(ctx, isl_error_invalid,
1062 "no variable field", goto error);
1063 if (!init)
1064 isl_die(ctx, isl_error_invalid,
1065 "no initialization field", goto error);
1066 if (!cond)
1067 isl_die(ctx, isl_error_invalid,
1068 "no condition field", goto error);
1069 if (!inc)
1070 isl_die(ctx, isl_error_invalid,
1071 "no increment field", goto error);
1072 if (!body)
1073 isl_die(ctx, isl_error_invalid,
1074 "no body field", goto error);
1076 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1077 body);
1078 error:
1079 pet_expr_free(iv);
1080 pet_expr_free(init);
1081 pet_expr_free(cond);
1082 pet_expr_free(inc);
1083 pet_tree_free(body);
1084 return NULL;
1087 /* Extract a pet_tree from "node".
1089 * We first extract the type of the pet_tree and then call
1090 * the appropriate function to extract and construct a pet_tree
1091 * of that type.
1093 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1094 yaml_document_t *document, yaml_node_t *node)
1096 enum pet_tree_type type = pet_tree_error;
1097 pet_tree *tree;
1098 yaml_node_pair_t *pair;
1100 if (node->type != YAML_MAPPING_NODE)
1101 isl_die(ctx, isl_error_invalid, "expecting mapping",
1102 return NULL);
1104 for (pair = node->data.mapping.pairs.start;
1105 pair < node->data.mapping.pairs.top; ++pair) {
1106 yaml_node_t *key, *value;
1108 key = yaml_document_get_node(document, pair->key);
1109 value = yaml_document_get_node(document, pair->value);
1111 if (key->type != YAML_SCALAR_NODE)
1112 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1113 return pet_tree_free(tree));
1115 if (!strcmp((char *) key->data.scalar.value, "type"))
1116 type = extract_tree_type(ctx, document, value);
1119 if (type == pet_tree_error)
1120 isl_die(ctx, isl_error_invalid, "cannot determine type",
1121 return NULL);
1123 switch (type) {
1124 case pet_tree_error:
1125 return NULL;
1126 case pet_tree_block:
1127 tree = extract_tree_block(ctx, document, node);
1128 break;
1129 case pet_tree_break:
1130 tree = pet_tree_new_break(ctx);
1131 break;
1132 case pet_tree_continue:
1133 tree = pet_tree_new_continue(ctx);
1134 break;
1135 case pet_tree_decl:
1136 tree = extract_tree_decl(ctx, document, node);
1137 break;
1138 case pet_tree_decl_init:
1139 tree = extract_tree_decl_init(ctx, document, node);
1140 break;
1141 case pet_tree_expr:
1142 tree = extract_tree_expr(ctx, document, node);
1143 break;
1144 case pet_tree_for:
1145 tree = extract_tree_for(ctx, document, node);
1146 break;
1147 case pet_tree_while:
1148 tree = extract_tree_while(ctx, document, node);
1149 break;
1150 case pet_tree_infinite_loop:
1151 tree = extract_tree_infinite_loop(ctx, document, node);
1152 break;
1153 case pet_tree_if:
1154 tree = extract_tree_if(ctx, document, node);
1155 break;
1156 case pet_tree_if_else:
1157 tree = extract_tree_if_else(ctx, document, node);
1158 break;
1161 return tree;
1164 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1165 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1167 int i;
1168 yaml_node_item_t *item;
1170 if (node->type != YAML_SEQUENCE_NODE)
1171 isl_die(ctx, isl_error_invalid, "expecting sequence",
1172 return pet_stmt_free(stmt));
1174 stmt->n_arg = node->data.sequence.items.top
1175 - node->data.sequence.items.start;
1176 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1177 if (!stmt->args)
1178 return pet_stmt_free(stmt);
1180 for (item = node->data.sequence.items.start, i = 0;
1181 item < node->data.sequence.items.top; ++item, ++i) {
1182 yaml_node_t *n;
1184 n = yaml_document_get_node(document, *item);
1185 stmt->args[i] = extract_expr(ctx, document, n);
1186 if (!stmt->args[i])
1187 return pet_stmt_free(stmt);
1190 return stmt;
1193 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1194 yaml_node_t *node)
1196 struct pet_stmt *stmt;
1197 yaml_node_pair_t * pair;
1198 int line = -1;
1199 unsigned start = 0, end = 0;
1200 char *indent = NULL;
1202 if (node->type != YAML_MAPPING_NODE)
1203 isl_die(ctx, isl_error_invalid, "expecting mapping",
1204 return NULL);
1206 stmt = isl_calloc_type(ctx, struct pet_stmt);
1207 if (!stmt)
1208 return NULL;
1210 stmt->loc = &pet_loc_dummy;
1212 for (pair = node->data.mapping.pairs.start;
1213 pair < node->data.mapping.pairs.top; ++pair) {
1214 yaml_node_t *key, *value;
1216 key = yaml_document_get_node(document, pair->key);
1217 value = yaml_document_get_node(document, pair->value);
1219 if (key->type != YAML_SCALAR_NODE)
1220 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1221 return pet_stmt_free(stmt));
1223 if (!strcmp((char *) key->data.scalar.value, "indent"))
1224 indent = extract_string(ctx, document, value);
1225 if (!strcmp((char *) key->data.scalar.value, "line"))
1226 line = extract_int(ctx, document, value);
1227 if (!strcmp((char *) key->data.scalar.value, "start"))
1228 start = extract_int(ctx, document, value);
1229 if (!strcmp((char *) key->data.scalar.value, "end"))
1230 end = extract_int(ctx, document, value);
1231 if (!strcmp((char *) key->data.scalar.value, "domain"))
1232 stmt->domain = extract_set(ctx, document, value);
1233 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1234 stmt->schedule = extract_map(ctx, document, value);
1235 if (!strcmp((char *) key->data.scalar.value, "body"))
1236 stmt->body = extract_tree(ctx, document, value);
1238 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1239 stmt = extract_stmt_arguments(ctx, document,
1240 value, stmt);
1241 if (!stmt)
1242 return NULL;
1245 if (!indent)
1246 indent = strdup("");
1247 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1248 if (!stmt->loc)
1249 return pet_stmt_free(stmt);
1251 return stmt;
1254 static struct pet_scop *extract_statements(isl_ctx *ctx,
1255 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1257 int i;
1258 yaml_node_item_t *item;
1260 if (node->type != YAML_SEQUENCE_NODE)
1261 isl_die(ctx, isl_error_invalid, "expecting sequence",
1262 return NULL);
1264 scop->n_stmt = node->data.sequence.items.top
1265 - node->data.sequence.items.start;
1266 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1267 if (!scop->stmts)
1268 return pet_scop_free(scop);
1270 for (item = node->data.sequence.items.start, i = 0;
1271 item < node->data.sequence.items.top; ++item, ++i) {
1272 yaml_node_t *n;
1274 n = yaml_document_get_node(document, *item);
1275 scop->stmts[i] = extract_stmt(ctx, document, n);
1276 if (!scop->stmts[i])
1277 return pet_scop_free(scop);
1280 return scop;
1283 /* Extract a pet_implication from "node".
1285 static struct pet_implication *extract_implication(isl_ctx *ctx,
1286 yaml_document_t *document, yaml_node_t *node)
1288 struct pet_implication *implication;
1289 yaml_node_pair_t * pair;
1291 if (node->type != YAML_MAPPING_NODE)
1292 isl_die(ctx, isl_error_invalid, "expecting mapping",
1293 return NULL);
1295 implication = isl_calloc_type(ctx, struct pet_implication);
1296 if (!implication)
1297 return NULL;
1299 for (pair = node->data.mapping.pairs.start;
1300 pair < node->data.mapping.pairs.top; ++pair) {
1301 yaml_node_t *key, *value;
1303 key = yaml_document_get_node(document, pair->key);
1304 value = yaml_document_get_node(document, pair->value);
1306 if (key->type != YAML_SCALAR_NODE)
1307 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1308 return pet_implication_free(implication));
1310 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1311 implication->satisfied =
1312 extract_int(ctx, document, value);
1313 if (!strcmp((char *) key->data.scalar.value, "extension"))
1314 implication->extension =
1315 extract_map(ctx, document, value);
1318 return implication;
1321 /* Extract a sequence of implications from "node" and
1322 * store them in scop->implications.
1324 static struct pet_scop *extract_implications(isl_ctx *ctx,
1325 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1327 int i;
1328 yaml_node_item_t *item;
1330 if (node->type != YAML_SEQUENCE_NODE)
1331 isl_die(ctx, isl_error_invalid, "expecting sequence",
1332 return NULL);
1334 scop->n_implication = node->data.sequence.items.top
1335 - node->data.sequence.items.start;
1336 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1337 scop->n_implication);
1338 if (!scop->implications)
1339 return pet_scop_free(scop);
1341 for (item = node->data.sequence.items.start, i = 0;
1342 item < node->data.sequence.items.top; ++item, ++i) {
1343 yaml_node_t *n;
1345 n = yaml_document_get_node(document, *item);
1346 scop->implications[i] = extract_implication(ctx, document, n);
1347 if (!scop->implications[i])
1348 return pet_scop_free(scop);
1351 return scop;
1354 /* Extract a pet_independence from "node".
1356 static struct pet_independence *extract_independence(isl_ctx *ctx,
1357 yaml_document_t *document, yaml_node_t *node)
1359 struct pet_independence *independence;
1360 yaml_node_pair_t * pair;
1362 if (node->type != YAML_MAPPING_NODE)
1363 isl_die(ctx, isl_error_invalid, "expecting mapping",
1364 return NULL);
1366 independence = isl_calloc_type(ctx, struct pet_independence);
1367 if (!independence)
1368 return NULL;
1370 for (pair = node->data.mapping.pairs.start;
1371 pair < node->data.mapping.pairs.top; ++pair) {
1372 yaml_node_t *key, *value;
1374 key = yaml_document_get_node(document, pair->key);
1375 value = yaml_document_get_node(document, pair->value);
1377 if (key->type != YAML_SCALAR_NODE)
1378 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1379 return pet_independence_free(independence));
1381 if (!strcmp((char *) key->data.scalar.value, "filter"))
1382 independence->filter =
1383 extract_union_map(ctx, document, value);
1384 if (!strcmp((char *) key->data.scalar.value, "local"))
1385 independence->local =
1386 extract_union_set(ctx, document, value);
1389 if (!independence->filter)
1390 isl_die(ctx, isl_error_invalid, "no filter field",
1391 return pet_independence_free(independence));
1392 if (!independence->local)
1393 isl_die(ctx, isl_error_invalid, "no local field",
1394 return pet_independence_free(independence));
1396 return independence;
1399 /* Extract a sequence of independences from "node" and
1400 * store them in scop->independences.
1402 static struct pet_scop *extract_independences(isl_ctx *ctx,
1403 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1405 int i;
1406 yaml_node_item_t *item;
1408 if (node->type != YAML_SEQUENCE_NODE)
1409 isl_die(ctx, isl_error_invalid, "expecting sequence",
1410 return NULL);
1412 scop->n_independence = node->data.sequence.items.top
1413 - node->data.sequence.items.start;
1414 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1415 scop->n_independence);
1416 if (!scop->independences)
1417 return pet_scop_free(scop);
1419 for (item = node->data.sequence.items.start, i = 0;
1420 item < node->data.sequence.items.top; ++item, ++i) {
1421 yaml_node_t *n;
1423 n = yaml_document_get_node(document, *item);
1424 scop->independences[i] = extract_independence(ctx, document, n);
1425 if (!scop->independences[i])
1426 return pet_scop_free(scop);
1429 return scop;
1432 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1433 yaml_node_t *node)
1435 struct pet_scop *scop;
1436 yaml_node_pair_t * pair;
1438 if (!node)
1439 return NULL;
1441 if (node->type != YAML_MAPPING_NODE)
1442 isl_die(ctx, isl_error_invalid, "expecting mapping",
1443 return NULL);
1445 scop = pet_scop_alloc(ctx);
1446 if (!scop)
1447 return NULL;
1449 for (pair = node->data.mapping.pairs.start;
1450 pair < node->data.mapping.pairs.top; ++pair) {
1451 yaml_node_t *key, *value;
1453 key = yaml_document_get_node(document, pair->key);
1454 value = yaml_document_get_node(document, pair->value);
1456 if (key->type != YAML_SCALAR_NODE)
1457 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1458 return pet_scop_free(scop));
1459 if (!strcmp((char *) key->data.scalar.value, "context"))
1460 scop->context = extract_set(ctx, document, value);
1461 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1462 scop->context_value = extract_set(ctx, document, value);
1463 if (!strcmp((char *) key->data.scalar.value, "types"))
1464 scop = extract_types(ctx, document, value, scop);
1465 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1466 scop = extract_arrays(ctx, document, value, scop);
1467 if (!strcmp((char *) key->data.scalar.value, "statements"))
1468 scop = extract_statements(ctx, document, value, scop);
1469 if (!strcmp((char *) key->data.scalar.value, "implications"))
1470 scop = extract_implications(ctx, document, value, scop);
1471 if (!strcmp((char *) key->data.scalar.value, "independences"))
1472 scop = extract_independences(ctx,
1473 document, value, scop);
1474 if (!scop)
1475 return NULL;
1478 if (!scop->context_value) {
1479 isl_space *space = isl_space_params_alloc(ctx, 0);
1480 scop->context_value = isl_set_universe(space);
1481 if (!scop->context_value)
1482 return pet_scop_free(scop);
1485 return scop;
1488 /* Extract a pet_scop from the YAML description in "in".
1490 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1492 struct pet_scop *scop = NULL;
1493 yaml_parser_t parser;
1494 yaml_node_t *root;
1495 yaml_document_t document = { 0 };
1497 yaml_parser_initialize(&parser);
1499 yaml_parser_set_input_file(&parser, in);
1501 if (!yaml_parser_load(&parser, &document))
1502 goto error;
1504 root = yaml_document_get_root_node(&document);
1506 scop = extract_scop(ctx, &document, root);
1508 yaml_document_delete(&document);
1510 yaml_parser_delete(&parser);
1512 return scop;
1513 error:
1514 yaml_parser_delete(&parser);
1515 pet_scop_free(scop);
1516 return NULL;