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