pet_expr: document the read and write fields
[pet.git] / parse.c
blob444f7fafedf515399cb2baa6e12f357a7417d621
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));
449 return expr;
452 /* Extract operation expression specific fields from "node" and
453 * update "expr" accordingly.
455 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
456 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
458 yaml_node_pair_t *pair;
460 for (pair = node->data.mapping.pairs.start;
461 pair < node->data.mapping.pairs.top; ++pair) {
462 yaml_node_t *key, *value;
464 key = yaml_document_get_node(document, pair->key);
465 value = yaml_document_get_node(document, pair->value);
467 if (key->type != YAML_SCALAR_NODE)
468 isl_die(ctx, isl_error_invalid, "expecting scalar key",
469 return pet_expr_free(expr));
471 if (!strcmp((char *) key->data.scalar.value, "operation"))
472 expr = pet_expr_op_set_type(expr,
473 extract_op(ctx, document, value));
476 return expr;
479 /* Extract pet_expr_call specific fields from "node" and
480 * update "expr" accordingly.
482 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
483 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
485 yaml_node_pair_t *pair;
487 for (pair = node->data.mapping.pairs.start;
488 pair < node->data.mapping.pairs.top; ++pair) {
489 yaml_node_t *key, *value;
491 key = yaml_document_get_node(document, pair->key);
492 value = yaml_document_get_node(document, pair->value);
494 if (key->type != YAML_SCALAR_NODE)
495 isl_die(ctx, isl_error_invalid, "expecting scalar key",
496 return pet_expr_free(expr));
498 if (!strcmp((char *) key->data.scalar.value, "name"))
499 expr = pet_expr_call_set_name(expr,
500 extract_string(ctx, document, value));
503 return expr;
506 /* Extract pet_expr_cast specific fields from "node" and
507 * update "expr" accordingly.
509 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
510 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
512 yaml_node_pair_t *pair;
514 for (pair = node->data.mapping.pairs.start;
515 pair < node->data.mapping.pairs.top; ++pair) {
516 yaml_node_t *key, *value;
518 key = yaml_document_get_node(document, pair->key);
519 value = yaml_document_get_node(document, pair->value);
521 if (key->type != YAML_SCALAR_NODE)
522 isl_die(ctx, isl_error_invalid, "expecting scalar key",
523 return pet_expr_free(expr));
525 if (!strcmp((char *) key->data.scalar.value, "type_name"))
526 expr = pet_expr_cast_set_type_name(expr,
527 extract_string(ctx, document, value));
530 return expr;
533 /* Extract pet_expr_int specific fields from "node" and
534 * update "expr" accordingly.
536 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
537 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
539 yaml_node_pair_t * pair;
541 for (pair = node->data.mapping.pairs.start;
542 pair < node->data.mapping.pairs.top; ++pair) {
543 yaml_node_t *key, *value;
545 key = yaml_document_get_node(document, pair->key);
546 value = yaml_document_get_node(document, pair->value);
548 if (key->type != YAML_SCALAR_NODE)
549 isl_die(ctx, isl_error_invalid, "expecting scalar key",
550 return pet_expr_free(expr));
552 if (!strcmp((char *) key->data.scalar.value, "value"))
553 expr = pet_expr_int_set_val(expr,
554 extract_val(ctx, document, value));
557 return expr;
560 /* Extract a pet_expr from "node".
562 * We first extract the type and arguments of the expression and
563 * then extract additional fields depending on the type.
565 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
566 yaml_document_t *document, yaml_node_t *node)
568 enum pet_expr_type type = pet_expr_error;
569 pet_expr *expr;
570 yaml_node_pair_t *pair;
572 if (node->type != YAML_MAPPING_NODE)
573 isl_die(ctx, isl_error_invalid, "expecting mapping",
574 return NULL);
576 for (pair = node->data.mapping.pairs.start;
577 pair < node->data.mapping.pairs.top; ++pair) {
578 yaml_node_t *key, *value;
580 key = yaml_document_get_node(document, pair->key);
581 value = yaml_document_get_node(document, pair->value);
583 if (key->type != YAML_SCALAR_NODE)
584 isl_die(ctx, isl_error_invalid, "expecting scalar key",
585 return pet_expr_free(expr));
587 if (!strcmp((char *) key->data.scalar.value, "type"))
588 type = extract_expr_type(ctx, document, value);
591 if (type == pet_expr_error)
592 isl_die(ctx, isl_error_invalid, "cannot determine type",
593 return NULL);
595 expr = pet_expr_alloc(ctx, type);
596 if (!expr)
597 return NULL;
599 for (pair = node->data.mapping.pairs.start;
600 pair < node->data.mapping.pairs.top; ++pair) {
601 yaml_node_t *key, *value;
603 key = yaml_document_get_node(document, pair->key);
604 value = yaml_document_get_node(document, pair->value);
606 if (!strcmp((char *) key->data.scalar.value, "arguments"))
607 expr = extract_arguments(ctx, document, value, expr);
608 if (!expr)
609 return NULL;
612 switch (type) {
613 case pet_expr_error:
614 isl_die(ctx, isl_error_internal, "unreachable code",
615 return NULL);
616 case pet_expr_access:
617 expr = extract_expr_access(ctx, document, node, expr);
618 break;
619 case pet_expr_double:
620 expr = extract_expr_double(ctx, document, node, expr);
621 break;
622 case pet_expr_call:
623 expr = extract_expr_call(ctx, document, node, expr);
624 break;
625 case pet_expr_cast:
626 expr = extract_expr_cast(ctx, document, node, expr);
627 break;
628 case pet_expr_int:
629 expr = extract_expr_int(ctx, document, node, expr);
630 break;
631 case pet_expr_op:
632 expr = extract_expr_op(ctx, document, node, expr);
633 break;
636 return expr;
639 /* Extract a pet_tree_type from "node".
641 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
642 yaml_document_t *document, yaml_node_t *node)
644 if (node->type != YAML_SCALAR_NODE)
645 isl_die(ctx, isl_error_invalid, "expecting scalar node",
646 return -1);
648 return pet_tree_str_type((char *) node->data.scalar.value);
651 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
652 yaml_document_t *document, yaml_node_t *node);
654 /* Extract a pet_tree of type pet_tree_block from "node".
656 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
657 yaml_document_t *document, yaml_node_t *node)
659 int block = 0;
660 int i, n;
661 yaml_node_pair_t *pair;
662 yaml_node_item_t *item;
663 yaml_node_t *children = NULL;
664 pet_tree *tree;
666 for (pair = node->data.mapping.pairs.start;
667 pair < node->data.mapping.pairs.top; ++pair) {
668 yaml_node_t *key, *value;
670 key = yaml_document_get_node(document, pair->key);
671 value = yaml_document_get_node(document, pair->value);
673 if (key->type != YAML_SCALAR_NODE)
674 isl_die(ctx, isl_error_invalid, "expecting scalar key",
675 return NULL);
677 if (!strcmp((char *) key->data.scalar.value, "block"))
678 block = extract_int(ctx, document, value);
679 if (!strcmp((char *) key->data.scalar.value, "children"))
680 children = value;
683 if (!children)
684 n = 0;
685 else
686 n = children->data.sequence.items.top -
687 children->data.sequence.items.start;
689 tree = pet_tree_new_block(ctx, block, n);
690 if (!children)
691 return tree;
693 for (item = children->data.sequence.items.start, i = 0;
694 item < children->data.sequence.items.top; ++item, ++i) {
695 yaml_node_t *n;
696 pet_tree *child;
698 n = yaml_document_get_node(document, *item);
699 child = extract_tree(ctx, document, n);
700 tree = pet_tree_block_add_child(tree, child);
703 return tree;
706 /* Extract a pet_tree of type pet_tree_decl from "node".
708 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
709 yaml_document_t *document, yaml_node_t *node)
711 yaml_node_pair_t *pair;
712 pet_expr *var = NULL;
714 for (pair = node->data.mapping.pairs.start;
715 pair < node->data.mapping.pairs.top; ++pair) {
716 yaml_node_t *key, *value;
718 key = yaml_document_get_node(document, pair->key);
719 value = yaml_document_get_node(document, pair->value);
721 if (key->type != YAML_SCALAR_NODE)
722 isl_die(ctx, isl_error_invalid, "expecting scalar key",
723 return NULL);
725 if (!strcmp((char *) key->data.scalar.value, "variable")) {
726 var = extract_expr(ctx, document, value);
727 if (!var)
728 return NULL;
732 if (!var)
733 isl_die(ctx, isl_error_invalid,
734 "no variable field", return NULL);
736 return pet_tree_new_decl(var);
739 /* Extract a pet_tree of type pet_tree_decl_init from "node".
741 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
742 yaml_document_t *document, yaml_node_t *node)
744 yaml_node_pair_t *pair;
745 pet_expr *var = NULL;
746 pet_expr *init = NULL;
748 for (pair = node->data.mapping.pairs.start;
749 pair < node->data.mapping.pairs.top; ++pair) {
750 yaml_node_t *key, *value;
752 key = yaml_document_get_node(document, pair->key);
753 value = yaml_document_get_node(document, pair->value);
755 if (key->type != YAML_SCALAR_NODE)
756 isl_die(ctx, isl_error_invalid, "expecting scalar key",
757 return NULL);
759 if (!strcmp((char *) key->data.scalar.value, "variable")) {
760 var = extract_expr(ctx, document, value);
761 if (!var)
762 goto error;
764 if (!strcmp((char *) key->data.scalar.value,
765 "initialization")) {
766 init = extract_expr(ctx, document, value);
767 if (!init)
768 goto error;
772 if (!var)
773 isl_die(ctx, isl_error_invalid,
774 "no variable field", goto error);
775 if (!init)
776 isl_die(ctx, isl_error_invalid,
777 "no initialization field", goto error);
779 return pet_tree_new_decl_init(var, init);
780 error:
781 pet_expr_free(var);
782 pet_expr_free(init);
783 return NULL;
786 /* Extract a pet_tree of type pet_tree_expr from "node".
788 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
789 yaml_document_t *document, yaml_node_t *node)
791 yaml_node_pair_t *pair;
792 pet_expr *expr = NULL;
794 for (pair = node->data.mapping.pairs.start;
795 pair < node->data.mapping.pairs.top; ++pair) {
796 yaml_node_t *key, *value;
798 key = yaml_document_get_node(document, pair->key);
799 value = yaml_document_get_node(document, pair->value);
801 if (key->type != YAML_SCALAR_NODE)
802 isl_die(ctx, isl_error_invalid, "expecting scalar key",
803 return NULL);
805 if (!strcmp((char *) key->data.scalar.value, "expr")) {
806 expr = extract_expr(ctx, document, value);
807 if (!expr)
808 return NULL;
812 if (!expr)
813 isl_die(ctx, isl_error_invalid,
814 "no expr field", return NULL);
816 return pet_tree_new_expr(expr);
819 /* Extract a pet_tree of type pet_tree_while from "node".
821 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
822 yaml_document_t *document, yaml_node_t *node)
824 yaml_node_pair_t *pair;
825 pet_expr *cond = NULL;
826 pet_tree *body = NULL;
828 for (pair = node->data.mapping.pairs.start;
829 pair < node->data.mapping.pairs.top; ++pair) {
830 yaml_node_t *key, *value;
832 key = yaml_document_get_node(document, pair->key);
833 value = yaml_document_get_node(document, pair->value);
835 if (key->type != YAML_SCALAR_NODE)
836 isl_die(ctx, isl_error_invalid, "expecting scalar key",
837 return NULL);
839 if (!strcmp((char *) key->data.scalar.value, "condition")) {
840 cond = extract_expr(ctx, document, value);
841 if (!cond)
842 goto error;
844 if (!strcmp((char *) key->data.scalar.value, "body")) {
845 body = extract_tree(ctx, document, value);
846 if (!body)
847 goto error;
851 if (!cond)
852 isl_die(ctx, isl_error_invalid,
853 "no condition field", goto error);
854 if (!body)
855 isl_die(ctx, isl_error_invalid,
856 "no body field", goto error);
858 return pet_tree_new_while(cond, body);
859 error:
860 pet_expr_free(cond);
861 pet_tree_free(body);
862 return NULL;
865 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
867 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
868 yaml_document_t *document, yaml_node_t *node)
870 yaml_node_pair_t *pair;
871 pet_tree *body;
873 for (pair = node->data.mapping.pairs.start;
874 pair < node->data.mapping.pairs.top; ++pair) {
875 yaml_node_t *key, *value;
877 key = yaml_document_get_node(document, pair->key);
878 value = yaml_document_get_node(document, pair->value);
880 if (key->type != YAML_SCALAR_NODE)
881 isl_die(ctx, isl_error_invalid, "expecting scalar key",
882 return NULL);
884 if (!strcmp((char *) key->data.scalar.value, "body")) {
885 body = extract_tree(ctx, document, value);
886 if (!body)
887 return NULL;
891 if (!body)
892 isl_die(ctx, isl_error_invalid,
893 "no body field", return NULL);
895 return pet_tree_new_infinite_loop(body);
898 /* Extract a pet_tree of type pet_tree_if from "node".
900 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
901 yaml_document_t *document, yaml_node_t *node)
903 yaml_node_pair_t *pair;
904 pet_expr *cond = NULL;
905 pet_tree *then_body = NULL;
907 for (pair = node->data.mapping.pairs.start;
908 pair < node->data.mapping.pairs.top; ++pair) {
909 yaml_node_t *key, *value;
911 key = yaml_document_get_node(document, pair->key);
912 value = yaml_document_get_node(document, pair->value);
914 if (key->type != YAML_SCALAR_NODE)
915 isl_die(ctx, isl_error_invalid, "expecting scalar key",
916 return NULL);
918 if (!strcmp((char *) key->data.scalar.value, "condition")) {
919 cond = extract_expr(ctx, document, value);
920 if (!cond)
921 goto error;
923 if (!strcmp((char *) key->data.scalar.value, "then")) {
924 then_body = extract_tree(ctx, document, value);
925 if (!then_body)
926 goto error;
930 if (!cond)
931 isl_die(ctx, isl_error_invalid,
932 "no condition field", goto error);
933 if (!then_body)
934 isl_die(ctx, isl_error_invalid,
935 "no then body", goto error);
937 return pet_tree_new_if(cond, then_body);
938 error:
939 pet_expr_free(cond);
940 pet_tree_free(then_body);
941 return NULL;
944 /* Extract a pet_tree of type pet_tree_if_else from "node".
946 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
947 yaml_document_t *document, yaml_node_t *node)
949 yaml_node_pair_t *pair;
950 pet_expr *cond = NULL;
951 pet_tree *then_body = NULL;
952 pet_tree *else_body = NULL;
954 for (pair = node->data.mapping.pairs.start;
955 pair < node->data.mapping.pairs.top; ++pair) {
956 yaml_node_t *key, *value;
958 key = yaml_document_get_node(document, pair->key);
959 value = yaml_document_get_node(document, pair->value);
961 if (key->type != YAML_SCALAR_NODE)
962 isl_die(ctx, isl_error_invalid, "expecting scalar key",
963 return NULL);
965 if (!strcmp((char *) key->data.scalar.value, "condition")) {
966 cond = extract_expr(ctx, document, value);
967 if (!cond)
968 goto error;
970 if (!strcmp((char *) key->data.scalar.value, "then")) {
971 then_body = extract_tree(ctx, document, value);
972 if (!then_body)
973 goto error;
975 if (!strcmp((char *) key->data.scalar.value, "else")) {
976 else_body = extract_tree(ctx, document, value);
977 if (!else_body)
978 goto error;
982 if (!cond)
983 isl_die(ctx, isl_error_invalid,
984 "no condition field", goto error);
985 if (!then_body)
986 isl_die(ctx, isl_error_invalid,
987 "no then body", goto error);
988 if (!else_body)
989 isl_die(ctx, isl_error_invalid,
990 "no else body", goto error);
992 return pet_tree_new_if_else(cond, then_body, else_body);
993 error:
994 pet_expr_free(cond);
995 pet_tree_free(then_body);
996 pet_tree_free(else_body);
997 return NULL;
1000 /* Extract a pet_tree of type pet_tree_for from "node".
1002 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1003 yaml_document_t *document, yaml_node_t *node)
1005 yaml_node_pair_t *pair;
1006 int declared = 0;
1007 int independent = 0;
1008 pet_expr *iv = NULL;
1009 pet_expr *init = NULL;
1010 pet_expr *cond = NULL;
1011 pet_expr *inc = NULL;
1012 pet_tree *body = NULL;
1014 for (pair = node->data.mapping.pairs.start;
1015 pair < node->data.mapping.pairs.top; ++pair) {
1016 yaml_node_t *key, *value;
1018 key = yaml_document_get_node(document, pair->key);
1019 value = yaml_document_get_node(document, pair->value);
1021 if (key->type != YAML_SCALAR_NODE)
1022 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1023 return NULL);
1025 if (!strcmp((char *) key->data.scalar.value, "declared"))
1026 declared = extract_int(ctx, document, value);
1027 if (!strcmp((char *) key->data.scalar.value, "independent"))
1028 independent = extract_int(ctx, document, value);
1029 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1030 iv = extract_expr(ctx, document, value);
1031 if (!iv)
1032 goto error;
1034 if (!strcmp((char *) key->data.scalar.value,
1035 "initialization")) {
1036 init = extract_expr(ctx, document, value);
1037 if (!init)
1038 goto error;
1040 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1041 cond = extract_expr(ctx, document, value);
1042 if (!cond)
1043 goto error;
1045 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1046 inc = extract_expr(ctx, document, value);
1047 if (!inc)
1048 goto error;
1050 if (!strcmp((char *) key->data.scalar.value, "body")) {
1051 body = extract_tree(ctx, document, value);
1052 if (!body)
1053 goto error;
1057 if (!iv)
1058 isl_die(ctx, isl_error_invalid,
1059 "no variable field", goto error);
1060 if (!init)
1061 isl_die(ctx, isl_error_invalid,
1062 "no initialization field", goto error);
1063 if (!cond)
1064 isl_die(ctx, isl_error_invalid,
1065 "no condition field", goto error);
1066 if (!inc)
1067 isl_die(ctx, isl_error_invalid,
1068 "no increment field", goto error);
1069 if (!body)
1070 isl_die(ctx, isl_error_invalid,
1071 "no body field", goto error);
1073 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1074 body);
1075 error:
1076 pet_expr_free(iv);
1077 pet_expr_free(init);
1078 pet_expr_free(cond);
1079 pet_expr_free(inc);
1080 pet_tree_free(body);
1081 return NULL;
1084 /* Extract a pet_tree from "node".
1086 * We first extract the type of the pet_tree and then call
1087 * the appropriate function to extract and construct a pet_tree
1088 * of that type.
1090 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1091 yaml_document_t *document, yaml_node_t *node)
1093 enum pet_tree_type type = pet_tree_error;
1094 pet_tree *tree;
1095 yaml_node_pair_t *pair;
1097 if (node->type != YAML_MAPPING_NODE)
1098 isl_die(ctx, isl_error_invalid, "expecting mapping",
1099 return NULL);
1101 for (pair = node->data.mapping.pairs.start;
1102 pair < node->data.mapping.pairs.top; ++pair) {
1103 yaml_node_t *key, *value;
1105 key = yaml_document_get_node(document, pair->key);
1106 value = yaml_document_get_node(document, pair->value);
1108 if (key->type != YAML_SCALAR_NODE)
1109 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1110 return pet_tree_free(tree));
1112 if (!strcmp((char *) key->data.scalar.value, "type"))
1113 type = extract_tree_type(ctx, document, value);
1116 if (type == pet_tree_error)
1117 isl_die(ctx, isl_error_invalid, "cannot determine type",
1118 return NULL);
1120 switch (type) {
1121 case pet_tree_error:
1122 return NULL;
1123 case pet_tree_block:
1124 tree = extract_tree_block(ctx, document, node);
1125 break;
1126 case pet_tree_break:
1127 tree = pet_tree_new_break(ctx);
1128 break;
1129 case pet_tree_continue:
1130 tree = pet_tree_new_continue(ctx);
1131 break;
1132 case pet_tree_decl:
1133 tree = extract_tree_decl(ctx, document, node);
1134 break;
1135 case pet_tree_decl_init:
1136 tree = extract_tree_decl_init(ctx, document, node);
1137 break;
1138 case pet_tree_expr:
1139 tree = extract_tree_expr(ctx, document, node);
1140 break;
1141 case pet_tree_for:
1142 tree = extract_tree_for(ctx, document, node);
1143 break;
1144 case pet_tree_while:
1145 tree = extract_tree_while(ctx, document, node);
1146 break;
1147 case pet_tree_infinite_loop:
1148 tree = extract_tree_infinite_loop(ctx, document, node);
1149 break;
1150 case pet_tree_if:
1151 tree = extract_tree_if(ctx, document, node);
1152 break;
1153 case pet_tree_if_else:
1154 tree = extract_tree_if_else(ctx, document, node);
1155 break;
1158 return tree;
1161 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1162 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1164 int i;
1165 yaml_node_item_t *item;
1167 if (node->type != YAML_SEQUENCE_NODE)
1168 isl_die(ctx, isl_error_invalid, "expecting sequence",
1169 return pet_stmt_free(stmt));
1171 stmt->n_arg = node->data.sequence.items.top
1172 - node->data.sequence.items.start;
1173 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1174 if (!stmt->args)
1175 return pet_stmt_free(stmt);
1177 for (item = node->data.sequence.items.start, i = 0;
1178 item < node->data.sequence.items.top; ++item, ++i) {
1179 yaml_node_t *n;
1181 n = yaml_document_get_node(document, *item);
1182 stmt->args[i] = extract_expr(ctx, document, n);
1183 if (!stmt->args[i])
1184 return pet_stmt_free(stmt);
1187 return stmt;
1190 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1191 yaml_node_t *node)
1193 struct pet_stmt *stmt;
1194 yaml_node_pair_t * pair;
1195 int line = -1;
1196 unsigned start = 0, end = 0;
1197 char *indent = NULL;
1199 if (node->type != YAML_MAPPING_NODE)
1200 isl_die(ctx, isl_error_invalid, "expecting mapping",
1201 return NULL);
1203 stmt = isl_calloc_type(ctx, struct pet_stmt);
1204 if (!stmt)
1205 return NULL;
1207 stmt->loc = &pet_loc_dummy;
1209 for (pair = node->data.mapping.pairs.start;
1210 pair < node->data.mapping.pairs.top; ++pair) {
1211 yaml_node_t *key, *value;
1213 key = yaml_document_get_node(document, pair->key);
1214 value = yaml_document_get_node(document, pair->value);
1216 if (key->type != YAML_SCALAR_NODE)
1217 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1218 return pet_stmt_free(stmt));
1220 if (!strcmp((char *) key->data.scalar.value, "indent"))
1221 indent = extract_string(ctx, document, value);
1222 if (!strcmp((char *) key->data.scalar.value, "line"))
1223 line = extract_int(ctx, document, value);
1224 if (!strcmp((char *) key->data.scalar.value, "start"))
1225 start = extract_int(ctx, document, value);
1226 if (!strcmp((char *) key->data.scalar.value, "end"))
1227 end = extract_int(ctx, document, value);
1228 if (!strcmp((char *) key->data.scalar.value, "domain"))
1229 stmt->domain = extract_set(ctx, document, value);
1230 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1231 stmt->schedule = extract_map(ctx, document, value);
1232 if (!strcmp((char *) key->data.scalar.value, "body"))
1233 stmt->body = extract_tree(ctx, document, value);
1235 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1236 stmt = extract_stmt_arguments(ctx, document,
1237 value, stmt);
1238 if (!stmt)
1239 return NULL;
1242 if (!indent)
1243 indent = strdup("");
1244 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1245 if (!stmt->loc)
1246 return pet_stmt_free(stmt);
1248 return stmt;
1251 static struct pet_scop *extract_statements(isl_ctx *ctx,
1252 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1254 int i;
1255 yaml_node_item_t *item;
1257 if (node->type != YAML_SEQUENCE_NODE)
1258 isl_die(ctx, isl_error_invalid, "expecting sequence",
1259 return NULL);
1261 scop->n_stmt = node->data.sequence.items.top
1262 - node->data.sequence.items.start;
1263 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1264 if (!scop->stmts)
1265 return pet_scop_free(scop);
1267 for (item = node->data.sequence.items.start, i = 0;
1268 item < node->data.sequence.items.top; ++item, ++i) {
1269 yaml_node_t *n;
1271 n = yaml_document_get_node(document, *item);
1272 scop->stmts[i] = extract_stmt(ctx, document, n);
1273 if (!scop->stmts[i])
1274 return pet_scop_free(scop);
1277 return scop;
1280 /* Extract a pet_implication from "node".
1282 static struct pet_implication *extract_implication(isl_ctx *ctx,
1283 yaml_document_t *document, yaml_node_t *node)
1285 struct pet_implication *implication;
1286 yaml_node_pair_t * pair;
1288 if (node->type != YAML_MAPPING_NODE)
1289 isl_die(ctx, isl_error_invalid, "expecting mapping",
1290 return NULL);
1292 implication = isl_calloc_type(ctx, struct pet_implication);
1293 if (!implication)
1294 return NULL;
1296 for (pair = node->data.mapping.pairs.start;
1297 pair < node->data.mapping.pairs.top; ++pair) {
1298 yaml_node_t *key, *value;
1300 key = yaml_document_get_node(document, pair->key);
1301 value = yaml_document_get_node(document, pair->value);
1303 if (key->type != YAML_SCALAR_NODE)
1304 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1305 return pet_implication_free(implication));
1307 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1308 implication->satisfied =
1309 extract_int(ctx, document, value);
1310 if (!strcmp((char *) key->data.scalar.value, "extension"))
1311 implication->extension =
1312 extract_map(ctx, document, value);
1315 return implication;
1318 /* Extract a sequence of implications from "node" and
1319 * store them in scop->implications.
1321 static struct pet_scop *extract_implications(isl_ctx *ctx,
1322 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1324 int i;
1325 yaml_node_item_t *item;
1327 if (node->type != YAML_SEQUENCE_NODE)
1328 isl_die(ctx, isl_error_invalid, "expecting sequence",
1329 return NULL);
1331 scop->n_implication = node->data.sequence.items.top
1332 - node->data.sequence.items.start;
1333 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1334 scop->n_implication);
1335 if (!scop->implications)
1336 return pet_scop_free(scop);
1338 for (item = node->data.sequence.items.start, i = 0;
1339 item < node->data.sequence.items.top; ++item, ++i) {
1340 yaml_node_t *n;
1342 n = yaml_document_get_node(document, *item);
1343 scop->implications[i] = extract_implication(ctx, document, n);
1344 if (!scop->implications[i])
1345 return pet_scop_free(scop);
1348 return scop;
1351 /* Extract a pet_independence from "node".
1353 static struct pet_independence *extract_independence(isl_ctx *ctx,
1354 yaml_document_t *document, yaml_node_t *node)
1356 struct pet_independence *independence;
1357 yaml_node_pair_t * pair;
1359 if (node->type != YAML_MAPPING_NODE)
1360 isl_die(ctx, isl_error_invalid, "expecting mapping",
1361 return NULL);
1363 independence = isl_calloc_type(ctx, struct pet_independence);
1364 if (!independence)
1365 return NULL;
1367 for (pair = node->data.mapping.pairs.start;
1368 pair < node->data.mapping.pairs.top; ++pair) {
1369 yaml_node_t *key, *value;
1371 key = yaml_document_get_node(document, pair->key);
1372 value = yaml_document_get_node(document, pair->value);
1374 if (key->type != YAML_SCALAR_NODE)
1375 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1376 return pet_independence_free(independence));
1378 if (!strcmp((char *) key->data.scalar.value, "filter"))
1379 independence->filter =
1380 extract_union_map(ctx, document, value);
1381 if (!strcmp((char *) key->data.scalar.value, "local"))
1382 independence->local =
1383 extract_union_set(ctx, document, value);
1386 if (!independence->filter)
1387 isl_die(ctx, isl_error_invalid, "no filter field",
1388 return pet_independence_free(independence));
1389 if (!independence->local)
1390 isl_die(ctx, isl_error_invalid, "no local field",
1391 return pet_independence_free(independence));
1393 return independence;
1396 /* Extract a sequence of independences from "node" and
1397 * store them in scop->independences.
1399 static struct pet_scop *extract_independences(isl_ctx *ctx,
1400 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1402 int i;
1403 yaml_node_item_t *item;
1405 if (node->type != YAML_SEQUENCE_NODE)
1406 isl_die(ctx, isl_error_invalid, "expecting sequence",
1407 return NULL);
1409 scop->n_independence = node->data.sequence.items.top
1410 - node->data.sequence.items.start;
1411 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1412 scop->n_independence);
1413 if (!scop->independences)
1414 return pet_scop_free(scop);
1416 for (item = node->data.sequence.items.start, i = 0;
1417 item < node->data.sequence.items.top; ++item, ++i) {
1418 yaml_node_t *n;
1420 n = yaml_document_get_node(document, *item);
1421 scop->independences[i] = extract_independence(ctx, document, n);
1422 if (!scop->independences[i])
1423 return pet_scop_free(scop);
1426 return scop;
1429 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1430 yaml_node_t *node)
1432 struct pet_scop *scop;
1433 yaml_node_pair_t * pair;
1435 if (!node)
1436 return NULL;
1438 if (node->type != YAML_MAPPING_NODE)
1439 isl_die(ctx, isl_error_invalid, "expecting mapping",
1440 return NULL);
1442 scop = pet_scop_alloc(ctx);
1443 if (!scop)
1444 return NULL;
1446 for (pair = node->data.mapping.pairs.start;
1447 pair < node->data.mapping.pairs.top; ++pair) {
1448 yaml_node_t *key, *value;
1450 key = yaml_document_get_node(document, pair->key);
1451 value = yaml_document_get_node(document, pair->value);
1453 if (key->type != YAML_SCALAR_NODE)
1454 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1455 return pet_scop_free(scop));
1456 if (!strcmp((char *) key->data.scalar.value, "context"))
1457 scop->context = extract_set(ctx, document, value);
1458 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1459 scop->context_value = extract_set(ctx, document, value);
1460 if (!strcmp((char *) key->data.scalar.value, "types"))
1461 scop = extract_types(ctx, document, value, scop);
1462 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1463 scop = extract_arrays(ctx, document, value, scop);
1464 if (!strcmp((char *) key->data.scalar.value, "statements"))
1465 scop = extract_statements(ctx, document, value, scop);
1466 if (!strcmp((char *) key->data.scalar.value, "implications"))
1467 scop = extract_implications(ctx, document, value, scop);
1468 if (!strcmp((char *) key->data.scalar.value, "independences"))
1469 scop = extract_independences(ctx,
1470 document, value, scop);
1471 if (!scop)
1472 return NULL;
1475 if (!scop->context_value) {
1476 isl_space *space = isl_space_params_alloc(ctx, 0);
1477 scop->context_value = isl_set_universe(space);
1478 if (!scop->context_value)
1479 return pet_scop_free(scop);
1482 return scop;
1485 /* Extract a pet_scop from the YAML description in "in".
1487 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1489 struct pet_scop *scop = NULL;
1490 yaml_parser_t parser;
1491 yaml_node_t *root;
1492 yaml_document_t document = { 0 };
1494 yaml_parser_initialize(&parser);
1496 yaml_parser_set_input_file(&parser, in);
1498 if (!yaml_parser_load(&parser, &document))
1499 goto error;
1501 root = yaml_document_get_root_node(&document);
1503 scop = extract_scop(ctx, &document, root);
1505 yaml_document_delete(&document);
1507 yaml_parser_delete(&parser);
1509 return scop;
1510 error:
1511 yaml_parser_delete(&parser);
1512 pet_scop_free(scop);
1513 return NULL;