scop.c: fix typos in comments
[pet.git] / parse.c
blob2c978e9c5300c63ffe69e281664f3743e953eabb
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Leiden University.
32 */
34 #include <stdlib.h>
35 #include <yaml.h>
37 #include "scop.h"
38 #include "scop_yaml.h"
40 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
41 yaml_node_t *node)
43 if (node->type != YAML_SCALAR_NODE)
44 isl_die(ctx, isl_error_invalid, "expecting scalar node",
45 return NULL);
47 return strdup((char *) node->data.scalar.value);
50 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
51 yaml_node_t *node)
53 if (node->type != YAML_SCALAR_NODE)
54 isl_die(ctx, isl_error_invalid, "expecting scalar node",
55 return -1);
57 return atoi((char *) node->data.scalar.value);
60 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
61 yaml_node_t *node)
63 if (node->type != YAML_SCALAR_NODE)
64 isl_die(ctx, isl_error_invalid, "expecting scalar node",
65 return -1);
67 return strtod((char *) node->data.scalar.value, NULL);
70 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
71 yaml_document_t *document, yaml_node_t *node)
73 if (node->type != YAML_SCALAR_NODE)
74 isl_die(ctx, isl_error_invalid, "expecting scalar node",
75 return -1);
77 return pet_str_type((char *) node->data.scalar.value);
80 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
81 yaml_node_t *node)
83 if (node->type != YAML_SCALAR_NODE)
84 isl_die(ctx, isl_error_invalid, "expecting scalar node",
85 return -1);
87 return pet_str_op((char *) node->data.scalar.value);
90 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
91 yaml_node_t *node)
93 if (node->type != YAML_SCALAR_NODE)
94 isl_die(ctx, isl_error_invalid, "expecting scalar node",
95 return NULL);
97 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
100 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
101 yaml_node_t *node)
103 if (node->type != YAML_SCALAR_NODE)
104 isl_die(ctx, isl_error_invalid, "expecting scalar node",
105 return NULL);
107 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
110 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
111 yaml_node_t *node)
113 if (node->type != YAML_SCALAR_NODE)
114 isl_die(ctx, isl_error_invalid, "expecting scalar node",
115 return NULL);
117 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
120 /* Extract an isl_multi_pw_aff from "node".
122 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
123 yaml_document_t *document, yaml_node_t *node)
125 if (node->type != YAML_SCALAR_NODE)
126 isl_die(ctx, isl_error_invalid, "expecting scalar node",
127 return NULL);
129 return isl_multi_pw_aff_read_from_str(ctx,
130 (char *) node->data.scalar.value);
133 /* Extract a pet_type from "node".
135 static struct pet_type *extract_type(isl_ctx *ctx,
136 yaml_document_t *document, yaml_node_t *node)
138 struct pet_type *type;
139 yaml_node_pair_t * pair;
141 if (node->type != YAML_MAPPING_NODE)
142 isl_die(ctx, isl_error_invalid, "expecting mapping",
143 return NULL);
145 type = isl_calloc_type(ctx, struct pet_type);
146 if (!type)
147 return NULL;
149 for (pair = node->data.mapping.pairs.start;
150 pair < node->data.mapping.pairs.top; ++pair) {
151 yaml_node_t *key, *value;
153 key = yaml_document_get_node(document, pair->key);
154 value = yaml_document_get_node(document, pair->value);
156 if (key->type != YAML_SCALAR_NODE)
157 isl_die(ctx, isl_error_invalid, "expecting scalar key",
158 return pet_type_free(type));
160 if (!strcmp((char *) key->data.scalar.value, "name"))
161 type->name = extract_string(ctx, document, value);
162 if (!strcmp((char *) key->data.scalar.value, "definition"))
163 type->definition = extract_string(ctx, document, value);
166 return type;
169 /* Extract a sequence of types from "node" and store them in scop->types.
171 static struct pet_scop *extract_types(isl_ctx *ctx,
172 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
174 int i;
175 yaml_node_item_t *item;
177 if (node->type != YAML_SEQUENCE_NODE)
178 isl_die(ctx, isl_error_invalid, "expecting sequence",
179 return NULL);
181 scop->n_type = node->data.sequence.items.top
182 - node->data.sequence.items.start;
183 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
184 if (!scop->types)
185 return pet_scop_free(scop);
187 for (item = node->data.sequence.items.start, i = 0;
188 item < node->data.sequence.items.top; ++item, ++i) {
189 yaml_node_t *n;
191 n = yaml_document_get_node(document, *item);
192 scop->types[i] = extract_type(ctx, document, n);
193 if (!scop->types[i])
194 return pet_scop_free(scop);
197 return scop;
200 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
201 yaml_node_t *node)
203 struct pet_array *array;
204 yaml_node_pair_t * pair;
206 if (node->type != YAML_MAPPING_NODE)
207 isl_die(ctx, isl_error_invalid, "expecting mapping",
208 return NULL);
210 array = isl_calloc_type(ctx, struct pet_array);
211 if (!array)
212 return NULL;
214 for (pair = node->data.mapping.pairs.start;
215 pair < node->data.mapping.pairs.top; ++pair) {
216 yaml_node_t *key, *value;
218 key = yaml_document_get_node(document, pair->key);
219 value = yaml_document_get_node(document, pair->value);
221 if (key->type != YAML_SCALAR_NODE)
222 isl_die(ctx, isl_error_invalid, "expecting scalar key",
223 return pet_array_free(array));
225 if (!strcmp((char *) key->data.scalar.value, "context"))
226 array->context = extract_set(ctx, document, value);
227 if (!strcmp((char *) key->data.scalar.value, "extent"))
228 array->extent = extract_set(ctx, document, value);
229 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
230 array->value_bounds = extract_set(ctx, document, value);
231 if (!strcmp((char *) key->data.scalar.value, "element_type"))
232 array->element_type =
233 extract_string(ctx, document, value);
234 if (!strcmp((char *) key->data.scalar.value, "element_size"))
235 array->element_size = extract_int(ctx, document, value);
236 if (!strcmp((char *) key->data.scalar.value,
237 "element_is_record"))
238 array->element_is_record =
239 extract_int(ctx, document, value);
240 if (!strcmp((char *) key->data.scalar.value, "live_out"))
241 array->live_out = extract_int(ctx, document, value);
242 if (!strcmp((char *) key->data.scalar.value,
243 "uniquely_defined"))
244 array->uniquely_defined =
245 extract_int(ctx, document, value);
246 if (!strcmp((char *) key->data.scalar.value, "declared"))
247 array->declared = extract_int(ctx, document, value);
248 if (!strcmp((char *) key->data.scalar.value, "exposed"))
249 array->exposed = extract_int(ctx, document, value);
252 return array;
255 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
256 yaml_node_t *node, struct pet_scop *scop)
258 int i;
259 yaml_node_item_t *item;
261 if (node->type != YAML_SEQUENCE_NODE)
262 isl_die(ctx, isl_error_invalid, "expecting sequence",
263 return NULL);
265 scop->n_array = node->data.sequence.items.top
266 - node->data.sequence.items.start;
267 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
268 if (!scop->arrays)
269 return pet_scop_free(scop);
271 for (item = node->data.sequence.items.start, i = 0;
272 item < node->data.sequence.items.top; ++item, ++i) {
273 yaml_node_t *n;
275 n = yaml_document_get_node(document, *item);
276 scop->arrays[i] = extract_array(ctx, document, n);
277 if (!scop->arrays[i])
278 return pet_scop_free(scop);
281 return scop;
284 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
285 yaml_node_t *node);
287 static struct pet_expr *extract_arguments(isl_ctx *ctx,
288 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
290 int i;
291 yaml_node_item_t *item;
293 if (node->type != YAML_SEQUENCE_NODE)
294 isl_die(ctx, isl_error_invalid, "expecting sequence",
295 return pet_expr_free(expr));
297 expr->n_arg = node->data.sequence.items.top
298 - node->data.sequence.items.start;
299 expr->args = isl_calloc_array(ctx, struct pet_expr *, expr->n_arg);
300 if (!expr->args)
301 return pet_expr_free(expr);
303 for (item = node->data.sequence.items.start, i = 0;
304 item < node->data.sequence.items.top; ++item, ++i) {
305 yaml_node_t *n;
307 n = yaml_document_get_node(document, *item);
308 expr->args[i] = extract_expr(ctx, document, n);
309 if (!expr->args[i])
310 return pet_expr_free(expr);
313 return expr;
316 /* Extract pet_expr_double specific fields from "node" and
317 * update "expr" accordingly.
319 static struct pet_expr *extract_expr_double(isl_ctx *ctx,
320 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
322 yaml_node_pair_t *pair;
324 for (pair = node->data.mapping.pairs.start;
325 pair < node->data.mapping.pairs.top; ++pair) {
326 yaml_node_t *key, *value;
328 key = yaml_document_get_node(document, pair->key);
329 value = yaml_document_get_node(document, pair->value);
331 if (key->type != YAML_SCALAR_NODE)
332 isl_die(ctx, isl_error_invalid, "expecting scalar key",
333 return pet_expr_free(expr));
335 if (!strcmp((char *) key->data.scalar.value, "value"))
336 expr->d.val = extract_double(ctx, document, value);
337 if (!strcmp((char *) key->data.scalar.value, "string"))
338 expr->d.s = extract_string(ctx, document, value);
341 return expr;
344 /* Extract pet_expr_access specific fields from "node" and
345 * update "expr" accordingly.
347 static struct pet_expr *extract_expr_access(isl_ctx *ctx,
348 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
350 yaml_node_pair_t *pair;
352 for (pair = node->data.mapping.pairs.start;
353 pair < node->data.mapping.pairs.top; ++pair) {
354 yaml_node_t *key, *value;
356 key = yaml_document_get_node(document, pair->key);
357 value = yaml_document_get_node(document, pair->value);
359 if (key->type != YAML_SCALAR_NODE)
360 isl_die(ctx, isl_error_invalid, "expecting scalar key",
361 return pet_expr_free(expr));
363 if (!strcmp((char *) key->data.scalar.value, "relation"))
364 expr->acc.access = extract_map(ctx, document, value);
365 if (!strcmp((char *) key->data.scalar.value, "index"))
366 expr->acc.index = extract_multi_pw_aff(ctx, document,
367 value);
368 if (!strcmp((char *) key->data.scalar.value, "reference"))
369 expr->acc.ref_id = extract_id(ctx, document, value);
370 if (!strcmp((char *) key->data.scalar.value, "read"))
371 expr->acc.read = extract_int(ctx, document, value);
372 if (!strcmp((char *) key->data.scalar.value, "write"))
373 expr->acc.write = extract_int(ctx, document, value);
376 return expr;
379 /* Extract operation expression specific fields from "node" and
380 * update "expr" accordingly.
382 static struct pet_expr *extract_expr_op(isl_ctx *ctx,
383 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
385 yaml_node_pair_t *pair;
387 for (pair = node->data.mapping.pairs.start;
388 pair < node->data.mapping.pairs.top; ++pair) {
389 yaml_node_t *key, *value;
391 key = yaml_document_get_node(document, pair->key);
392 value = yaml_document_get_node(document, pair->value);
394 if (key->type != YAML_SCALAR_NODE)
395 isl_die(ctx, isl_error_invalid, "expecting scalar key",
396 return pet_expr_free(expr));
398 if (!strcmp((char *) key->data.scalar.value, "operation"))
399 expr->op = extract_op(ctx, document, value);
402 return expr;
405 /* Extract pet_expr_call specific fields from "node" and
406 * update "expr" accordingly.
408 static struct pet_expr *extract_expr_call(isl_ctx *ctx,
409 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
411 yaml_node_pair_t *pair;
413 for (pair = node->data.mapping.pairs.start;
414 pair < node->data.mapping.pairs.top; ++pair) {
415 yaml_node_t *key, *value;
417 key = yaml_document_get_node(document, pair->key);
418 value = yaml_document_get_node(document, pair->value);
420 if (key->type != YAML_SCALAR_NODE)
421 isl_die(ctx, isl_error_invalid, "expecting scalar key",
422 return pet_expr_free(expr));
424 if (!strcmp((char *) key->data.scalar.value, "name"))
425 expr->name = extract_string(ctx, document, value);
428 return expr;
431 /* Extract pet_expr_cast specific fields from "node" and
432 * update "expr" accordingly.
434 static struct pet_expr *extract_expr_cast(isl_ctx *ctx,
435 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
437 yaml_node_pair_t *pair;
439 for (pair = node->data.mapping.pairs.start;
440 pair < node->data.mapping.pairs.top; ++pair) {
441 yaml_node_t *key, *value;
443 key = yaml_document_get_node(document, pair->key);
444 value = yaml_document_get_node(document, pair->value);
446 if (key->type != YAML_SCALAR_NODE)
447 isl_die(ctx, isl_error_invalid, "expecting scalar key",
448 return pet_expr_free(expr));
450 if (!strcmp((char *) key->data.scalar.value, "type_name"))
451 expr->type_name = extract_string(ctx, document, value);
454 return expr;
457 /* Extract a pet_expr from "node".
459 * We first extract the type and arguments of the expression and
460 * then extract additional fields depending on the type.
462 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
463 yaml_node_t *node)
465 struct pet_expr *expr;
466 yaml_node_pair_t *pair;
468 if (node->type != YAML_MAPPING_NODE)
469 isl_die(ctx, isl_error_invalid, "expecting mapping",
470 return NULL);
472 expr = isl_calloc_type(ctx, struct pet_expr);
473 if (!expr)
474 return NULL;
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, "type"))
488 expr->type = extract_expr_type(ctx, document, value);
490 if (!strcmp((char *) key->data.scalar.value, "arguments"))
491 expr = extract_arguments(ctx, document, value, expr);
492 if (!expr)
493 return NULL;
496 switch (expr->type) {
497 case pet_expr_access:
498 expr = extract_expr_access(ctx, document, node, expr);
499 break;
500 case pet_expr_double:
501 expr = extract_expr_double(ctx, document, node, expr);
502 break;
503 case pet_expr_call:
504 expr = extract_expr_call(ctx, document, node, expr);
505 break;
506 case pet_expr_cast:
507 expr = extract_expr_cast(ctx, document, node, expr);
508 break;
509 case pet_expr_unary:
510 case pet_expr_binary:
511 case pet_expr_ternary:
512 expr = extract_expr_op(ctx, document, node, expr);
513 break;
516 return expr;
519 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
520 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
522 int i;
523 yaml_node_item_t *item;
525 if (node->type != YAML_SEQUENCE_NODE)
526 isl_die(ctx, isl_error_invalid, "expecting sequence",
527 return pet_stmt_free(stmt));
529 stmt->n_arg = node->data.sequence.items.top
530 - node->data.sequence.items.start;
531 stmt->args = isl_calloc_array(ctx, struct pet_expr *, stmt->n_arg);
532 if (!stmt->args)
533 return pet_stmt_free(stmt);
535 for (item = node->data.sequence.items.start, i = 0;
536 item < node->data.sequence.items.top; ++item, ++i) {
537 yaml_node_t *n;
539 n = yaml_document_get_node(document, *item);
540 stmt->args[i] = extract_expr(ctx, document, n);
541 if (!stmt->args[i])
542 return pet_stmt_free(stmt);
545 return stmt;
548 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
549 yaml_node_t *node)
551 struct pet_stmt *stmt;
552 yaml_node_pair_t * pair;
554 if (node->type != YAML_MAPPING_NODE)
555 isl_die(ctx, isl_error_invalid, "expecting mapping",
556 return NULL);
558 stmt = isl_calloc_type(ctx, struct pet_stmt);
559 if (!stmt)
560 return NULL;
562 for (pair = node->data.mapping.pairs.start;
563 pair < node->data.mapping.pairs.top; ++pair) {
564 yaml_node_t *key, *value;
566 key = yaml_document_get_node(document, pair->key);
567 value = yaml_document_get_node(document, pair->value);
569 if (key->type != YAML_SCALAR_NODE)
570 isl_die(ctx, isl_error_invalid, "expecting scalar key",
571 return pet_stmt_free(stmt));
573 if (!strcmp((char *) key->data.scalar.value, "line"))
574 stmt->line = extract_int(ctx, document, value);
575 if (!strcmp((char *) key->data.scalar.value, "domain"))
576 stmt->domain = extract_set(ctx, document, value);
577 if (!strcmp((char *) key->data.scalar.value, "schedule"))
578 stmt->schedule = extract_map(ctx, document, value);
579 if (!strcmp((char *) key->data.scalar.value, "body"))
580 stmt->body = extract_expr(ctx, document, value);
582 if (!strcmp((char *) key->data.scalar.value, "arguments"))
583 stmt = extract_stmt_arguments(ctx, document,
584 value, stmt);
585 if (!stmt)
586 return NULL;
589 return stmt;
592 static struct pet_scop *extract_statements(isl_ctx *ctx,
593 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
595 int i;
596 yaml_node_item_t *item;
598 if (node->type != YAML_SEQUENCE_NODE)
599 isl_die(ctx, isl_error_invalid, "expecting sequence",
600 return NULL);
602 scop->n_stmt = node->data.sequence.items.top
603 - node->data.sequence.items.start;
604 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
605 if (!scop->stmts)
606 return pet_scop_free(scop);
608 for (item = node->data.sequence.items.start, i = 0;
609 item < node->data.sequence.items.top; ++item, ++i) {
610 yaml_node_t *n;
612 n = yaml_document_get_node(document, *item);
613 scop->stmts[i] = extract_stmt(ctx, document, n);
614 if (!scop->stmts[i])
615 return pet_scop_free(scop);
618 return scop;
621 /* Extract a pet_implication from "node".
623 static struct pet_implication *extract_implication(isl_ctx *ctx,
624 yaml_document_t *document, yaml_node_t *node)
626 struct pet_implication *implication;
627 yaml_node_pair_t * pair;
629 if (node->type != YAML_MAPPING_NODE)
630 isl_die(ctx, isl_error_invalid, "expecting mapping",
631 return NULL);
633 implication = isl_calloc_type(ctx, struct pet_implication);
634 if (!implication)
635 return NULL;
637 for (pair = node->data.mapping.pairs.start;
638 pair < node->data.mapping.pairs.top; ++pair) {
639 yaml_node_t *key, *value;
641 key = yaml_document_get_node(document, pair->key);
642 value = yaml_document_get_node(document, pair->value);
644 if (key->type != YAML_SCALAR_NODE)
645 isl_die(ctx, isl_error_invalid, "expecting scalar key",
646 return pet_implication_free(implication));
648 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
649 implication->satisfied =
650 extract_int(ctx, document, value);
651 if (!strcmp((char *) key->data.scalar.value, "extension"))
652 implication->extension =
653 extract_map(ctx, document, value);
656 return implication;
659 /* Extract a sequence of implications from "node" and
660 * store them in scop->implications.
662 static struct pet_scop *extract_implications(isl_ctx *ctx,
663 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
665 int i;
666 yaml_node_item_t *item;
668 if (node->type != YAML_SEQUENCE_NODE)
669 isl_die(ctx, isl_error_invalid, "expecting sequence",
670 return NULL);
672 scop->n_implication = node->data.sequence.items.top
673 - node->data.sequence.items.start;
674 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
675 scop->n_implication);
676 if (!scop->implications)
677 return pet_scop_free(scop);
679 for (item = node->data.sequence.items.start, i = 0;
680 item < node->data.sequence.items.top; ++item, ++i) {
681 yaml_node_t *n;
683 n = yaml_document_get_node(document, *item);
684 scop->implications[i] = extract_implication(ctx, document, n);
685 if (!scop->implications[i])
686 return pet_scop_free(scop);
689 return scop;
692 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
693 yaml_node_t *node)
695 struct pet_scop *scop;
696 yaml_node_pair_t * pair;
698 if (!node)
699 return NULL;
701 if (node->type != YAML_MAPPING_NODE)
702 isl_die(ctx, isl_error_invalid, "expecting mapping",
703 return NULL);
705 scop = pet_scop_alloc(ctx);
706 if (!scop)
707 return NULL;
709 for (pair = node->data.mapping.pairs.start;
710 pair < node->data.mapping.pairs.top; ++pair) {
711 yaml_node_t *key, *value;
713 key = yaml_document_get_node(document, pair->key);
714 value = yaml_document_get_node(document, pair->value);
716 if (key->type != YAML_SCALAR_NODE)
717 isl_die(ctx, isl_error_invalid, "expecting scalar key",
718 return pet_scop_free(scop));
719 if (!strcmp((char *) key->data.scalar.value, "context"))
720 scop->context = extract_set(ctx, document, value);
721 if (!strcmp((char *) key->data.scalar.value, "context_value"))
722 scop->context_value = extract_set(ctx, document, value);
723 if (!strcmp((char *) key->data.scalar.value, "types"))
724 scop = extract_types(ctx, document, value, scop);
725 if (!strcmp((char *) key->data.scalar.value, "arrays"))
726 scop = extract_arrays(ctx, document, value, scop);
727 if (!strcmp((char *) key->data.scalar.value, "statements"))
728 scop = extract_statements(ctx, document, value, scop);
729 if (!strcmp((char *) key->data.scalar.value, "implications"))
730 scop = extract_implications(ctx, document, value, scop);
731 if (!scop)
732 return NULL;
735 if (!scop->context_value) {
736 isl_space *space = isl_space_params_alloc(ctx, 0);
737 scop->context_value = isl_set_universe(space);
738 if (!scop->context_value)
739 return pet_scop_free(scop);
742 return scop;
745 /* Extract a pet_scop from the YAML description in "in".
747 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
749 struct pet_scop *scop = NULL;
750 yaml_parser_t parser;
751 yaml_node_t *root;
752 yaml_document_t document = { 0 };
754 yaml_parser_initialize(&parser);
756 yaml_parser_set_input_file(&parser, in);
758 if (!yaml_parser_load(&parser, &document))
759 goto error;
761 root = yaml_document_get_root_node(&document);
763 scop = extract_scop(ctx, &document, root);
765 yaml_document_delete(&document);
767 yaml_parser_delete(&parser);
769 return scop;
770 error:
771 yaml_parser_delete(&parser);
772 pet_scop_free(scop);
773 return NULL;