interface/cpp.cc: cpp_generator::print_method_impl: drop unused variable
[isl.git] / interface / cpp.cc
blobb8a62b823e9dc29f4bb812280bd1ba5f1ea500de
1 /*
2 * Copyright 2016, 2017 Tobias Grosser. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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 TOBIAS GROSSER ''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 SVEN VERDOOLAEGE 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 * Tobias Grosser.
34 #include <cstdarg>
35 #include <cstdio>
36 #include <iostream>
37 #include <map>
38 #include <sstream>
39 #include <string>
40 #include <vector>
42 #include "cpp.h"
43 #include "isl_config.h"
45 /* Print string formatted according to "fmt" to ostream "os".
47 * This osprintf method allows us to use printf style formatting constructs when
48 * writing to an ostream.
50 static void osprintf(ostream &os, const char *format, ...)
52 va_list arguments;
53 char *string_pointer;
54 size_t size;
56 va_start(arguments, format);
57 size = vsnprintf(NULL, 0, format, arguments);
58 string_pointer = new char[size + 1];
59 va_end(arguments);
60 va_start(arguments, format);
61 vsnprintf(string_pointer, size + 1, format, arguments);
62 va_end(arguments);
63 os << string_pointer;
64 delete[] string_pointer;
67 /* Convert "l" to a string.
69 static std::string to_string(long l)
71 std::ostringstream strm;
72 strm << l;
73 return strm.str();
76 /* Generate a cpp interface based on the extracted types and functions.
78 * Print first a set of forward declarations for all isl wrapper
79 * classes, then the declarations of the classes, and at the end all
80 * implementations.
82 void cpp_generator::generate()
84 ostream &os = cout;
86 osprintf(os, "\n");
87 osprintf(os, "namespace isl {\n\n");
88 osprintf(os, "inline namespace noexceptions {\n\n");
90 print_forward_declarations(os);
91 osprintf(os, "\n");
92 print_declarations(os);
93 osprintf(os, "\n");
94 print_implementations(os);
96 osprintf(os, "} // namespace noexceptions\n");
97 osprintf(os, "} // namespace isl\n");
100 /* Print forward declarations for all classes to "os".
102 void cpp_generator::print_forward_declarations(ostream &os)
104 map<string, isl_class>::iterator ci;
106 osprintf(os, "// forward declarations\n");
108 for (ci = classes.begin(); ci != classes.end(); ++ci)
109 print_class_forward_decl(os, ci->second);
112 /* Print all declarations to "os".
114 void cpp_generator::print_declarations(ostream &os)
116 map<string, isl_class>::iterator ci;
117 bool first = true;
119 for (ci = classes.begin(); ci != classes.end(); ++ci) {
120 if (first)
121 first = false;
122 else
123 osprintf(os, "\n");
125 print_class(os, ci->second);
129 /* Print all implementations to "os".
131 void cpp_generator::print_implementations(ostream &os)
133 map<string, isl_class>::iterator ci;
134 bool first = true;
136 for (ci = classes.begin(); ci != classes.end(); ++ci) {
137 if (first)
138 first = false;
139 else
140 osprintf(os, "\n");
142 print_class_impl(os, ci->second);
146 /* Print declarations for class "clazz" to "os".
148 void cpp_generator::print_class(ostream &os, const isl_class &clazz)
150 const char *name = clazz.name.c_str();
151 std::string cppstring = type2cpp(clazz);
152 const char *cppname = cppstring.c_str();
154 osprintf(os, "// declarations for isl::%s\n", cppname);
156 print_class_factory_decl(os, clazz);
157 osprintf(os, "\n");
158 osprintf(os, "class %s {\n", cppname);
159 print_class_factory_decl(os, clazz, " friend ");
160 osprintf(os, "\n");
161 osprintf(os, " %s *ptr = nullptr;\n", name);
162 osprintf(os, "\n");
163 print_private_constructors_decl(os, clazz);
164 osprintf(os, "\n");
165 osprintf(os, "public:\n");
166 print_public_constructors_decl(os, clazz);
167 print_constructors_decl(os, clazz);
168 print_copy_assignment_decl(os, clazz);
169 print_destructor_decl(os, clazz);
170 print_ptr_decl(os, clazz);
171 print_get_ctx_decl(os);
172 osprintf(os, "\n");
173 print_methods_decl(os, clazz);
175 osprintf(os, "};\n");
178 /* Print forward declaration of class "clazz" to "os".
180 void cpp_generator::print_class_forward_decl(ostream &os,
181 const isl_class &clazz)
183 std::string cppstring = type2cpp(clazz);
184 const char *cppname = cppstring.c_str();
186 osprintf(os, "class %s;\n", cppname);
189 /* Print global factory functions to "os".
191 * Each class has two global factory functions:
193 * isl::set manage(__isl_take isl_set *ptr);
194 * isl::set manage_copy(__isl_keep isl_set *ptr);
196 * A user can construct isl C++ objects from a raw pointer and indicate whether
197 * they intend to take the ownership of the object or not through these global
198 * factory functions. This ensures isl object creation is very explicit and
199 * pointers are not converted by accident. Thanks to overloading, manage() and
200 * manage_copy() can be called on any isl raw pointer and the corresponding
201 * object is automatically created, without the user having to choose the right
202 * isl object type.
204 void cpp_generator::print_class_factory_decl(ostream &os,
205 const isl_class &clazz, const std::string &prefix)
207 const char *name = clazz.name.c_str();
208 std::string cppstring = type2cpp(clazz);
209 const char *cppname = cppstring.c_str();
211 os << prefix;
212 osprintf(os, "inline isl::%s manage(__isl_take %s *ptr);\n", cppname,
213 name);
214 os << prefix;
215 osprintf(os, "inline isl::%s manage_copy(__isl_keep %s *ptr);\n",
216 cppname, name);
219 /* Print declarations of private constructors for class "clazz" to "os".
221 * Each class has currently one private constructor:
223 * 1) Constructor from a plain isl_* C pointer
225 * Example:
227 * set(__isl_take isl_set *ptr);
229 * The raw pointer constructor is kept private. Object creation is only
230 * possible through isl::manage() or isl::manage_copy().
232 void cpp_generator::print_private_constructors_decl(ostream &os,
233 const isl_class &clazz)
235 const char *name = clazz.name.c_str();
236 std::string cppstring = type2cpp(clazz);
237 const char *cppname = cppstring.c_str();
239 osprintf(os, " inline explicit %s(__isl_take %s *ptr);\n", cppname,
240 name);
243 /* Print declarations of public constructors for class "clazz" to "os".
245 * Each class currently has two public constructors:
247 * 1) A default constructor
248 * 2) A copy constructor
250 * Example:
252 * set();
253 * set(const isl::set &set);
255 void cpp_generator::print_public_constructors_decl(ostream &os,
256 const isl_class &clazz)
258 std::string cppstring = type2cpp(clazz);
259 const char *cppname = cppstring.c_str();
260 osprintf(os, " inline /* implicit */ %s();\n", cppname);
262 osprintf(os, " inline /* implicit */ %s(const isl::%s &obj);\n",
263 cppname, cppname);
266 /* Print declarations for constructors for class "class" to "os".
268 * For each isl function that is marked as __isl_constructor,
269 * add a corresponding C++ constructor.
271 * Example:
273 * inline /\* implicit *\/ union_set(isl::basic_set bset);
274 * inline /\* implicit *\/ union_set(isl::set set);
275 * inline explicit val(isl::ctx ctx, long i);
276 * inline explicit val(isl::ctx ctx, const std::string &str);
278 void cpp_generator::print_constructors_decl(ostream &os,
279 const isl_class &clazz)
281 set<FunctionDecl *>::const_iterator in;
282 const set<FunctionDecl *> &constructors = clazz.constructors;
284 for (in = constructors.begin(); in != constructors.end(); ++in) {
285 FunctionDecl *cons = *in;
286 string fullname = cons->getName();
287 function_kind kind = function_kind_constructor;
289 print_method_decl(os, clazz, fullname, cons, kind);
293 /* Print declarations of copy assignment operator for class "clazz"
294 * to "os".
296 * Each class has one assignment operator.
298 * isl:set &set::operator=(isl::set obj)
301 void cpp_generator::print_copy_assignment_decl(ostream &os,
302 const isl_class &clazz)
304 std::string cppstring = type2cpp(clazz);
305 const char *cppname = cppstring.c_str();
307 osprintf(os, " inline isl::%s &operator=(isl::%s obj);\n", cppname,
308 cppname);
311 /* Print declaration of destructor for class "clazz" to "os".
313 void cpp_generator::print_destructor_decl(ostream &os, const isl_class &clazz)
315 std::string cppstring = type2cpp(clazz);
316 const char *cppname = cppstring.c_str();
318 osprintf(os, " inline ~%s();\n", cppname);
321 /* Print declaration of pointer functions for class "clazz" to "os".
323 * To obtain a raw pointer three functions are provided:
325 * 1) __isl_give isl_set *copy()
327 * Returns a pointer to a _copy_ of the internal object
329 * 2) __isl_keep isl_set *get()
331 * Returns a pointer to the internal object
333 * 3) __isl_give isl_set *release()
335 * Returns a pointer to the internal object and resets the
336 * internal pointer to nullptr.
338 * We also provide functionality to explicitly check if a pointer is
339 * currently managed by this object.
341 * 4) bool is_null()
343 * Check if the current object is a null pointer.
345 * The functions get() and release() model the value_ptr proposed in
346 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339.pdf.
347 * The copy() function is an extension to allow the user to explicitly
348 * copy the underlying object.
350 * Also generate a declaration to delete copy() for r-values, for
351 * r-values release() should be used to avoid unnecessary copies.
353 void cpp_generator::print_ptr_decl(ostream &os, const isl_class &clazz)
355 const char *name = clazz.name.c_str();
357 osprintf(os, " inline __isl_give %s *copy() const &;\n", name);
358 osprintf(os, " inline __isl_give %s *copy() && = delete;\n", name);
359 osprintf(os, " inline __isl_keep %s *get() const;\n", name);
360 osprintf(os, " inline __isl_give %s *release();\n", name);
361 osprintf(os, " inline bool is_null() const;\n");
364 /* Print the declaration of the get_ctx method.
366 void cpp_generator::print_get_ctx_decl(ostream &os)
368 osprintf(os, " inline isl::ctx get_ctx() const;\n");
371 /* Print declarations for methods in class "clazz" to "os".
373 void cpp_generator::print_methods_decl(ostream &os, const isl_class &clazz)
375 map<string, set<FunctionDecl *> >::const_iterator it;
377 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
378 print_method_group_decl(os, clazz, it->first, it->second);
381 /* Print declarations for methods "methods" of name "fullname" in class "clazz"
382 * to "os".
384 * "fullname" is the name of the generated C++ method. It commonly corresponds
385 * to the isl name, with the object type prefix dropped.
386 * In case of overloaded methods, the result type suffix has also been removed.
388 void cpp_generator::print_method_group_decl(ostream &os, const isl_class &clazz,
389 const string &fullname, const set<FunctionDecl *> &methods)
391 set<FunctionDecl *>::const_iterator it;
393 for (it = methods.begin(); it != methods.end(); ++it) {
394 function_kind kind = get_method_kind(clazz, *it);
395 print_method_decl(os, clazz, fullname, *it, kind);
399 /* Print declarations for "method" in class "clazz" to "os".
401 * "fullname" is the name of the generated C++ method. It commonly corresponds
402 * to the isl name, with the object type prefix dropped.
403 * In case of overloaded methods, the result type suffix has also been removed.
405 * "kind" specifies the kind of method that should be generated.
407 void cpp_generator::print_method_decl(ostream &os, const isl_class &clazz,
408 const string &fullname, FunctionDecl *method, function_kind kind)
410 print_method_header(os, clazz, method, fullname, true, kind);
413 /* Print implementations for class "clazz" to "os".
415 void cpp_generator::print_class_impl(ostream &os, const isl_class &clazz)
417 std::string cppstring = type2cpp(clazz);
418 const char *cppname = cppstring.c_str();
420 osprintf(os, "// implementations for isl::%s\n", cppname);
422 print_class_factory_impl(os, clazz);
423 osprintf(os, "\n");
424 print_public_constructors_impl(os, clazz);
425 osprintf(os, "\n");
426 print_private_constructors_impl(os, clazz);
427 osprintf(os, "\n");
428 print_constructors_impl(os, clazz);
429 osprintf(os, "\n");
430 print_copy_assignment_impl(os, clazz);
431 osprintf(os, "\n");
432 print_destructor_impl(os, clazz);
433 osprintf(os, "\n");
434 print_ptr_impl(os, clazz);
435 osprintf(os, "\n");
436 print_get_ctx_impl(os, clazz);
437 osprintf(os, "\n");
438 print_methods_impl(os, clazz);
441 /* Print implementation of global factory functions to "os".
443 void cpp_generator::print_class_factory_impl(ostream &os,
444 const isl_class &clazz)
446 const char *name = clazz.name.c_str();
447 std::string cppstring = type2cpp(clazz);
448 const char *cppname = cppstring.c_str();
450 osprintf(os, "isl::%s manage(__isl_take %s *ptr) {\n", cppname, name);
451 osprintf(os, " return %s(ptr);\n", cppname);
452 osprintf(os, "}\n");
454 osprintf(os, "isl::%s manage_copy(__isl_keep %s *ptr) {\n", cppname,
455 name);
456 osprintf(os, " return %s(%s_copy(ptr));\n", cppname, name);
457 osprintf(os, "}\n");
460 /* Print implementations of private constructors for class "clazz" to "os".
462 void cpp_generator::print_private_constructors_impl(ostream &os,
463 const isl_class &clazz)
465 const char *name = clazz.name.c_str();
466 std::string cppstring = type2cpp(clazz);
467 const char *cppname = cppstring.c_str();
469 osprintf(os, "%s::%s(__isl_take %s *ptr)\n : ptr(ptr) {}\n",
470 cppname, cppname, name);
473 /* Print implementations of public constructors for class "clazz" to "os".
475 void cpp_generator::print_public_constructors_impl(ostream &os,
476 const isl_class &clazz)
478 std::string cppstring = type2cpp(clazz);
479 const char *cppname = cppstring.c_str();
481 osprintf(os, "%s::%s()\n : ptr(nullptr) {}\n\n", cppname, cppname);
482 osprintf(os, "%s::%s(const isl::%s &obj)\n : ptr(obj.copy()) {}\n",
483 cppname, cppname, cppname);
486 /* Print implementations of constructors for class "clazz" to "os".
488 void cpp_generator::print_constructors_impl(ostream &os,
489 const isl_class &clazz)
491 set<FunctionDecl *>::const_iterator in;
492 const set<FunctionDecl *> constructors = clazz.constructors;
494 for (in = constructors.begin(); in != constructors.end(); ++in) {
495 FunctionDecl *cons = *in;
496 string fullname = cons->getName();
497 function_kind kind = function_kind_constructor;
499 print_method_impl(os, clazz, fullname, cons, kind);
503 /* Print implementation of copy assignment operator for class "clazz" to "os".
505 void cpp_generator::print_copy_assignment_impl(ostream &os,
506 const isl_class &clazz)
508 const char *name = clazz.name.c_str();
509 std::string cppstring = type2cpp(clazz);
510 const char *cppname = cppstring.c_str();
512 osprintf(os, "%s &%s::operator=(isl::%s obj) {\n", cppname,
513 cppname, cppname);
514 osprintf(os, " std::swap(this->ptr, obj.ptr);\n", name);
515 osprintf(os, " return *this;\n");
516 osprintf(os, "}\n");
519 /* Print implementation of destructor for class "clazz" to "os".
521 void cpp_generator::print_destructor_impl(ostream &os,
522 const isl_class &clazz)
524 const char *name = clazz.name.c_str();
525 std::string cppstring = type2cpp(clazz);
526 const char *cppname = cppstring.c_str();
528 osprintf(os, "%s::~%s() {\n", cppname, cppname);
529 osprintf(os, " if (ptr)\n");
530 osprintf(os, " %s_free(ptr);\n", name);
531 osprintf(os, "}\n");
534 /* Print implementation of ptr() functions for class "clazz" to "os".
536 void cpp_generator::print_ptr_impl(ostream &os, const isl_class &clazz)
538 const char *name = clazz.name.c_str();
539 std::string cppstring = type2cpp(clazz);
540 const char *cppname = cppstring.c_str();
542 osprintf(os, "__isl_give %s *%s::copy() const & {\n", name, cppname);
543 osprintf(os, " return %s_copy(ptr);\n", name);
544 osprintf(os, "}\n\n");
545 osprintf(os, "__isl_keep %s *%s::get() const {\n", name, cppname);
546 osprintf(os, " return ptr;\n");
547 osprintf(os, "}\n\n");
548 osprintf(os, "__isl_give %s *%s::release() {\n", name, cppname);
549 osprintf(os, " %s *tmp = ptr;\n", name);
550 osprintf(os, " ptr = nullptr;\n");
551 osprintf(os, " return tmp;\n");
552 osprintf(os, "}\n\n");
553 osprintf(os, "bool %s::is_null() const {\n", cppname);
554 osprintf(os, " return ptr == nullptr;\n");
555 osprintf(os, "}\n");
558 /* Print the implementation of the get_ctx method.
560 void cpp_generator::print_get_ctx_impl(ostream &os, const isl_class &clazz)
562 const char *name = clazz.name.c_str();
563 std::string cppstring = type2cpp(clazz);
564 const char *cppname = cppstring.c_str();
566 osprintf(os, "isl::ctx %s::get_ctx() const {\n", cppname);
567 osprintf(os, " return isl::ctx(%s_get_ctx(ptr));\n", name);
568 osprintf(os, "}\n");
571 /* Print definitions for methods of class "clazz" to "os".
573 void cpp_generator::print_methods_impl(ostream &os, const isl_class &clazz)
575 map<string, set<FunctionDecl *> >::const_iterator it;
576 bool first = true;
578 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it) {
579 if (first)
580 first = false;
581 else
582 osprintf(os, "\n");
583 print_method_group_impl(os, clazz, it->first, it->second);
587 /* Print definitions for methods "methods" of name "fullname" in class "clazz"
588 * to "os".
590 * "fullname" is the name of the generated C++ method. It commonly corresponds
591 * to the isl name, with the object type prefix dropped.
592 * In case of overloaded methods, the result type suffix has also been removed.
594 * "kind" specifies the kind of method that should be generated.
596 void cpp_generator::print_method_group_impl(ostream &os, const isl_class &clazz,
597 const string &fullname, const set<FunctionDecl *> &methods)
599 set<FunctionDecl *>::const_iterator it;
600 bool first = true;
602 for (it = methods.begin(); it != methods.end(); ++it) {
603 function_kind kind;
604 if (first)
605 first = false;
606 else
607 osprintf(os, "\n");
608 kind = get_method_kind(clazz, *it);
609 print_method_impl(os, clazz, fullname, *it, kind);
613 /* Print the use of "param" to "os".
615 * "load_from_this_ptr" specifies whether the parameter should be loaded from
616 * the this-ptr. In case a value is loaded from a this pointer, the original
617 * value must be preserved and must consequently be copied. Values that are
618 * loaded from parameters do not need to be preserved, as such values will
619 * already be copies of the actual parameters. It is consequently possible
620 * to directly take the pointer from these values, which saves
621 * an unnecessary copy.
623 * In case the parameter is a callback function, two parameters get printed,
624 * a wrapper for the callback function and a pointer to the actual
625 * callback function. The wrapper is expected to be available
626 * in a previously declared variable <name>_lambda, while
627 * the actual callback function is expected in <name>_p.
628 * The caller of this function must ensure that these variables exist.
630 void cpp_generator::print_method_param_use(ostream &os, ParmVarDecl *param,
631 bool load_from_this_ptr)
633 string name = param->getName().str();
634 const char *name_str = name.c_str();
635 QualType type = param->getOriginalType();
637 if (type->isIntegerType()) {
638 osprintf(os, "%s", name_str);
639 return;
642 if (is_string(type)) {
643 osprintf(os, "%s.c_str()", name_str);
644 return;
647 if (is_callback(type)) {
648 osprintf(os, "%s_lambda, ", name_str);
649 osprintf(os, "&%s_p", name_str);
650 return;
653 if (!load_from_this_ptr && !is_callback(type))
654 osprintf(os, "%s.", name_str);
656 if (keeps(param)) {
657 osprintf(os, "get()");
658 } else {
659 if (load_from_this_ptr)
660 osprintf(os, "copy()");
661 else
662 osprintf(os, "release()");
666 /* Print definition for "method" in class "clazz" to "os".
668 * "fullname" is the name of the generated C++ method. It commonly corresponds
669 * to the isl name, with the object type prefix dropped.
670 * In case of overloaded methods, the result type suffix has also been removed.
672 * "kind" specifies the kind of method that should be generated.
674 * This method distinguishes three kinds of methods: member methods, static
675 * methods, and constructors.
677 * Member methods call "method" by passing to the underlying isl function the
678 * isl object belonging to "this" as first argument and the remaining arguments
679 * as subsequent arguments. The result of the isl function is returned as a new
680 * object if the underlying isl function returns an isl_* ptr or an isl_bool
681 * value, as std::string if the isl function returns 'const char *', and as
682 * unmodified return value otherwise.
684 * Static methods call "method" by passing all arguments to the underlying isl
685 * function, as no this-pointer is available. The result is a newly managed
686 * isl C++ object.
688 * Constructors create a new object from a given set of input parameters. They
689 * do not return a value, but instead update the pointer stored inside the
690 * newly created object.
692 * If the method has a callback argument, we reduce the number of parameters
693 * that are exposed by one to hide the user pointer from the interface. On
694 * the C++ side no user pointer is needed, as arguments can be forwarded
695 * as part of the std::function argument which specifies the callback function.
697 void cpp_generator::print_method_impl(ostream &os, const isl_class &clazz,
698 const string &fullname, FunctionDecl *method, function_kind kind)
700 string methodname = method->getName();
701 int num_params = method->getNumParams();
702 QualType return_type = method->getReturnType();
703 string rettype_str = type2cpp(return_type);
704 bool has_callback = false;
706 print_method_header(os, clazz, method, fullname, false, kind);
708 for (int i = 0; i < num_params; ++i) {
709 ParmVarDecl *param = method->getParamDecl(i);
710 if (is_callback(param->getType())) {
711 has_callback = true;
712 num_params -= 1;
713 print_callback_local(os, param);
717 osprintf(os, " auto res = %s(", methodname.c_str());
719 for (int i = 0; i < num_params; ++i) {
720 ParmVarDecl *param = method->getParamDecl(i);
721 bool load_from_this_ptr = false;
723 if (i == 0 && kind == function_kind_member_method)
724 load_from_this_ptr = true;
726 print_method_param_use(os, param, load_from_this_ptr);
728 if (i != num_params - 1)
729 osprintf(os, ", ");
731 osprintf(os, ");\n");
733 if (kind == function_kind_constructor) {
734 osprintf(os, " ptr = res;\n");
735 } else if (is_isl_type(return_type) || is_isl_bool(return_type)) {
736 osprintf(os, " return manage(res);\n");
737 } else if (has_callback) {
738 osprintf(os, " return %s(res);\n", rettype_str.c_str());
739 } else if (is_string(return_type)) {
740 osprintf(os, " std::string tmp(res);\n");
741 if (gives(method))
742 osprintf(os, " free(res);\n");
743 osprintf(os, " return tmp;\n");
744 } else {
745 osprintf(os, " return res;\n");
748 osprintf(os, "}\n");
751 /* Print the header for "method" in class "clazz" to "os".
753 * Print the header of a declaration if "is_declaration" is set, otherwise print
754 * the header of a method definition.
756 * "fullname" is the name of the generated C++ method. It commonly corresponds
757 * to the isl name, with the object type prefix dropped.
758 * In case of overloaded methods, the result type suffix has also been removed.
760 * "kind" specifies the kind of method that should be generated.
762 * This function prints headers for member methods, static methods, and
763 * constructors, either for their declaration or definition.
765 * Member functions are declared as "const", as they do not change the current
766 * object, but instead create a new object. They always retrieve the first
767 * parameter of the original isl function from the this-pointer of the object,
768 * such that only starting at the second parameter the parameters of the
769 * original function become part of the method's interface.
771 * A function
773 * __isl_give isl_set *isl_set_intersect(__isl_take isl_set *s1,
774 * __isl_take isl_set *s2);
776 * is translated into:
778 * inline isl::set intersect(isl::set set2) const;
780 * For static functions and constructors all parameters of the original isl
781 * function are exposed.
783 * Parameters that are defined as __isl_keep or are of type string, are passed
784 * as const reference, which allows the compiler to optimize the parameter
785 * transfer.
787 * Constructors are marked as explicit using the C++ keyword 'explicit' or as
788 * implicit using a comment in place of the explicit keyword. By annotating
789 * implicit constructors with a comment, users of the interface are made
790 * aware of the potential danger that implicit construction is possible
791 * for these constructors, whereas without a comment not every user would
792 * know that implicit construction is allowed in absence of an explicit keyword.
794 void cpp_generator::print_method_header(ostream &os, const isl_class &clazz,
795 FunctionDecl *method, const string &fullname, bool is_declaration,
796 function_kind kind)
798 string cname = fullname.substr(clazz.name.length() + 1);
799 string rettype_str = type2cpp(method->getReturnType());
800 string classname = type2cpp(clazz);
801 int num_params = method->getNumParams();
802 int first_param = 0;
804 cname = rename_method(cname);
805 if (kind == function_kind_member_method)
806 first_param = 1;
808 if (is_declaration) {
809 osprintf(os, " ");
811 if (kind == function_kind_static_method)
812 osprintf(os, "static ");
814 osprintf(os, "inline ");
816 if (kind == function_kind_constructor) {
817 if (is_implicit_conversion(clazz, method))
818 osprintf(os, "/* implicit */ ");
819 else
820 osprintf(os, "explicit ");
824 if (kind != function_kind_constructor)
825 osprintf(os, "%s ", rettype_str.c_str());
827 if (!is_declaration)
828 osprintf(os, "%s::", classname.c_str());
830 if (kind != function_kind_constructor)
831 osprintf(os, "%s", cname.c_str());
832 else
833 osprintf(os, "%s", classname.c_str());
835 osprintf(os, "(");
837 for (int i = first_param; i < num_params; ++i) {
838 ParmVarDecl *param = method->getParamDecl(i);
839 QualType type = param->getOriginalType();
840 string cpptype = type2cpp(type);
842 if (is_callback(type))
843 num_params--;
845 if (keeps(param) || is_string(type) || is_callback(type))
846 osprintf(os, "const %s &%s", cpptype.c_str(),
847 param->getName().str().c_str());
848 else
849 osprintf(os, "%s %s", cpptype.c_str(),
850 param->getName().str().c_str());
852 if (i != num_params - 1)
853 osprintf(os, ", ");
856 osprintf(os, ")");
858 if (kind == function_kind_member_method)
859 osprintf(os, " const");
861 if (is_declaration)
862 osprintf(os, ";\n");
863 else
864 osprintf(os, " {\n");
867 /* Generate the list of argument types for a callback function of
868 * type "type". If "cpp" is set, then generate the C++ type list, otherwise
869 * the C type list.
871 * For a callback of type
873 * isl_stat (*)(__isl_take isl_map *map, void *user)
875 * the following C++ argument list is generated:
877 * isl::map
879 string cpp_generator::generate_callback_args(QualType type, bool cpp)
881 std::string type_str;
882 const FunctionProtoType *callback;
883 int num_params;
885 callback = type->getPointeeType()->getAs<FunctionProtoType>();
886 num_params = callback->getNumArgs();
887 if (cpp)
888 num_params--;
890 for (long i = 0; i < num_params; i++) {
891 QualType type = callback->getArgType(i);
893 if (cpp)
894 type_str += type2cpp(type);
895 else
896 type_str += type.getAsString();
898 if (!cpp)
899 type_str += "arg_" + ::to_string(i);
901 if (i != num_params - 1)
902 type_str += ", ";
905 return type_str;
908 /* Generate the full cpp type of a callback function of type "type".
910 * For a callback of type
912 * isl_stat (*)(__isl_take isl_map *map, void *user)
914 * the following type is generated:
916 * std::function<isl::stat(isl::map)>
918 string cpp_generator::generate_callback_type(QualType type)
920 std::string type_str;
921 const FunctionProtoType *callback = type->getPointeeType()->getAs<FunctionProtoType>();
922 QualType return_type = callback->getReturnType();
923 string rettype_str = type2cpp(return_type);
925 type_str = "std::function<";
926 type_str += rettype_str;
927 type_str += "(";
928 type_str += generate_callback_args(type, true);
929 type_str += ")>";
931 return type_str;
934 /* Print the local variables that are needed for a callback argument,
935 * in particular, print a lambda function that wraps the callback and
936 * a pointer to the actual C++ callback function.
938 * For a callback of the form
940 * isl_stat (*fn)(__isl_take isl_map *map, void *user)
942 * the following lambda function is generated:
944 * auto fn_lambda = [](isl_map *arg_0, void *arg_1) -> isl_stat {
945 * auto *func = *static_cast<const std::function<stat(map)> **>(arg_1);
946 * stat ret = (*func)(isl::manage(arg_0));
947 * return isl_stat(ret);
948 * };
950 * The pointer to the std::function C++ callback function is stored in fn_p.
951 * This std::function object represents the actual user
952 * callback function together with the locally captured state at the caller.
954 * The lambda function is expected to be used as a C callback function
955 * where the lambda itself is provided as the function pointer and
956 * where the user void pointer is a pointer to fn_p.
957 * The std::function object is extracted from the pointer to fn_p
958 * inside the lambda function.
959 * The double indirection is used to avoid having to worry about
960 * const casting.
962 void cpp_generator::print_callback_local(ostream &os, ParmVarDecl *param)
964 string pname;
965 QualType ptype;
966 string call_args, c_args, cpp_args, rettype, last_idx;
967 const FunctionProtoType *callback;
968 int num_params;
970 pname = param->getName().str();
971 ptype = param->getType();
973 c_args = generate_callback_args(ptype, false);
974 cpp_args = generate_callback_type(ptype);
976 callback = ptype->getPointeeType()->getAs<FunctionProtoType>();
977 rettype = callback->getReturnType().getAsString();
978 num_params = callback->getNumArgs();
980 last_idx = ::to_string(num_params - 1);
982 for (long i = 0; i < num_params - 1; i++) {
983 call_args += "isl::manage(arg_" + ::to_string(i) + ")";
984 if (i != num_params - 2)
985 call_args += ", ";
988 osprintf(os, " auto %s_p = &%s;\n", pname.c_str(), pname.c_str());
989 osprintf(os,
990 " auto %s_lambda = [](%s) -> %s {\n"
991 " auto *func = *static_cast<const %s **>(arg_%s);\n"
992 " stat ret = (*func)(%s);\n"
993 " return isl_stat(ret);\n"
994 " };\n",
995 pname.c_str(), c_args.c_str(), rettype.c_str(),
996 cpp_args.c_str(), last_idx.c_str(), call_args.c_str());
999 /* An array listing functions that must be renamed and the function name they
1000 * should be renamed to. We currently rename functions in case their name would
1001 * match a reserved C++ keyword, which is not allowed in C++.
1003 static const char *rename_map[][2] = {
1004 { "union", "unite" },
1007 /* Rename method "name" in case the method name in the C++ bindings should not
1008 * match the name in the C bindings. We do this for example to avoid
1009 * C++ keywords.
1011 std::string cpp_generator::rename_method(std::string name)
1013 for (size_t i = 0; i < sizeof(rename_map) / sizeof(rename_map[0]); i++)
1014 if (name.compare(rename_map[i][0]) == 0)
1015 return rename_map[i][1];
1017 return name;
1020 /* Translate isl class "clazz" to its corresponding C++ type.
1022 string cpp_generator::type2cpp(const isl_class &clazz)
1024 return type2cpp(clazz.name);
1027 /* Translate type string "type_str" to its C++ name counterpart.
1029 string cpp_generator::type2cpp(string type_str)
1031 return type_str.substr(4);
1034 /* Translate QualType "type" to its C++ name counterpart.
1036 string cpp_generator::type2cpp(QualType type)
1038 if (is_isl_type(type))
1039 return "isl::" + type2cpp(type->getPointeeType().getAsString());
1041 if (is_isl_bool(type))
1042 return "isl::boolean";
1044 if (is_isl_stat(type))
1045 return "isl::stat";
1047 if (type->isIntegerType())
1048 return type.getAsString();
1050 if (is_string(type))
1051 return "std::string";
1053 if (is_callback(type))
1054 return generate_callback_type(type);
1056 die("Cannot convert type to C++ type");
1059 /* Check if "subclass_type" is a subclass of "class_type".
1061 bool cpp_generator::is_subclass(QualType subclass_type,
1062 const isl_class &class_type)
1064 std::string type_str = subclass_type->getPointeeType().getAsString();
1065 std::vector<std::string> superclasses;
1066 std::vector<const isl_class *> parents;
1067 std::vector<std::string>::iterator ci;
1069 superclasses = generator::find_superclasses(classes[type_str].type);
1071 for (ci = superclasses.begin(); ci < superclasses.end(); ci++)
1072 parents.push_back(&classes[*ci]);
1074 while (!parents.empty()) {
1075 const isl_class *candidate = parents.back();
1077 parents.pop_back();
1079 if (&class_type == candidate)
1080 return true;
1082 superclasses = generator::find_superclasses(candidate->type);
1084 for (ci = superclasses.begin(); ci < superclasses.end(); ci++)
1085 parents.push_back(&classes[*ci]);
1088 return false;
1091 /* Check if "cons" is an implicit conversion constructor of class "clazz".
1093 * An implicit conversion constructor is generated in case "cons" has a single
1094 * parameter, where the parameter type is a subclass of the class that is
1095 * currently being generated.
1097 bool cpp_generator::is_implicit_conversion(const isl_class &clazz,
1098 FunctionDecl *cons)
1100 ParmVarDecl *param = cons->getParamDecl(0);
1101 QualType type = param->getOriginalType();
1103 int num_params = cons->getNumParams();
1104 if (num_params != 1)
1105 return false;
1107 if (is_isl_type(type) && !is_isl_ctx(type) && is_subclass(type, clazz))
1108 return true;
1110 return false;
1113 /* Get kind of "method" in "clazz".
1115 * Given the declaration of a static or member method, returns its kind.
1117 cpp_generator::function_kind cpp_generator::get_method_kind(
1118 const isl_class &clazz, FunctionDecl *method)
1120 if (is_static(clazz, method))
1121 return function_kind_static_method;
1122 else
1123 return function_kind_member_method;