cpp_conversion.cc revision 1.1 1 1.1 mrg /*
2 1.1 mrg * Copyright 2018 Sven Verdoolaege
3 1.1 mrg *
4 1.1 mrg * Use of this software is governed by the MIT license
5 1.1 mrg *
6 1.1 mrg * Written by Sven Verdoolaege.
7 1.1 mrg */
8 1.1 mrg
9 1.1 mrg #include <stdio.h>
10 1.1 mrg #include <map>
11 1.1 mrg #include <string>
12 1.1 mrg
13 1.1 mrg #include "cpp.h"
14 1.1 mrg #include "cpp_conversion.h"
15 1.1 mrg
16 1.1 mrg /* If "clazz" describes a subclass of a C type, then print code
17 1.1 mrg * for converting an object of the class derived from the C type
18 1.1 mrg * to the subclass. Do this by first converting this class
19 1.1 mrg * to the immediate superclass of the subclass and then converting
20 1.1 mrg * from this superclass to the subclass.
21 1.1 mrg */
22 1.1 mrg void cpp_conversion_generator::cast(const isl_class &clazz, const char *to)
23 1.1 mrg {
24 1.1 mrg string name = cpp_generator::type2cpp(clazz);
25 1.1 mrg
26 1.1 mrg if (!clazz.is_type_subclass())
27 1.1 mrg return;
28 1.1 mrg
29 1.1 mrg cast(classes[clazz.superclass_name], to);
30 1.1 mrg printf(".as<%s%s>()", to, name.c_str());
31 1.1 mrg }
32 1.1 mrg
33 1.1 mrg /* Print a function called "function" for converting objects of
34 1.1 mrg * "clazz" from the "from" bindings to the "to" bindings.
35 1.1 mrg * If "clazz" describes a subclass of a C type, then the result
36 1.1 mrg * of the conversion between bindings is derived from the C type and
37 1.1 mrg * needs to be converted back to the subclass.
38 1.1 mrg */
39 1.1 mrg void cpp_conversion_generator::convert(const isl_class &clazz,
40 1.1 mrg const char *from, const char *to, const char *function)
41 1.1 mrg {
42 1.1 mrg string name = cpp_generator::type2cpp(clazz);
43 1.1 mrg
44 1.1 mrg printf("%s%s %s(%s%s obj) {\n",
45 1.1 mrg to, name.c_str(), function, from, name.c_str());
46 1.1 mrg printf("\t""return %s""manage(obj.copy())", to);
47 1.1 mrg cast(clazz, to);
48 1.1 mrg printf(";\n");
49 1.1 mrg printf("}\n");
50 1.1 mrg printf("\n");
51 1.1 mrg }
52 1.1 mrg
53 1.1 mrg /* Print functions for converting objects of "clazz"
54 1.1 mrg * between the default and the checked C++ bindings.
55 1.1 mrg *
56 1.1 mrg * The conversion from default to checked is called "check".
57 1.1 mrg * The inverse conversion is called "uncheck".
58 1.1 mrg * For example, to "set", the following two functions are generated:
59 1.1 mrg *
60 1.1 mrg * checked::set check(set obj) {
61 1.1 mrg * return checked::manage(obj.copy());
62 1.1 mrg * }
63 1.1 mrg *
64 1.1 mrg * set uncheck(checked::set obj) {
65 1.1 mrg * return manage(obj.copy());
66 1.1 mrg * }
67 1.1 mrg */
68 1.1 mrg void cpp_conversion_generator::print(const isl_class &clazz)
69 1.1 mrg {
70 1.1 mrg convert(clazz, "", "checked::", "check");
71 1.1 mrg convert(clazz, "checked::", "", "uncheck");
72 1.1 mrg }
73 1.1 mrg
74 1.1 mrg /* Generate conversion functions for converting objects between
75 1.1 mrg * the default and the checked C++ bindings.
76 1.1 mrg * Do this for each exported class.
77 1.1 mrg */
78 1.1 mrg void cpp_conversion_generator::generate()
79 1.1 mrg {
80 1.1 mrg map<string, isl_class>::iterator ci;
81 1.1 mrg
82 1.1 mrg printf("namespace isl {\n\n");
83 1.1 mrg for (ci = classes.begin(); ci != classes.end(); ++ci)
84 1.1 mrg print(ci->second);
85 1.1 mrg printf("} // namespace isl\n");
86 1.1 mrg }
87