pet_stmt_build_ast_exprs: ignore expression arguments not appearing in index
[pet.git] / parse.c
blob200b6ec2753728563f5b7ef7be0a220a0db12695
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 relations (if any)
394 * need 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, "may_read"))
436 expr = pet_expr_access_set_access(expr,
437 pet_expr_access_may_read,
438 extract_union_map(ctx, document, value));
439 if (!strcmp((char *) key->data.scalar.value, "may_write"))
440 expr = pet_expr_access_set_access(expr,
441 pet_expr_access_may_write,
442 extract_union_map(ctx, document, value));
443 if (!strcmp((char *) key->data.scalar.value, "must_write"))
444 expr = pet_expr_access_set_access(expr,
445 pet_expr_access_must_write,
446 extract_union_map(ctx, document, value));
447 if (!strcmp((char *) key->data.scalar.value, "killed"))
448 expr = pet_expr_access_set_access(expr,
449 pet_expr_access_killed,
450 extract_union_map(ctx, document, value));
451 if (!strcmp((char *) key->data.scalar.value, "reference"))
452 expr = pet_expr_access_set_ref_id(expr,
453 extract_id(ctx, document, value));
454 if (!strcmp((char *) key->data.scalar.value, "read"))
455 expr = pet_expr_access_set_read(expr,
456 extract_int(ctx, document, value));
457 if (!strcmp((char *) key->data.scalar.value, "write"))
458 expr = pet_expr_access_set_write(expr,
459 extract_int(ctx, document, value));
460 if (!strcmp((char *) key->data.scalar.value, "kill"))
461 expr = pet_expr_access_set_kill(expr,
462 extract_int(ctx, document, value));
465 return expr;
468 /* Extract operation expression specific fields from "node" and
469 * update "expr" accordingly.
471 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
472 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
474 yaml_node_pair_t *pair;
476 for (pair = node->data.mapping.pairs.start;
477 pair < node->data.mapping.pairs.top; ++pair) {
478 yaml_node_t *key, *value;
480 key = yaml_document_get_node(document, pair->key);
481 value = yaml_document_get_node(document, pair->value);
483 if (key->type != YAML_SCALAR_NODE)
484 isl_die(ctx, isl_error_invalid, "expecting scalar key",
485 return pet_expr_free(expr));
487 if (!strcmp((char *) key->data.scalar.value, "operation"))
488 expr = pet_expr_op_set_type(expr,
489 extract_op(ctx, document, value));
492 return expr;
495 /* Extract pet_expr_call specific fields from "node" and
496 * update "expr" accordingly.
498 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
499 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
501 yaml_node_pair_t *pair;
503 for (pair = node->data.mapping.pairs.start;
504 pair < node->data.mapping.pairs.top; ++pair) {
505 yaml_node_t *key, *value;
507 key = yaml_document_get_node(document, pair->key);
508 value = yaml_document_get_node(document, pair->value);
510 if (key->type != YAML_SCALAR_NODE)
511 isl_die(ctx, isl_error_invalid, "expecting scalar key",
512 return pet_expr_free(expr));
514 if (!strcmp((char *) key->data.scalar.value, "name"))
515 expr = pet_expr_call_set_name(expr,
516 extract_string(ctx, document, value));
519 return expr;
522 /* Extract pet_expr_cast specific fields from "node" and
523 * update "expr" accordingly.
525 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
526 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
528 yaml_node_pair_t *pair;
530 for (pair = node->data.mapping.pairs.start;
531 pair < node->data.mapping.pairs.top; ++pair) {
532 yaml_node_t *key, *value;
534 key = yaml_document_get_node(document, pair->key);
535 value = yaml_document_get_node(document, pair->value);
537 if (key->type != YAML_SCALAR_NODE)
538 isl_die(ctx, isl_error_invalid, "expecting scalar key",
539 return pet_expr_free(expr));
541 if (!strcmp((char *) key->data.scalar.value, "type_name"))
542 expr = pet_expr_cast_set_type_name(expr,
543 extract_string(ctx, document, value));
546 return expr;
549 /* Extract pet_expr_int specific fields from "node" and
550 * update "expr" accordingly.
552 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
553 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
555 yaml_node_pair_t * pair;
557 for (pair = node->data.mapping.pairs.start;
558 pair < node->data.mapping.pairs.top; ++pair) {
559 yaml_node_t *key, *value;
561 key = yaml_document_get_node(document, pair->key);
562 value = yaml_document_get_node(document, pair->value);
564 if (key->type != YAML_SCALAR_NODE)
565 isl_die(ctx, isl_error_invalid, "expecting scalar key",
566 return pet_expr_free(expr));
568 if (!strcmp((char *) key->data.scalar.value, "value"))
569 expr = pet_expr_int_set_val(expr,
570 extract_val(ctx, document, value));
573 return expr;
576 /* Extract a pet_expr from "node".
578 * We first extract the type and arguments of the expression and
579 * then extract additional fields depending on the type.
581 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
582 yaml_document_t *document, yaml_node_t *node)
584 enum pet_expr_type type = pet_expr_error;
585 pet_expr *expr;
586 yaml_node_pair_t *pair;
588 if (node->type != YAML_MAPPING_NODE)
589 isl_die(ctx, isl_error_invalid, "expecting mapping",
590 return NULL);
592 for (pair = node->data.mapping.pairs.start;
593 pair < node->data.mapping.pairs.top; ++pair) {
594 yaml_node_t *key, *value;
596 key = yaml_document_get_node(document, pair->key);
597 value = yaml_document_get_node(document, pair->value);
599 if (key->type != YAML_SCALAR_NODE)
600 isl_die(ctx, isl_error_invalid, "expecting scalar key",
601 return pet_expr_free(expr));
603 if (!strcmp((char *) key->data.scalar.value, "type"))
604 type = extract_expr_type(ctx, document, value);
607 if (type == pet_expr_error)
608 isl_die(ctx, isl_error_invalid, "cannot determine type",
609 return NULL);
611 expr = pet_expr_alloc(ctx, type);
612 if (!expr)
613 return NULL;
615 for (pair = node->data.mapping.pairs.start;
616 pair < node->data.mapping.pairs.top; ++pair) {
617 yaml_node_t *key, *value;
619 key = yaml_document_get_node(document, pair->key);
620 value = yaml_document_get_node(document, pair->value);
622 if (!strcmp((char *) key->data.scalar.value, "arguments"))
623 expr = extract_arguments(ctx, document, value, expr);
624 if (!expr)
625 return NULL;
628 switch (type) {
629 case pet_expr_error:
630 isl_die(ctx, isl_error_internal, "unreachable code",
631 return NULL);
632 case pet_expr_access:
633 expr = extract_expr_access(ctx, document, node, expr);
634 break;
635 case pet_expr_double:
636 expr = extract_expr_double(ctx, document, node, expr);
637 break;
638 case pet_expr_call:
639 expr = extract_expr_call(ctx, document, node, expr);
640 break;
641 case pet_expr_cast:
642 expr = extract_expr_cast(ctx, document, node, expr);
643 break;
644 case pet_expr_int:
645 expr = extract_expr_int(ctx, document, node, expr);
646 break;
647 case pet_expr_op:
648 expr = extract_expr_op(ctx, document, node, expr);
649 break;
652 return expr;
655 /* Extract a pet_tree_type from "node".
657 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
658 yaml_document_t *document, yaml_node_t *node)
660 if (node->type != YAML_SCALAR_NODE)
661 isl_die(ctx, isl_error_invalid, "expecting scalar node",
662 return -1);
664 return pet_tree_str_type((char *) node->data.scalar.value);
667 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
668 yaml_document_t *document, yaml_node_t *node);
670 /* Extract a pet_tree of type pet_tree_block from "node".
672 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
673 yaml_document_t *document, yaml_node_t *node)
675 int block = 0;
676 int i, n;
677 yaml_node_pair_t *pair;
678 yaml_node_item_t *item;
679 yaml_node_t *children = NULL;
680 pet_tree *tree;
682 for (pair = node->data.mapping.pairs.start;
683 pair < node->data.mapping.pairs.top; ++pair) {
684 yaml_node_t *key, *value;
686 key = yaml_document_get_node(document, pair->key);
687 value = yaml_document_get_node(document, pair->value);
689 if (key->type != YAML_SCALAR_NODE)
690 isl_die(ctx, isl_error_invalid, "expecting scalar key",
691 return NULL);
693 if (!strcmp((char *) key->data.scalar.value, "block"))
694 block = extract_int(ctx, document, value);
695 if (!strcmp((char *) key->data.scalar.value, "children"))
696 children = value;
699 if (!children)
700 n = 0;
701 else
702 n = children->data.sequence.items.top -
703 children->data.sequence.items.start;
705 tree = pet_tree_new_block(ctx, block, n);
706 if (!children)
707 return tree;
709 for (item = children->data.sequence.items.start, i = 0;
710 item < children->data.sequence.items.top; ++item, ++i) {
711 yaml_node_t *n;
712 pet_tree *child;
714 n = yaml_document_get_node(document, *item);
715 child = extract_tree(ctx, document, n);
716 tree = pet_tree_block_add_child(tree, child);
719 return tree;
722 /* Extract a pet_tree of type pet_tree_decl from "node".
724 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
725 yaml_document_t *document, yaml_node_t *node)
727 yaml_node_pair_t *pair;
728 pet_expr *var = NULL;
730 for (pair = node->data.mapping.pairs.start;
731 pair < node->data.mapping.pairs.top; ++pair) {
732 yaml_node_t *key, *value;
734 key = yaml_document_get_node(document, pair->key);
735 value = yaml_document_get_node(document, pair->value);
737 if (key->type != YAML_SCALAR_NODE)
738 isl_die(ctx, isl_error_invalid, "expecting scalar key",
739 return NULL);
741 if (!strcmp((char *) key->data.scalar.value, "variable")) {
742 var = extract_expr(ctx, document, value);
743 if (!var)
744 return NULL;
748 if (!var)
749 isl_die(ctx, isl_error_invalid,
750 "no variable field", return NULL);
752 return pet_tree_new_decl(var);
755 /* Extract a pet_tree of type pet_tree_decl_init from "node".
757 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
758 yaml_document_t *document, yaml_node_t *node)
760 yaml_node_pair_t *pair;
761 pet_expr *var = NULL;
762 pet_expr *init = NULL;
764 for (pair = node->data.mapping.pairs.start;
765 pair < node->data.mapping.pairs.top; ++pair) {
766 yaml_node_t *key, *value;
768 key = yaml_document_get_node(document, pair->key);
769 value = yaml_document_get_node(document, pair->value);
771 if (key->type != YAML_SCALAR_NODE)
772 isl_die(ctx, isl_error_invalid, "expecting scalar key",
773 return NULL);
775 if (!strcmp((char *) key->data.scalar.value, "variable")) {
776 var = extract_expr(ctx, document, value);
777 if (!var)
778 goto error;
780 if (!strcmp((char *) key->data.scalar.value,
781 "initialization")) {
782 init = extract_expr(ctx, document, value);
783 if (!init)
784 goto error;
788 if (!var)
789 isl_die(ctx, isl_error_invalid,
790 "no variable field", goto error);
791 if (!init)
792 isl_die(ctx, isl_error_invalid,
793 "no initialization field", goto error);
795 return pet_tree_new_decl_init(var, init);
796 error:
797 pet_expr_free(var);
798 pet_expr_free(init);
799 return NULL;
802 /* Extract a pet_tree of type pet_tree_expr from "node".
804 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
805 yaml_document_t *document, yaml_node_t *node)
807 yaml_node_pair_t *pair;
808 pet_expr *expr = NULL;
810 for (pair = node->data.mapping.pairs.start;
811 pair < node->data.mapping.pairs.top; ++pair) {
812 yaml_node_t *key, *value;
814 key = yaml_document_get_node(document, pair->key);
815 value = yaml_document_get_node(document, pair->value);
817 if (key->type != YAML_SCALAR_NODE)
818 isl_die(ctx, isl_error_invalid, "expecting scalar key",
819 return NULL);
821 if (!strcmp((char *) key->data.scalar.value, "expr")) {
822 expr = extract_expr(ctx, document, value);
823 if (!expr)
824 return NULL;
828 if (!expr)
829 isl_die(ctx, isl_error_invalid,
830 "no expr field", return NULL);
832 return pet_tree_new_expr(expr);
835 /* Extract a pet_tree of type pet_tree_while from "node".
837 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
838 yaml_document_t *document, yaml_node_t *node)
840 yaml_node_pair_t *pair;
841 pet_expr *cond = NULL;
842 pet_tree *body = NULL;
844 for (pair = node->data.mapping.pairs.start;
845 pair < node->data.mapping.pairs.top; ++pair) {
846 yaml_node_t *key, *value;
848 key = yaml_document_get_node(document, pair->key);
849 value = yaml_document_get_node(document, pair->value);
851 if (key->type != YAML_SCALAR_NODE)
852 isl_die(ctx, isl_error_invalid, "expecting scalar key",
853 return NULL);
855 if (!strcmp((char *) key->data.scalar.value, "condition")) {
856 cond = extract_expr(ctx, document, value);
857 if (!cond)
858 goto error;
860 if (!strcmp((char *) key->data.scalar.value, "body")) {
861 body = extract_tree(ctx, document, value);
862 if (!body)
863 goto error;
867 if (!cond)
868 isl_die(ctx, isl_error_invalid,
869 "no condition field", goto error);
870 if (!body)
871 isl_die(ctx, isl_error_invalid,
872 "no body field", goto error);
874 return pet_tree_new_while(cond, body);
875 error:
876 pet_expr_free(cond);
877 pet_tree_free(body);
878 return NULL;
881 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
883 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
884 yaml_document_t *document, yaml_node_t *node)
886 yaml_node_pair_t *pair;
887 pet_tree *body;
889 for (pair = node->data.mapping.pairs.start;
890 pair < node->data.mapping.pairs.top; ++pair) {
891 yaml_node_t *key, *value;
893 key = yaml_document_get_node(document, pair->key);
894 value = yaml_document_get_node(document, pair->value);
896 if (key->type != YAML_SCALAR_NODE)
897 isl_die(ctx, isl_error_invalid, "expecting scalar key",
898 return NULL);
900 if (!strcmp((char *) key->data.scalar.value, "body")) {
901 body = extract_tree(ctx, document, value);
902 if (!body)
903 return NULL;
907 if (!body)
908 isl_die(ctx, isl_error_invalid,
909 "no body field", return NULL);
911 return pet_tree_new_infinite_loop(body);
914 /* Extract a pet_tree of type pet_tree_if from "node".
916 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
917 yaml_document_t *document, yaml_node_t *node)
919 yaml_node_pair_t *pair;
920 pet_expr *cond = NULL;
921 pet_tree *then_body = NULL;
923 for (pair = node->data.mapping.pairs.start;
924 pair < node->data.mapping.pairs.top; ++pair) {
925 yaml_node_t *key, *value;
927 key = yaml_document_get_node(document, pair->key);
928 value = yaml_document_get_node(document, pair->value);
930 if (key->type != YAML_SCALAR_NODE)
931 isl_die(ctx, isl_error_invalid, "expecting scalar key",
932 return NULL);
934 if (!strcmp((char *) key->data.scalar.value, "condition")) {
935 cond = extract_expr(ctx, document, value);
936 if (!cond)
937 goto error;
939 if (!strcmp((char *) key->data.scalar.value, "then")) {
940 then_body = extract_tree(ctx, document, value);
941 if (!then_body)
942 goto error;
946 if (!cond)
947 isl_die(ctx, isl_error_invalid,
948 "no condition field", goto error);
949 if (!then_body)
950 isl_die(ctx, isl_error_invalid,
951 "no then body", goto error);
953 return pet_tree_new_if(cond, then_body);
954 error:
955 pet_expr_free(cond);
956 pet_tree_free(then_body);
957 return NULL;
960 /* Extract a pet_tree of type pet_tree_if_else from "node".
962 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
963 yaml_document_t *document, yaml_node_t *node)
965 yaml_node_pair_t *pair;
966 pet_expr *cond = NULL;
967 pet_tree *then_body = NULL;
968 pet_tree *else_body = NULL;
970 for (pair = node->data.mapping.pairs.start;
971 pair < node->data.mapping.pairs.top; ++pair) {
972 yaml_node_t *key, *value;
974 key = yaml_document_get_node(document, pair->key);
975 value = yaml_document_get_node(document, pair->value);
977 if (key->type != YAML_SCALAR_NODE)
978 isl_die(ctx, isl_error_invalid, "expecting scalar key",
979 return NULL);
981 if (!strcmp((char *) key->data.scalar.value, "condition")) {
982 cond = extract_expr(ctx, document, value);
983 if (!cond)
984 goto error;
986 if (!strcmp((char *) key->data.scalar.value, "then")) {
987 then_body = extract_tree(ctx, document, value);
988 if (!then_body)
989 goto error;
991 if (!strcmp((char *) key->data.scalar.value, "else")) {
992 else_body = extract_tree(ctx, document, value);
993 if (!else_body)
994 goto error;
998 if (!cond)
999 isl_die(ctx, isl_error_invalid,
1000 "no condition field", goto error);
1001 if (!then_body)
1002 isl_die(ctx, isl_error_invalid,
1003 "no then body", goto error);
1004 if (!else_body)
1005 isl_die(ctx, isl_error_invalid,
1006 "no else body", goto error);
1008 return pet_tree_new_if_else(cond, then_body, else_body);
1009 error:
1010 pet_expr_free(cond);
1011 pet_tree_free(then_body);
1012 pet_tree_free(else_body);
1013 return NULL;
1016 /* Extract a pet_tree of type pet_tree_for from "node".
1018 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1019 yaml_document_t *document, yaml_node_t *node)
1021 yaml_node_pair_t *pair;
1022 int declared = 0;
1023 int independent = 0;
1024 pet_expr *iv = NULL;
1025 pet_expr *init = NULL;
1026 pet_expr *cond = NULL;
1027 pet_expr *inc = NULL;
1028 pet_tree *body = NULL;
1030 for (pair = node->data.mapping.pairs.start;
1031 pair < node->data.mapping.pairs.top; ++pair) {
1032 yaml_node_t *key, *value;
1034 key = yaml_document_get_node(document, pair->key);
1035 value = yaml_document_get_node(document, pair->value);
1037 if (key->type != YAML_SCALAR_NODE)
1038 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1039 return NULL);
1041 if (!strcmp((char *) key->data.scalar.value, "declared"))
1042 declared = extract_int(ctx, document, value);
1043 if (!strcmp((char *) key->data.scalar.value, "independent"))
1044 independent = extract_int(ctx, document, value);
1045 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1046 iv = extract_expr(ctx, document, value);
1047 if (!iv)
1048 goto error;
1050 if (!strcmp((char *) key->data.scalar.value,
1051 "initialization")) {
1052 init = extract_expr(ctx, document, value);
1053 if (!init)
1054 goto error;
1056 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1057 cond = extract_expr(ctx, document, value);
1058 if (!cond)
1059 goto error;
1061 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1062 inc = extract_expr(ctx, document, value);
1063 if (!inc)
1064 goto error;
1066 if (!strcmp((char *) key->data.scalar.value, "body")) {
1067 body = extract_tree(ctx, document, value);
1068 if (!body)
1069 goto error;
1073 if (!iv)
1074 isl_die(ctx, isl_error_invalid,
1075 "no variable field", goto error);
1076 if (!init)
1077 isl_die(ctx, isl_error_invalid,
1078 "no initialization field", goto error);
1079 if (!cond)
1080 isl_die(ctx, isl_error_invalid,
1081 "no condition field", goto error);
1082 if (!inc)
1083 isl_die(ctx, isl_error_invalid,
1084 "no increment field", goto error);
1085 if (!body)
1086 isl_die(ctx, isl_error_invalid,
1087 "no body field", goto error);
1089 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1090 body);
1091 error:
1092 pet_expr_free(iv);
1093 pet_expr_free(init);
1094 pet_expr_free(cond);
1095 pet_expr_free(inc);
1096 pet_tree_free(body);
1097 return NULL;
1100 /* Extract a pet_tree from "node".
1102 * We first extract the type of the pet_tree and then call
1103 * the appropriate function to extract and construct a pet_tree
1104 * of that type.
1106 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1107 yaml_document_t *document, yaml_node_t *node)
1109 enum pet_tree_type type = pet_tree_error;
1110 pet_tree *tree;
1111 yaml_node_pair_t *pair;
1113 if (node->type != YAML_MAPPING_NODE)
1114 isl_die(ctx, isl_error_invalid, "expecting mapping",
1115 return NULL);
1117 for (pair = node->data.mapping.pairs.start;
1118 pair < node->data.mapping.pairs.top; ++pair) {
1119 yaml_node_t *key, *value;
1121 key = yaml_document_get_node(document, pair->key);
1122 value = yaml_document_get_node(document, pair->value);
1124 if (key->type != YAML_SCALAR_NODE)
1125 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1126 return pet_tree_free(tree));
1128 if (!strcmp((char *) key->data.scalar.value, "type"))
1129 type = extract_tree_type(ctx, document, value);
1132 if (type == pet_tree_error)
1133 isl_die(ctx, isl_error_invalid, "cannot determine type",
1134 return NULL);
1136 switch (type) {
1137 case pet_tree_error:
1138 return NULL;
1139 case pet_tree_block:
1140 tree = extract_tree_block(ctx, document, node);
1141 break;
1142 case pet_tree_break:
1143 tree = pet_tree_new_break(ctx);
1144 break;
1145 case pet_tree_continue:
1146 tree = pet_tree_new_continue(ctx);
1147 break;
1148 case pet_tree_decl:
1149 tree = extract_tree_decl(ctx, document, node);
1150 break;
1151 case pet_tree_decl_init:
1152 tree = extract_tree_decl_init(ctx, document, node);
1153 break;
1154 case pet_tree_expr:
1155 tree = extract_tree_expr(ctx, document, node);
1156 break;
1157 case pet_tree_for:
1158 tree = extract_tree_for(ctx, document, node);
1159 break;
1160 case pet_tree_while:
1161 tree = extract_tree_while(ctx, document, node);
1162 break;
1163 case pet_tree_infinite_loop:
1164 tree = extract_tree_infinite_loop(ctx, document, node);
1165 break;
1166 case pet_tree_if:
1167 tree = extract_tree_if(ctx, document, node);
1168 break;
1169 case pet_tree_if_else:
1170 tree = extract_tree_if_else(ctx, document, node);
1171 break;
1174 return tree;
1177 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1178 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1180 int i;
1181 yaml_node_item_t *item;
1183 if (node->type != YAML_SEQUENCE_NODE)
1184 isl_die(ctx, isl_error_invalid, "expecting sequence",
1185 return pet_stmt_free(stmt));
1187 stmt->n_arg = node->data.sequence.items.top
1188 - node->data.sequence.items.start;
1189 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1190 if (!stmt->args)
1191 return pet_stmt_free(stmt);
1193 for (item = node->data.sequence.items.start, i = 0;
1194 item < node->data.sequence.items.top; ++item, ++i) {
1195 yaml_node_t *n;
1197 n = yaml_document_get_node(document, *item);
1198 stmt->args[i] = extract_expr(ctx, document, n);
1199 if (!stmt->args[i])
1200 return pet_stmt_free(stmt);
1203 return stmt;
1206 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1207 yaml_node_t *node)
1209 struct pet_stmt *stmt;
1210 yaml_node_pair_t * pair;
1211 int line = -1;
1212 unsigned start = 0, end = 0;
1213 char *indent = NULL;
1215 if (node->type != YAML_MAPPING_NODE)
1216 isl_die(ctx, isl_error_invalid, "expecting mapping",
1217 return NULL);
1219 stmt = isl_calloc_type(ctx, struct pet_stmt);
1220 if (!stmt)
1221 return NULL;
1223 stmt->loc = &pet_loc_dummy;
1225 for (pair = node->data.mapping.pairs.start;
1226 pair < node->data.mapping.pairs.top; ++pair) {
1227 yaml_node_t *key, *value;
1229 key = yaml_document_get_node(document, pair->key);
1230 value = yaml_document_get_node(document, pair->value);
1232 if (key->type != YAML_SCALAR_NODE)
1233 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1234 return pet_stmt_free(stmt));
1236 if (!strcmp((char *) key->data.scalar.value, "indent"))
1237 indent = extract_string(ctx, document, value);
1238 if (!strcmp((char *) key->data.scalar.value, "line"))
1239 line = extract_int(ctx, document, value);
1240 if (!strcmp((char *) key->data.scalar.value, "start"))
1241 start = extract_int(ctx, document, value);
1242 if (!strcmp((char *) key->data.scalar.value, "end"))
1243 end = extract_int(ctx, document, value);
1244 if (!strcmp((char *) key->data.scalar.value, "domain"))
1245 stmt->domain = extract_set(ctx, document, value);
1246 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1247 stmt->schedule = extract_map(ctx, document, value);
1248 if (!strcmp((char *) key->data.scalar.value, "body"))
1249 stmt->body = extract_tree(ctx, document, value);
1251 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1252 stmt = extract_stmt_arguments(ctx, document,
1253 value, stmt);
1254 if (!stmt)
1255 return NULL;
1258 if (!indent)
1259 indent = strdup("");
1260 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1261 if (!stmt->loc)
1262 return pet_stmt_free(stmt);
1264 return stmt;
1267 static struct pet_scop *extract_statements(isl_ctx *ctx,
1268 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1270 int i;
1271 yaml_node_item_t *item;
1273 if (node->type != YAML_SEQUENCE_NODE)
1274 isl_die(ctx, isl_error_invalid, "expecting sequence",
1275 return NULL);
1277 scop->n_stmt = node->data.sequence.items.top
1278 - node->data.sequence.items.start;
1279 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1280 if (!scop->stmts)
1281 return pet_scop_free(scop);
1283 for (item = node->data.sequence.items.start, i = 0;
1284 item < node->data.sequence.items.top; ++item, ++i) {
1285 yaml_node_t *n;
1287 n = yaml_document_get_node(document, *item);
1288 scop->stmts[i] = extract_stmt(ctx, document, n);
1289 if (!scop->stmts[i])
1290 return pet_scop_free(scop);
1293 return scop;
1296 /* Extract a pet_implication from "node".
1298 static struct pet_implication *extract_implication(isl_ctx *ctx,
1299 yaml_document_t *document, yaml_node_t *node)
1301 struct pet_implication *implication;
1302 yaml_node_pair_t * pair;
1304 if (node->type != YAML_MAPPING_NODE)
1305 isl_die(ctx, isl_error_invalid, "expecting mapping",
1306 return NULL);
1308 implication = isl_calloc_type(ctx, struct pet_implication);
1309 if (!implication)
1310 return NULL;
1312 for (pair = node->data.mapping.pairs.start;
1313 pair < node->data.mapping.pairs.top; ++pair) {
1314 yaml_node_t *key, *value;
1316 key = yaml_document_get_node(document, pair->key);
1317 value = yaml_document_get_node(document, pair->value);
1319 if (key->type != YAML_SCALAR_NODE)
1320 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1321 return pet_implication_free(implication));
1323 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1324 implication->satisfied =
1325 extract_int(ctx, document, value);
1326 if (!strcmp((char *) key->data.scalar.value, "extension"))
1327 implication->extension =
1328 extract_map(ctx, document, value);
1331 return implication;
1334 /* Extract a sequence of implications from "node" and
1335 * store them in scop->implications.
1337 static struct pet_scop *extract_implications(isl_ctx *ctx,
1338 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1340 int i;
1341 yaml_node_item_t *item;
1343 if (node->type != YAML_SEQUENCE_NODE)
1344 isl_die(ctx, isl_error_invalid, "expecting sequence",
1345 return NULL);
1347 scop->n_implication = node->data.sequence.items.top
1348 - node->data.sequence.items.start;
1349 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1350 scop->n_implication);
1351 if (!scop->implications)
1352 return pet_scop_free(scop);
1354 for (item = node->data.sequence.items.start, i = 0;
1355 item < node->data.sequence.items.top; ++item, ++i) {
1356 yaml_node_t *n;
1358 n = yaml_document_get_node(document, *item);
1359 scop->implications[i] = extract_implication(ctx, document, n);
1360 if (!scop->implications[i])
1361 return pet_scop_free(scop);
1364 return scop;
1367 /* Extract a pet_independence from "node".
1369 static struct pet_independence *extract_independence(isl_ctx *ctx,
1370 yaml_document_t *document, yaml_node_t *node)
1372 struct pet_independence *independence;
1373 yaml_node_pair_t * pair;
1375 if (node->type != YAML_MAPPING_NODE)
1376 isl_die(ctx, isl_error_invalid, "expecting mapping",
1377 return NULL);
1379 independence = isl_calloc_type(ctx, struct pet_independence);
1380 if (!independence)
1381 return NULL;
1383 for (pair = node->data.mapping.pairs.start;
1384 pair < node->data.mapping.pairs.top; ++pair) {
1385 yaml_node_t *key, *value;
1387 key = yaml_document_get_node(document, pair->key);
1388 value = yaml_document_get_node(document, pair->value);
1390 if (key->type != YAML_SCALAR_NODE)
1391 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1392 return pet_independence_free(independence));
1394 if (!strcmp((char *) key->data.scalar.value, "filter"))
1395 independence->filter =
1396 extract_union_map(ctx, document, value);
1397 if (!strcmp((char *) key->data.scalar.value, "local"))
1398 independence->local =
1399 extract_union_set(ctx, document, value);
1402 if (!independence->filter)
1403 isl_die(ctx, isl_error_invalid, "no filter field",
1404 return pet_independence_free(independence));
1405 if (!independence->local)
1406 isl_die(ctx, isl_error_invalid, "no local field",
1407 return pet_independence_free(independence));
1409 return independence;
1412 /* Extract a sequence of independences from "node" and
1413 * store them in scop->independences.
1415 static struct pet_scop *extract_independences(isl_ctx *ctx,
1416 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1418 int i;
1419 yaml_node_item_t *item;
1421 if (node->type != YAML_SEQUENCE_NODE)
1422 isl_die(ctx, isl_error_invalid, "expecting sequence",
1423 return NULL);
1425 scop->n_independence = node->data.sequence.items.top
1426 - node->data.sequence.items.start;
1427 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1428 scop->n_independence);
1429 if (!scop->independences)
1430 return pet_scop_free(scop);
1432 for (item = node->data.sequence.items.start, i = 0;
1433 item < node->data.sequence.items.top; ++item, ++i) {
1434 yaml_node_t *n;
1436 n = yaml_document_get_node(document, *item);
1437 scop->independences[i] = extract_independence(ctx, document, n);
1438 if (!scop->independences[i])
1439 return pet_scop_free(scop);
1442 return scop;
1445 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1446 yaml_node_t *node)
1448 struct pet_scop *scop;
1449 yaml_node_pair_t * pair;
1451 if (!node)
1452 return NULL;
1454 if (node->type != YAML_MAPPING_NODE)
1455 isl_die(ctx, isl_error_invalid, "expecting mapping",
1456 return NULL);
1458 scop = pet_scop_alloc(ctx);
1459 if (!scop)
1460 return NULL;
1462 for (pair = node->data.mapping.pairs.start;
1463 pair < node->data.mapping.pairs.top; ++pair) {
1464 yaml_node_t *key, *value;
1466 key = yaml_document_get_node(document, pair->key);
1467 value = yaml_document_get_node(document, pair->value);
1469 if (key->type != YAML_SCALAR_NODE)
1470 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1471 return pet_scop_free(scop));
1472 if (!strcmp((char *) key->data.scalar.value, "context"))
1473 scop->context = extract_set(ctx, document, value);
1474 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1475 scop->context_value = extract_set(ctx, document, value);
1476 if (!strcmp((char *) key->data.scalar.value, "types"))
1477 scop = extract_types(ctx, document, value, scop);
1478 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1479 scop = extract_arrays(ctx, document, value, scop);
1480 if (!strcmp((char *) key->data.scalar.value, "statements"))
1481 scop = extract_statements(ctx, document, value, scop);
1482 if (!strcmp((char *) key->data.scalar.value, "implications"))
1483 scop = extract_implications(ctx, document, value, scop);
1484 if (!strcmp((char *) key->data.scalar.value, "independences"))
1485 scop = extract_independences(ctx,
1486 document, value, scop);
1487 if (!scop)
1488 return NULL;
1491 if (!scop->context_value) {
1492 isl_space *space = isl_space_params_alloc(ctx, 0);
1493 scop->context_value = isl_set_universe(space);
1494 if (!scop->context_value)
1495 return pet_scop_free(scop);
1498 return scop;
1501 /* Extract a pet_scop from the YAML description in "in".
1503 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1505 struct pet_scop *scop = NULL;
1506 yaml_parser_t parser;
1507 yaml_node_t *root;
1508 yaml_document_t document = { 0 };
1510 yaml_parser_initialize(&parser);
1512 yaml_parser_set_input_file(&parser, in);
1514 if (!yaml_parser_load(&parser, &document))
1515 goto error;
1517 root = yaml_document_get_root_node(&document);
1519 scop = extract_scop(ctx, &document, root);
1521 yaml_document_delete(&document);
1523 yaml_parser_delete(&parser);
1525 return scop;
1526 error:
1527 yaml_parser_delete(&parser);
1528 pet_scop_free(scop);
1529 return NULL;