pet.cc: extract out PetASTConsumer::add_pragma_handlers
[pet.git] / emit.c
blob1bd7f16f3a0e92f28741a2e28e94ed6169f2c80b
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 <yaml.h>
37 #include "expr.h"
38 #include "loc.h"
39 #include "scop.h"
40 #include "scop_yaml.h"
41 #include "tree.h"
43 static int emit_string(yaml_emitter_t *emitter, const char *str)
45 yaml_event_t event;
47 if (!yaml_scalar_event_initialize(&event, NULL, NULL,
48 (yaml_char_t *) str, strlen(str),
49 1, 1, YAML_PLAIN_SCALAR_STYLE))
50 return -1;
51 if (!yaml_emitter_emit(emitter, &event))
52 return -1;
54 return 0;
57 /* Print the string "name" and the string "str" to "emitter".
59 static int emit_named_string(yaml_emitter_t *emitter, const char *name,
60 const char *str)
62 if (emit_string(emitter, name) < 0)
63 return -1;
64 if (emit_string(emitter, str) < 0)
65 return -1;
66 return 0;
69 /* Print the isl_id "id" to "emitter".
71 static int emit_id(yaml_emitter_t *emitter, __isl_keep isl_id *id)
73 return emit_string(emitter, isl_id_get_name(id));
76 /* Print the string "name" and the isl_id "id" to "emitter".
78 static int emit_named_id(yaml_emitter_t *emitter, const char *name,
79 __isl_keep isl_id *id)
81 if (emit_string(emitter, name) < 0)
82 return -1;
83 if (emit_id(emitter, id) < 0)
84 return -1;
85 return 0;
88 static int emit_int(yaml_emitter_t *emitter, int i)
90 char buffer[40];
92 snprintf(buffer, sizeof(buffer), "%d", i);
93 return emit_string(emitter, buffer);
96 static int emit_named_int(yaml_emitter_t *emitter, const char *name, int i)
98 if (emit_string(emitter, name) < 0)
99 return -1;
100 if (emit_int(emitter, i) < 0)
101 return -1;
102 return 0;
105 /* Print the unsigned integer "u" to "emitter".
107 static int emit_unsigned(yaml_emitter_t *emitter, unsigned u)
109 char buffer[40];
111 snprintf(buffer, sizeof(buffer), "%u", u);
112 return emit_string(emitter, buffer);
115 /* Print the string "name" and the unsigned integer "u" to "emitter".
117 static int emit_named_unsigned(yaml_emitter_t *emitter, const char *name,
118 unsigned u)
120 if (emit_string(emitter, name) < 0)
121 return -1;
122 if (emit_int(emitter, u) < 0)
123 return -1;
124 return 0;
127 static int emit_double(yaml_emitter_t *emitter, double d)
129 char buffer[40];
131 snprintf(buffer, sizeof(buffer), "%g", d);
132 return emit_string(emitter, buffer);
135 static int emit_map(yaml_emitter_t *emitter, __isl_keep isl_map *map)
137 isl_ctx *ctx = isl_map_get_ctx(map);
138 isl_printer *p;
139 char *str;
140 int r;
142 p = isl_printer_to_str(ctx);
143 p = isl_printer_print_map(p, map);
144 str = isl_printer_get_str(p);
145 isl_printer_free(p);
146 r = emit_string(emitter, str);
147 free(str);
148 return r;
151 /* Print the isl_val "val" to "emitter".
153 static int emit_val(yaml_emitter_t *emitter, __isl_keep isl_val *val)
155 isl_ctx *ctx = isl_val_get_ctx(val);
156 isl_printer *p;
157 char *str;
158 int r;
160 p = isl_printer_to_str(ctx);
161 p = isl_printer_print_val(p, val);
162 str = isl_printer_get_str(p);
163 isl_printer_free(p);
164 r = emit_string(emitter, str);
165 free(str);
166 return r;
169 /* Print the string "name" and the isl_val "val" to "emitter".
171 static int emit_named_val(yaml_emitter_t *emitter, const char *name,
172 __isl_keep isl_val *val)
174 if (emit_string(emitter, name) < 0)
175 return -1;
176 if (emit_val(emitter, val) < 0)
177 return -1;
178 return 0;
181 static int emit_set(yaml_emitter_t *emitter, __isl_keep isl_set *set)
183 isl_ctx *ctx = isl_set_get_ctx(set);
184 isl_printer *p;
185 char *str;
186 int r;
188 p = isl_printer_to_str(ctx);
189 p = isl_printer_print_set(p, set);
190 str = isl_printer_get_str(p);
191 isl_printer_free(p);
192 r = emit_string(emitter, str);
193 free(str);
194 return r;
197 static int emit_named_set(yaml_emitter_t *emitter, const char *name,
198 __isl_keep isl_set *set)
200 if (emit_string(emitter, name) < 0)
201 return -1;
202 if (emit_set(emitter, set) < 0)
203 return -1;
204 return 0;
207 /* Print the string "name" and the map "map" to "emitter".
209 static int emit_named_map(yaml_emitter_t *emitter, const char *name,
210 __isl_keep isl_map *map)
212 if (emit_string(emitter, name) < 0)
213 return -1;
214 if (emit_map(emitter, map) < 0)
215 return -1;
216 return 0;
219 /* Print the isl_multi_pw_aff "mpa" to "emitter".
221 static int emit_multi_pw_aff(yaml_emitter_t *emitter,
222 __isl_keep isl_multi_pw_aff *mpa)
224 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(mpa);
225 isl_printer *p;
226 char *str;
227 int r;
229 p = isl_printer_to_str(ctx);
230 p = isl_printer_print_multi_pw_aff(p, mpa);
231 str = isl_printer_get_str(p);
232 isl_printer_free(p);
233 r = emit_string(emitter, str);
234 free(str);
235 return r;
238 /* Print the string "name" and the isl_multi_pw_aff "mpa" to "emitter".
240 static int emit_named_multi_pw_aff(yaml_emitter_t *emitter, const char *name,
241 __isl_keep isl_multi_pw_aff *mpa)
243 if (emit_string(emitter, name) < 0)
244 return -1;
245 if (emit_multi_pw_aff(emitter, mpa) < 0)
246 return -1;
247 return 0;
250 /* Print "type" to "emitter".
252 static int emit_type(yaml_emitter_t *emitter, struct pet_type *type)
254 yaml_event_t event;
256 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
257 YAML_BLOCK_MAPPING_STYLE))
258 return -1;
259 if (!yaml_emitter_emit(emitter, &event))
260 return -1;
262 if (emit_string(emitter, "name") < 0)
263 return -1;
264 if (emit_string(emitter, type->name) < 0)
265 return -1;
267 if (emit_string(emitter, "definition") < 0)
268 return -1;
269 if (emit_string(emitter, type->definition) < 0)
270 return -1;
272 if (!yaml_mapping_end_event_initialize(&event))
273 return -1;
274 if (!yaml_emitter_emit(emitter, &event))
275 return -1;
277 return 0;
280 /* Print the list of "n_type" "types", if any, to "emitter".
282 static int emit_types(yaml_emitter_t *emitter, int n_type,
283 struct pet_type **types)
285 int i;
286 yaml_event_t event;
288 if (n_type == 0)
289 return 0;
291 if (emit_string(emitter, "types") < 0)
292 return -1;
293 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
294 YAML_BLOCK_SEQUENCE_STYLE))
295 return -1;
296 if (!yaml_emitter_emit(emitter, &event))
297 return -1;
299 for (i = 0; i < n_type; ++i)
300 if (emit_type(emitter, types[i]) < 0)
301 return -1;
303 if (!yaml_sequence_end_event_initialize(&event))
304 return -1;
305 if (!yaml_emitter_emit(emitter, &event))
306 return -1;
308 return 0;
311 static int emit_array(yaml_emitter_t *emitter, struct pet_array *array)
313 yaml_event_t event;
315 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
316 YAML_BLOCK_MAPPING_STYLE))
317 return -1;
318 if (!yaml_emitter_emit(emitter, &event))
319 return -1;
321 if (emit_string(emitter, "context") < 0)
322 return -1;
323 if (emit_set(emitter, array->context) < 0)
324 return -1;
326 if (emit_string(emitter, "extent") < 0)
327 return -1;
328 if (emit_set(emitter, array->extent) < 0)
329 return -1;
331 if (array->value_bounds) {
332 if (emit_string(emitter, "value_bounds") < 0)
333 return -1;
334 if (emit_set(emitter, array->value_bounds) < 0)
335 return -1;
338 if (emit_string(emitter, "element_type") < 0)
339 return -1;
340 if (emit_string(emitter, array->element_type) < 0)
341 return -1;
342 if (emit_named_int(emitter, "element_size", array->element_size) < 0)
343 return -1;
345 if (array->element_is_record)
346 if (emit_named_int(emitter, "element_is_record",
347 array->element_is_record) < 0)
348 return -1;
350 if (array->live_out) {
351 if (emit_string(emitter, "live_out") < 0)
352 return -1;
353 if (emit_string(emitter, "1") < 0)
354 return -1;
357 if (array->uniquely_defined) {
358 if (emit_string(emitter, "uniquely_defined") < 0)
359 return -1;
360 if (emit_string(emitter, "1") < 0)
361 return -1;
364 if (array->declared && emit_named_int(emitter, "declared", 1) < 0)
365 return -1;
366 if (array->exposed && emit_named_int(emitter, "exposed", 1) < 0)
367 return -1;
369 if (!yaml_mapping_end_event_initialize(&event))
370 return -1;
371 if (!yaml_emitter_emit(emitter, &event))
372 return -1;
374 return 0;
377 static int emit_arrays(yaml_emitter_t *emitter, int n_array,
378 struct pet_array **arrays)
380 int i;
381 yaml_event_t event;
383 if (emit_string(emitter, "arrays") < 0)
384 return -1;
385 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
386 YAML_BLOCK_SEQUENCE_STYLE))
387 return -1;
388 if (!yaml_emitter_emit(emitter, &event))
389 return -1;
391 for (i = 0; i < n_array; ++i)
392 if (emit_array(emitter, arrays[i]) < 0)
393 return -1;
395 if (!yaml_sequence_end_event_initialize(&event))
396 return -1;
397 if (!yaml_emitter_emit(emitter, &event))
398 return -1;
400 return 0;
403 static int emit_expr_type(yaml_emitter_t *emitter, enum pet_expr_type type)
405 if (emit_string(emitter, pet_type_str(type)) < 0)
406 return -1;
407 return 0;
410 static int emit_expr(yaml_emitter_t *emitter, __isl_keep pet_expr *expr)
412 yaml_event_t event;
414 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
415 YAML_BLOCK_MAPPING_STYLE))
416 return -1;
417 if (!yaml_emitter_emit(emitter, &event))
418 return -1;
420 if (emit_string(emitter, "type") < 0)
421 return -1;
422 if (emit_expr_type(emitter, expr->type) < 0)
423 return -1;
425 switch (expr->type) {
426 case pet_expr_error:
427 return -1;
428 case pet_expr_int:
429 if (emit_named_val(emitter, "value", expr->i) < 0)
430 return -1;
431 break;
432 case pet_expr_double:
433 if (emit_string(emitter, "value") < 0)
434 return -1;
435 if (emit_double(emitter, expr->d.val) < 0)
436 return -1;
437 if (emit_string(emitter, "string") < 0)
438 return -1;
439 if (emit_string(emitter, expr->d.s) < 0)
440 return -1;
441 break;
442 case pet_expr_access:
443 if (emit_string(emitter, "relation") < 0)
444 return -1;
445 if (emit_map(emitter, expr->acc.access) < 0)
446 return -1;
447 if (emit_named_multi_pw_aff(emitter,
448 "index", expr->acc.index) < 0)
449 return -1;
450 if (expr->acc.ref_id &&
451 emit_named_id(emitter, "reference", expr->acc.ref_id) < 0)
452 return -1;
453 if (emit_string(emitter, "read") < 0)
454 return -1;
455 if (emit_int(emitter, expr->acc.read) < 0)
456 return -1;
457 if (emit_string(emitter, "write") < 0)
458 return -1;
459 if (emit_int(emitter, expr->acc.write) < 0)
460 return -1;
461 break;
462 case pet_expr_op:
463 if (emit_string(emitter, "operation") < 0)
464 return -1;
465 if (emit_string(emitter, pet_op_str(expr->op)) < 0)
466 return -1;
467 break;
468 case pet_expr_call:
469 if (emit_string(emitter, "name") < 0)
470 return -1;
471 if (emit_string(emitter, expr->name) < 0)
472 return -1;
473 break;
474 case pet_expr_cast:
475 if (emit_string(emitter, "type_name") < 0)
476 return -1;
477 if (emit_string(emitter, expr->type_name) < 0)
478 return -1;
479 break;
482 if (expr->n_arg > 0) {
483 int i;
485 if (emit_string(emitter, "arguments") < 0)
486 return -1;
487 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
488 YAML_BLOCK_SEQUENCE_STYLE))
489 return -1;
490 if (!yaml_emitter_emit(emitter, &event))
491 return -1;
493 for (i = 0; i < expr->n_arg; ++i)
494 if (emit_expr(emitter, expr->args[i]) < 0)
495 return -1;
497 if (!yaml_sequence_end_event_initialize(&event))
498 return -1;
499 if (!yaml_emitter_emit(emitter, &event))
500 return -1;
503 if (!yaml_mapping_end_event_initialize(&event))
504 return -1;
505 if (!yaml_emitter_emit(emitter, &event))
506 return -1;
508 return 0;
511 /* Print the string "name" and the expression "expr" to "emitter".
513 static int emit_named_expr(yaml_emitter_t *emitter, const char *name,
514 __isl_keep pet_expr *expr)
516 if (emit_string(emitter, name) < 0)
517 return -1;
518 if (emit_expr(emitter, expr) < 0)
519 return -1;
520 return 0;
523 /* Print "type" to "emitter".
525 static int emit_tree_type(yaml_emitter_t *emitter, enum pet_tree_type type)
527 if (emit_string(emitter, pet_tree_type_str(type)) < 0)
528 return -1;
529 return 0;
532 /* Recursively print "tree" to "emitter".
534 static int emit_tree(yaml_emitter_t *emitter, __isl_keep pet_tree *tree)
536 yaml_event_t event;
537 int i;
539 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
540 YAML_BLOCK_MAPPING_STYLE))
541 return -1;
542 if (!yaml_emitter_emit(emitter, &event))
543 return -1;
545 if (emit_string(emitter, "type") < 0)
546 return -1;
547 if (emit_tree_type(emitter, tree->type) < 0)
548 return -1;
550 switch (tree->type) {
551 case pet_tree_error:
552 return -1;
553 case pet_tree_block:
554 if (emit_named_int(emitter, "block", tree->u.b.block) < 0)
555 return -1;
556 if (tree->u.b.n == 0)
557 break;
559 if (emit_string(emitter, "children") < 0)
560 return -1;
561 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
562 YAML_BLOCK_SEQUENCE_STYLE))
563 return -1;
564 if (!yaml_emitter_emit(emitter, &event))
565 return -1;
567 for (i = 0; i < tree->u.b.n; ++i)
568 if (emit_tree(emitter, tree->u.b.child[i]) < 0)
569 return -1;
571 if (!yaml_sequence_end_event_initialize(&event))
572 return -1;
573 if (!yaml_emitter_emit(emitter, &event))
574 return -1;
575 break;
576 case pet_tree_break:
577 case pet_tree_continue:
578 break;
579 case pet_tree_decl:
580 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
581 return -1;
582 break;
583 case pet_tree_decl_init:
584 if (emit_named_expr(emitter, "variable", tree->u.d.var) < 0)
585 return -1;
586 if (emit_named_expr(emitter,
587 "initialization", tree->u.d.init) < 0)
588 return -1;
589 break;
590 case pet_tree_expr:
591 if (emit_named_expr(emitter, "expr", tree->u.e.expr) < 0)
592 return -1;
593 break;
594 case pet_tree_for:
595 if (emit_named_int(emitter, "declared", tree->u.l.declared) < 0)
596 return -1;
597 if (emit_named_expr(emitter, "variable", tree->u.l.iv) < 0)
598 return -1;
599 if (emit_named_expr(emitter,
600 "initialization", tree->u.l.init) < 0)
601 return -1;
602 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
603 return -1;
604 if (emit_named_expr(emitter, "increment", tree->u.l.inc) < 0)
605 return -1;
606 if (emit_string(emitter, "body") < 0)
607 return -1;
608 if (emit_tree(emitter, tree->u.l.body) < 0)
609 return -1;
610 break;
611 case pet_tree_while:
612 if (emit_named_expr(emitter, "condition", tree->u.l.cond) < 0)
613 return -1;
614 if (emit_string(emitter, "body") < 0)
615 return -1;
616 if (emit_tree(emitter, tree->u.l.body) < 0)
617 return -1;
618 break;
619 case pet_tree_infinite_loop:
620 if (emit_string(emitter, "body") < 0)
621 return -1;
622 if (emit_tree(emitter, tree->u.l.body) < 0)
623 return -1;
624 break;
625 case pet_tree_if:
626 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
627 return -1;
628 if (emit_string(emitter, "then") < 0)
629 return -1;
630 if (emit_tree(emitter, tree->u.i.then_body) < 0)
631 return -1;
632 break;
633 case pet_tree_if_else:
634 if (emit_named_expr(emitter, "condition", tree->u.i.cond) < 0)
635 return -1;
636 if (emit_string(emitter, "then") < 0)
637 return -1;
638 if (emit_tree(emitter, tree->u.i.then_body) < 0)
639 return -1;
640 if (emit_string(emitter, "else") < 0)
641 return -1;
642 if (emit_tree(emitter, tree->u.i.else_body) < 0)
643 return -1;
644 break;
647 if (!yaml_mapping_end_event_initialize(&event))
648 return -1;
649 if (!yaml_emitter_emit(emitter, &event))
650 return -1;
652 return 0;
655 static int emit_stmt(yaml_emitter_t *emitter, struct pet_stmt *stmt)
657 yaml_event_t event;
659 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
660 YAML_BLOCK_MAPPING_STYLE))
661 return -1;
662 if (!yaml_emitter_emit(emitter, &event))
663 return -1;
665 if (emit_string(emitter, "line") < 0)
666 return -1;
667 if (emit_int(emitter, pet_loc_get_line(stmt->loc)) < 0)
668 return -1;
670 if (emit_string(emitter, "domain") < 0)
671 return -1;
672 if (emit_set(emitter, stmt->domain) < 0)
673 return -1;
675 if (emit_string(emitter, "schedule") < 0)
676 return -1;
677 if (emit_map(emitter, stmt->schedule) < 0)
678 return -1;
680 if (emit_string(emitter, "body") < 0)
681 return -1;
682 if (emit_tree(emitter, stmt->body) < 0)
683 return -1;
685 if (stmt->n_arg > 0) {
686 int i;
688 if (emit_string(emitter, "arguments") < 0)
689 return -1;
690 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
691 YAML_BLOCK_SEQUENCE_STYLE))
692 return -1;
693 if (!yaml_emitter_emit(emitter, &event))
694 return -1;
696 for (i = 0; i < stmt->n_arg; ++i)
697 if (emit_expr(emitter, stmt->args[i]) < 0)
698 return -1;
700 if (!yaml_sequence_end_event_initialize(&event))
701 return -1;
702 if (!yaml_emitter_emit(emitter, &event))
703 return -1;
706 if (!yaml_mapping_end_event_initialize(&event))
707 return -1;
708 if (!yaml_emitter_emit(emitter, &event))
709 return -1;
711 return 0;
714 static int emit_statements(yaml_emitter_t *emitter, int n_stmt,
715 struct pet_stmt **stmts)
717 int i;
718 yaml_event_t event;
720 if (emit_string(emitter, "statements") < 0)
721 return -1;
722 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
723 YAML_BLOCK_SEQUENCE_STYLE))
724 return -1;
725 if (!yaml_emitter_emit(emitter, &event))
726 return -1;
728 for (i = 0; i < n_stmt; ++i)
729 if (emit_stmt(emitter, stmts[i]) < 0)
730 return -1;
732 if (!yaml_sequence_end_event_initialize(&event))
733 return -1;
734 if (!yaml_emitter_emit(emitter, &event))
735 return -1;
737 return 0;
740 /* Print "implication" to "emitter".
742 static int emit_implication(yaml_emitter_t *emitter,
743 struct pet_implication *implication)
745 yaml_event_t event;
747 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
748 YAML_BLOCK_MAPPING_STYLE))
749 return -1;
750 if (!yaml_emitter_emit(emitter, &event))
751 return -1;
753 if (emit_named_int(emitter, "satisfied", implication->satisfied) < 0)
754 return -1;
756 if (emit_named_map(emitter, "extension", implication->extension) < 0)
757 return -1;
759 if (!yaml_mapping_end_event_initialize(&event))
760 return -1;
761 if (!yaml_emitter_emit(emitter, &event))
762 return -1;
764 return 0;
767 /* Print the list of "n_implication" "implications", if any, to "emitter".
769 static int emit_implications(yaml_emitter_t *emitter, int n_implication,
770 struct pet_implication **implications)
772 int i;
773 yaml_event_t event;
775 if (n_implication == 0)
776 return 0;
778 if (emit_string(emitter, "implications") < 0)
779 return -1;
780 if (!yaml_sequence_start_event_initialize(&event, NULL, NULL, 1,
781 YAML_BLOCK_SEQUENCE_STYLE))
782 return -1;
783 if (!yaml_emitter_emit(emitter, &event))
784 return -1;
786 for (i = 0; i < n_implication; ++i)
787 if (emit_implication(emitter, implications[i]) < 0)
788 return -1;
790 if (!yaml_sequence_end_event_initialize(&event))
791 return -1;
792 if (!yaml_emitter_emit(emitter, &event))
793 return -1;
795 return 0;
798 static int emit_scop(yaml_emitter_t *emitter, struct pet_scop *scop)
800 yaml_event_t event;
802 if (!yaml_mapping_start_event_initialize(&event, NULL, NULL, 1,
803 YAML_BLOCK_MAPPING_STYLE))
804 return -1;
805 if (!yaml_emitter_emit(emitter, &event))
806 return -1;
808 if (emit_named_unsigned(emitter,
809 "start", pet_loc_get_start(scop->loc)) < 0)
810 return -1;
811 if (emit_named_unsigned(emitter, "end", pet_loc_get_end(scop->loc)) < 0)
812 return -1;
813 if (emit_named_string(emitter,
814 "indent", pet_loc_get_indent(scop->loc)) < 0)
815 return -1;
816 if (emit_string(emitter, "context") < 0)
817 return -1;
818 if (emit_set(emitter, scop->context) < 0)
819 return -1;
820 if (!isl_set_plain_is_universe(scop->context_value) &&
821 emit_named_set(emitter, "context_value", scop->context_value) < 0)
822 return -1;
824 if (emit_types(emitter, scop->n_type, scop->types) < 0)
825 return -1;
826 if (emit_arrays(emitter, scop->n_array, scop->arrays) < 0)
827 return -1;
829 if (emit_statements(emitter, scop->n_stmt, scop->stmts) < 0)
830 return -1;
832 if (emit_implications(emitter, scop->n_implication,
833 scop->implications) < 0)
834 return -1;
836 if (!yaml_mapping_end_event_initialize(&event))
837 return -1;
838 if (!yaml_emitter_emit(emitter, &event))
839 return -1;
841 return 0;
844 /* Print a YAML serialization of "scop" to "out".
846 int pet_scop_emit(FILE *out, struct pet_scop *scop)
848 yaml_emitter_t emitter;
849 yaml_event_t event;
851 yaml_emitter_initialize(&emitter);
853 yaml_emitter_set_output_file(&emitter, out);
855 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
856 if (!yaml_emitter_emit(&emitter, &event))
857 goto error;
859 if (!yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1))
860 goto error;
861 if (!yaml_emitter_emit(&emitter, &event))
862 goto error;
864 if (emit_scop(&emitter, scop) < 0)
865 goto error;
867 if (!yaml_document_end_event_initialize(&event, 1))
868 goto error;
869 if (!yaml_emitter_emit(&emitter, &event))
870 goto error;
872 yaml_stream_end_event_initialize(&event);
873 if (!yaml_emitter_emit(&emitter, &event))
874 goto error;
876 yaml_emitter_delete(&emitter);
877 return 0;
878 error:
879 yaml_emitter_delete(&emitter);
880 return -1;