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
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
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
, ...)
56 va_start(arguments
, format
);
57 size
= vsnprintf(NULL
, 0, format
, arguments
);
58 string_pointer
= new char[size
+ 1];
60 va_start(arguments
, format
);
61 vsnprintf(string_pointer
, size
+ 1, format
, arguments
);
64 delete[] string_pointer
;
67 /* Convert "l" to a string.
69 static std::string
to_string(long l
)
71 std::ostringstream strm
;
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
82 void cpp_generator::generate()
87 osprintf(os
, "namespace isl {\n\n");
88 osprintf(os
, "inline namespace noexceptions {\n\n");
90 print_forward_declarations(os
);
92 print_declarations(os
);
94 print_implementations(os
);
96 osprintf(os
, "} // namespace noexceptions\n");
97 osprintf(os
, "} // namespace isl\n\n");
98 osprintf(os
, "#endif /* ISL_CPP_NOEXCEPTIONS */\n");
101 /* Print forward declarations for all classes to "os".
103 void cpp_generator::print_forward_declarations(ostream
&os
)
105 map
<string
, isl_class
>::iterator ci
;
107 osprintf(os
, "// forward declarations\n");
109 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
)
110 print_class_forward_decl(os
, ci
->second
);
113 /* Print all declarations to "os".
115 void cpp_generator::print_declarations(ostream
&os
)
117 map
<string
, isl_class
>::iterator ci
;
120 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
126 print_class(os
, ci
->second
);
130 /* Print all implementations to "os".
132 void cpp_generator::print_implementations(ostream
&os
)
134 map
<string
, isl_class
>::iterator ci
;
137 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
143 print_class_impl(os
, ci
->second
);
147 /* Print declarations for class "clazz" to "os".
149 void cpp_generator::print_class(ostream
&os
, const isl_class
&clazz
)
151 const char *name
= clazz
.name
.c_str();
152 std::string cppstring
= type2cpp(clazz
);
153 const char *cppname
= cppstring
.c_str();
155 osprintf(os
, "// declarations for isl::%s\n", cppname
);
157 print_class_factory_decl(os
, clazz
);
159 osprintf(os
, "class %s {\n", cppname
);
160 print_class_factory_decl(os
, clazz
, " friend ");
162 osprintf(os
, " %s *ptr = nullptr;\n", name
);
164 print_private_constructors_decl(os
, clazz
);
166 osprintf(os
, "public:\n");
167 print_public_constructors_decl(os
, clazz
);
168 print_constructors_decl(os
, clazz
);
169 print_copy_assignment_decl(os
, clazz
);
170 print_destructor_decl(os
, clazz
);
171 print_ptr_decl(os
, clazz
);
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
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();
212 osprintf(os
, "inline isl::%s manage(__isl_take %s *ptr);\n", cppname
,
215 osprintf(os
, "inline isl::%s manage_copy(__isl_keep %s *ptr);\n",
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
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
,
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
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",
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.
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"
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
,
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.
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 declarations for methods in class "clazz" to "os".
366 void cpp_generator::print_methods_decl(ostream
&os
, const isl_class
&clazz
)
368 map
<string
, set
<FunctionDecl
*> >::const_iterator it
;
370 for (it
= clazz
.methods
.begin(); it
!= clazz
.methods
.end(); ++it
)
371 print_method_group_decl(os
, clazz
, it
->first
, it
->second
);
374 /* Print declarations for methods "methods" of name "fullname" in class "clazz"
377 * "fullname" is the name of the generated C++ method. It commonly corresponds
378 * to the isl name, with the object type prefix dropped.
379 * In case of overloaded methods, the result type suffix has also been removed.
381 void cpp_generator::print_method_group_decl(ostream
&os
, const isl_class
&clazz
,
382 const string
&fullname
, const set
<FunctionDecl
*> &methods
)
384 set
<FunctionDecl
*>::const_iterator it
;
386 for (it
= methods
.begin(); it
!= methods
.end(); ++it
) {
387 function_kind kind
= get_method_kind(clazz
, *it
);
388 print_method_decl(os
, clazz
, fullname
, *it
, kind
);
392 /* Print declarations for "method" in class "clazz" to "os".
394 * "fullname" is the name of the generated C++ method. It commonly corresponds
395 * to the isl name, with the object type prefix dropped.
396 * In case of overloaded methods, the result type suffix has also been removed.
398 * "kind" specifies the kind of method that should be generated.
400 void cpp_generator::print_method_decl(ostream
&os
, const isl_class
&clazz
,
401 const string
&fullname
, FunctionDecl
*method
, function_kind kind
)
403 print_method_header(os
, clazz
, method
, fullname
, true, kind
);
406 /* Print implementations for class "clazz" to "os".
408 void cpp_generator::print_class_impl(ostream
&os
, const isl_class
&clazz
)
410 std::string cppstring
= type2cpp(clazz
);
411 const char *cppname
= cppstring
.c_str();
413 osprintf(os
, "// implementations for isl::%s\n", cppname
);
415 print_class_factory_impl(os
, clazz
);
417 print_public_constructors_impl(os
, clazz
);
419 print_private_constructors_impl(os
, clazz
);
421 print_constructors_impl(os
, clazz
);
423 print_copy_assignment_impl(os
, clazz
);
425 print_destructor_impl(os
, clazz
);
427 print_ptr_impl(os
, clazz
);
429 print_methods_impl(os
, clazz
);
432 /* Print implementation of global factory function to "os".
434 void cpp_generator::print_class_factory_impl(ostream
&os
,
435 const isl_class
&clazz
)
437 const char *name
= clazz
.name
.c_str();
438 std::string cppstring
= type2cpp(clazz
);
439 const char *cppname
= cppstring
.c_str();
441 osprintf(os
, "isl::%s manage(__isl_take %s *ptr) {\n", cppname
, name
);
442 osprintf(os
, " return %s(ptr);\n", cppname
);
445 osprintf(os
, "isl::%s manage_copy(__isl_keep %s *ptr) {\n", cppname
,
447 osprintf(os
, " return %s(%s_copy(ptr));\n", cppname
, name
);
451 /* Print implementations of private constructors for class "clazz" to "os".
453 void cpp_generator::print_private_constructors_impl(ostream
&os
,
454 const isl_class
&clazz
)
456 const char *name
= clazz
.name
.c_str();
457 std::string cppstring
= type2cpp(clazz
);
458 const char *cppname
= cppstring
.c_str();
460 osprintf(os
, "%s::%s(__isl_take %s *ptr)\n : ptr(ptr) {}\n",
461 cppname
, cppname
, name
);
464 /* Print implementations of public constructors for class "clazz" to "os".
466 void cpp_generator::print_public_constructors_impl(ostream
&os
,
467 const isl_class
&clazz
)
469 const char *name
= clazz
.name
.c_str();
470 std::string cppstring
= type2cpp(clazz
);
471 const char *cppname
= cppstring
.c_str();
473 osprintf(os
, "%s::%s()\n : ptr(nullptr) {}\n\n", cppname
, cppname
);
474 osprintf(os
, "%s::%s(const isl::%s &obj)\n : ptr(obj.copy()) {}\n",
475 cppname
, cppname
, cppname
, name
);
478 /* Print implementations of constructors for class "clazz" to "os".
480 void cpp_generator::print_constructors_impl(ostream
&os
,
481 const isl_class
&clazz
)
483 set
<FunctionDecl
*>::const_iterator in
;
484 const set
<FunctionDecl
*> constructors
= clazz
.constructors
;
486 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
) {
487 FunctionDecl
*cons
= *in
;
488 string fullname
= cons
->getName();
489 function_kind kind
= function_kind_constructor
;
491 print_method_impl(os
, clazz
, fullname
, cons
, kind
);
495 /* Print implementation of copy assignment operator for class "clazz" to "os".
497 void cpp_generator::print_copy_assignment_impl(ostream
&os
,
498 const isl_class
&clazz
)
500 const char *name
= clazz
.name
.c_str();
501 std::string cppstring
= type2cpp(clazz
);
502 const char *cppname
= cppstring
.c_str();
504 osprintf(os
, "%s &%s::operator=(isl::%s obj) {\n", cppname
,
506 osprintf(os
, " std::swap(this->ptr, obj.ptr);\n", name
);
507 osprintf(os
, " return *this;\n");
511 /* Print implementation of destructor for class "clazz" to "os".
513 void cpp_generator::print_destructor_impl(ostream
&os
,
514 const isl_class
&clazz
)
516 const char *name
= clazz
.name
.c_str();
517 std::string cppstring
= type2cpp(clazz
);
518 const char *cppname
= cppstring
.c_str();
520 osprintf(os
, "%s::~%s() {\n", cppname
, cppname
);
521 osprintf(os
, " if (ptr)\n");
522 osprintf(os
, " %s_free(ptr);\n", name
);
526 /* Print implementation of ptr() functions for class "clazz" to "os".
528 void cpp_generator::print_ptr_impl(ostream
&os
, const isl_class
&clazz
)
530 const char *name
= clazz
.name
.c_str();
531 std::string cppstring
= type2cpp(clazz
);
532 const char *cppname
= cppstring
.c_str();
534 osprintf(os
, "__isl_give %s *%s::copy() const & {\n", name
, cppname
);
535 osprintf(os
, " return %s_copy(ptr);\n", name
);
536 osprintf(os
, "}\n\n");
537 osprintf(os
, "__isl_keep %s *%s::get() const {\n", name
, cppname
);
538 osprintf(os
, " return ptr;\n");
539 osprintf(os
, "}\n\n");
540 osprintf(os
, "__isl_give %s *%s::release() {\n", name
, cppname
);
541 osprintf(os
, " %s *tmp = ptr;\n", name
);
542 osprintf(os
, " ptr = nullptr;\n");
543 osprintf(os
, " return tmp;\n");
544 osprintf(os
, "}\n\n");
545 osprintf(os
, "bool %s::is_null() const {\n", cppname
);
546 osprintf(os
, " return ptr == nullptr;\n");
550 /* Print definitions for methods of class "clazz" to "os".
552 void cpp_generator::print_methods_impl(ostream
&os
, const isl_class
&clazz
)
554 map
<string
, set
<FunctionDecl
*> >::const_iterator it
;
557 for (it
= clazz
.methods
.begin(); it
!= clazz
.methods
.end(); ++it
) {
562 print_method_group_impl(os
, clazz
, it
->first
, it
->second
);
566 /* Print definitions for methods "methods" of name "fullname" in class "clazz"
569 * "fullname" is the name of the generated C++ method. It commonly corresponds
570 * to the isl name, with the object type prefix dropped.
571 * In case of overloaded methods, the result type suffix has also been removed.
573 * "kind" specifies the kind of method that should be generated.
575 void cpp_generator::print_method_group_impl(ostream
&os
, const isl_class
&clazz
,
576 const string
&fullname
, const set
<FunctionDecl
*> &methods
)
578 set
<FunctionDecl
*>::const_iterator it
;
581 for (it
= methods
.begin(); it
!= methods
.end(); ++it
) {
587 kind
= get_method_kind(clazz
, *it
);
588 print_method_impl(os
, clazz
, fullname
, *it
, kind
);
592 /* Print the use of "param" to "os".
594 * "load_from_this_ptr" specifies whether the parameter should be loaded from
595 * the this-ptr. In case a value is loaded from a this pointer, the original
596 * value must be preserved and must consequently be copied. Values that are
597 * loaded from parameters do not need to be preserved, as such values will
598 * already be copies of the actual parameters. It is consequently possible
599 * to directly take the pointer from these values, which saves
600 * an unnecessary copy.
602 * In case the parameter is a callback function, two parameters get printed,
603 * a wrapper for the callback function and a pointer to the actual
604 * callback function. The wrapper is expected to be available
605 * in a previously declared variable <name>_lambda, while
606 * the actual callback function is expected in <name>_p.
607 * The caller of this function must ensure that these variables exist.
609 void cpp_generator::print_method_param_use(ostream
&os
, ParmVarDecl
*param
,
610 bool load_from_this_ptr
)
612 string name
= param
->getName().str();
613 const char *name_str
= name
.c_str();
614 QualType type
= param
->getOriginalType();
616 if (type
->isIntegerType()) {
617 osprintf(os
, "%s", name_str
);
621 if (is_string(type
)) {
622 osprintf(os
, "%s.c_str()", name_str
);
626 if (is_callback(type
)) {
627 osprintf(os
, "%s_lambda, ", name_str
);
628 osprintf(os
, "&%s_p", name_str
);
632 if (!load_from_this_ptr
&& !is_callback(type
))
633 osprintf(os
, "%s.", name_str
);
636 osprintf(os
, "get()");
638 if (load_from_this_ptr
)
639 osprintf(os
, "copy()");
641 osprintf(os
, "release()");
645 /* Print definition for "method" in class "clazz" to "os".
647 * "fullname" is the name of the generated C++ method. It commonly corresponds
648 * to the isl name, with the object type prefix dropped.
649 * In case of overloaded methods, the result type suffix has also been removed.
651 * "kind" specifies the kind of method that should be generated.
653 * This method distinguishes three kinds of methods: member methods, static
654 * methods, and constructors.
656 * Member methods call "method" by passing to the underlying isl function the
657 * isl object belonging to "this" as first argument and the remaining arguments
658 * as subsequent arguments. The result of the isl function is returned as a new
659 * object if the underlying isl function returns an isl_* ptr or an isl_bool
660 * value, as std::string if the isl function returns 'const char *', and as
661 * unmodified return value otherwise.
663 * Static methods call "method" by passing all arguments to the underlying isl
664 * function, as no this-pointer is available. The result is a newly managed
667 * Constructors create a new object from a given set of input parameters. They
668 * do not return a value, but instead update the pointer stored inside the
669 * newly created object.
671 * If the method has a callback argument, we reduce the number of parameters
672 * that are exposed by one to hide the user pointer from the interface. On
673 * the C++ side no user pointer is needed, as arguments can be forwarded
674 * as part of the std::function argument which specifies the callback function.
676 void cpp_generator::print_method_impl(ostream
&os
, const isl_class
&clazz
,
677 const string
&fullname
, FunctionDecl
*method
, function_kind kind
)
679 string cname
= fullname
.substr(clazz
.name
.length() + 1);
680 string methodname
= method
->getName();
681 int num_params
= method
->getNumParams();
682 QualType return_type
= method
->getReturnType();
683 string rettype_str
= type2cpp(return_type
);
684 bool has_callback
= false;
686 print_method_header(os
, clazz
, method
, fullname
, false, kind
);
688 for (int i
= 0; i
< num_params
; ++i
) {
689 ParmVarDecl
*param
= method
->getParamDecl(i
);
690 if (is_callback(param
->getType())) {
693 print_callback_local(os
, param
);
697 osprintf(os
, " auto res = %s(", methodname
.c_str());
699 for (int i
= 0; i
< num_params
; ++i
) {
700 ParmVarDecl
*param
= method
->getParamDecl(i
);
701 bool load_from_this_ptr
= false;
703 if (i
== 0 && kind
== function_kind_member_method
)
704 load_from_this_ptr
= true;
706 print_method_param_use(os
, param
, load_from_this_ptr
);
708 if (i
!= num_params
- 1)
711 osprintf(os
, ");\n");
713 if (kind
== function_kind_constructor
) {
714 osprintf(os
, " ptr = res;\n");
715 } else if (is_isl_type(return_type
) || is_isl_bool(return_type
)) {
716 osprintf(os
, " return manage(res);\n");
717 } else if (has_callback
) {
718 osprintf(os
, " return %s(res);\n", rettype_str
.c_str());
719 } else if (is_string(return_type
)) {
720 osprintf(os
, " std::string tmp(res);\n");
722 osprintf(os
, " free(res);\n");
723 osprintf(os
, " return tmp;\n");
725 osprintf(os
, " return res;\n");
731 /* Print the header for "method" in class "clazz" to "os".
733 * Print the header of a declaration if "is_declaration" is set, otherwise print
734 * the header of a method definition.
736 * "fullname" is the name of the generated C++ method. It commonly corresponds
737 * to the isl name, with the object type prefix dropped.
738 * In case of overloaded methods, the result type suffix has also been removed.
740 * "kind" specifies the kind of method that should be generated.
742 * This function prints headers for member methods, static methods, and
743 * constructors, either for their declaration or definition.
745 * Member functions are declared as "const", as they do not change the current
746 * object, but instead create a new object. They always retrieve the first
747 * parameter of the original isl function from the this-pointer of the object,
748 * such that only starting at the second parameter the parameters of the
749 * original function become part of the method's interface.
753 * __isl_give isl_set *isl_set_intersect(__isl_take isl_set *s1,
754 * __isl_take isl_set *s2);
756 * is translated into:
758 * inline isl::set intersect(isl::set set2) const;
760 * For static functions and constructors all parameters of the original isl
761 * function are exposed.
763 * Parameters that are defined as __isl_keep or are of type string, are passed
764 * as const reference, which allows the compiler to optimize the parameter
767 * Constructors are marked as explicit using the C++ keyword 'explicit' or as
768 * implicit using a comment in place of the explicit keyword. By annotating
769 * implicit constructors with a comment, users of the interface are made
770 * aware of the potential danger that implicit construction is possible
771 * for these constructors, whereas without a comment not every user would
772 * know that implicit construction is allowed in absence of an explicit keyword.
774 void cpp_generator::print_method_header(ostream
&os
, const isl_class
&clazz
,
775 FunctionDecl
*method
, const string
&fullname
, bool is_declaration
,
778 string cname
= fullname
.substr(clazz
.name
.length() + 1);
779 string rettype_str
= type2cpp(method
->getReturnType());
780 string classname
= type2cpp(clazz
);
781 int num_params
= method
->getNumParams();
784 cname
= rename_method(cname
);
785 if (kind
== function_kind_member_method
)
788 if (is_declaration
) {
791 if (kind
== function_kind_static_method
)
792 osprintf(os
, "static ");
794 osprintf(os
, "inline ");
796 if (kind
== function_kind_constructor
) {
797 if (is_implicit_conversion(clazz
, method
))
798 osprintf(os
, "/* implicit */ ");
800 osprintf(os
, "explicit ");
804 if (kind
!= function_kind_constructor
)
805 osprintf(os
, "%s ", rettype_str
.c_str());
808 osprintf(os
, "%s::", classname
.c_str());
810 if (kind
!= function_kind_constructor
)
811 osprintf(os
, "%s", cname
.c_str());
813 osprintf(os
, "%s", classname
.c_str());
817 for (int i
= first_param
; i
< num_params
; ++i
) {
818 ParmVarDecl
*param
= method
->getParamDecl(i
);
819 QualType type
= param
->getOriginalType();
820 string cpptype
= type2cpp(type
);
822 if (is_callback(type
))
825 if (keeps(param
) || is_string(type
) || is_callback(type
))
826 osprintf(os
, "const %s &%s", cpptype
.c_str(),
827 param
->getName().str().c_str());
829 osprintf(os
, "%s %s", cpptype
.c_str(),
830 param
->getName().str().c_str());
832 if (i
!= num_params
- 1)
838 if (kind
== function_kind_member_method
)
839 osprintf(os
, " const");
844 osprintf(os
, " {\n");
847 /* Generate the list of argument types for a callback function of
848 * type "type". If "cpp" is set, then generate the C++ type list, otherwise
851 * For a callback of type
853 * isl_stat (*)(__isl_take isl_map *map, void *user)
855 * the following C++ argument list is generated:
859 string
cpp_generator::generate_callback_args(QualType type
, bool cpp
)
861 std::string type_str
;
862 const FunctionProtoType
*callback
;
865 callback
= type
->getPointeeType()->getAs
<FunctionProtoType
>();
866 num_params
= callback
->getNumArgs();
870 for (long i
= 0; i
< num_params
; i
++) {
871 QualType type
= callback
->getArgType(i
);
874 type_str
+= type2cpp(type
);
876 type_str
+= type
.getAsString();
879 type_str
+= "arg_" + ::to_string(i
);
881 if (i
!= num_params
- 1)
888 /* Generate the full cpp type of a callback function of type "type".
890 * For a callback of type
892 * isl_stat (*)(__isl_take isl_map *map, void *user)
894 * the following type is generated:
896 * std::function<isl::stat(isl::map)>
898 string
cpp_generator::generate_callback_type(QualType type
)
900 std::string type_str
;
901 const FunctionProtoType
*callback
= type
->getPointeeType()->getAs
<FunctionProtoType
>();
902 QualType return_type
= callback
->getReturnType();
903 string rettype_str
= type2cpp(return_type
);
905 type_str
= "std::function<";
906 type_str
+= rettype_str
;
908 type_str
+= generate_callback_args(type
, true);
914 /* Print the local variables that are needed for a callback argument,
915 * in particular, print a lambda function that wraps the callback and
916 * a pointer to the actual C++ callback function.
918 * For a callback of the form
920 * isl_stat (*fn)(__isl_take isl_map *map, void *user)
922 * the following lambda function is generated:
924 * auto fn_lambda = [](isl_map *arg_0, void *arg_1) -> isl_stat {
925 * auto *func = *static_cast<const std::function<stat(map)> **>(arg_1);
926 * stat ret = (*func)(isl::manage(arg_0));
927 * return isl_stat(ret);
930 * The pointer to the std::function C++ callback function is stored in fn_p.
931 * This std::function object represents the actual user
932 * callback function together with the locally captured state at the caller.
934 * The lambda function is expected to be used as a C callback function
935 * where the lambda itself is provided as the function pointer and
936 * where the user void pointer is a pointer to fn_p.
937 * The std::function object is extracted from the pointer to fn_p
938 * inside the lambda function.
939 * The double indirection is used to avoid having to worry about
942 void cpp_generator::print_callback_local(ostream
&os
, ParmVarDecl
*param
) {
945 string call_args
, c_args
, cpp_args
, rettype
, last_idx
;
946 const FunctionProtoType
*callback
;
949 pname
= param
->getName().str();
950 ptype
= param
->getType();
952 c_args
= generate_callback_args(ptype
, false);
953 cpp_args
= generate_callback_type(ptype
);
955 callback
= ptype
->getPointeeType()->getAs
<FunctionProtoType
>();
956 rettype
= callback
->getReturnType().getAsString();
957 num_params
= callback
->getNumArgs();
959 last_idx
= ::to_string(num_params
- 1);
961 for (long i
= 0; i
< num_params
- 1; i
++) {
962 call_args
+= "isl::manage(arg_" + ::to_string(i
) + ")";
963 if (i
!= num_params
- 2)
967 osprintf(os
, " auto %s_p = &%s;\n", pname
.c_str(), pname
.c_str());
969 " auto %s_lambda = [](%s) -> %s {\n"
970 " auto *func = *static_cast<const %s **>(arg_%s);\n"
971 " stat ret = (*func)(%s);\n"
972 " return isl_stat(ret);\n"
974 pname
.c_str(), c_args
.c_str(), rettype
.c_str(),
975 cpp_args
.c_str(), last_idx
.c_str(), call_args
.c_str());
978 /* An array listing functions that must be renamed and the function name they
979 * should be renamed to. We currently rename functions in case their name would
980 * match a reserved C++ keyword, which is not allowed in C++.
982 static const char *rename_map
[][2] = {
983 { "union", "unite" },
986 /* Rename method "name" in case the method name in the C++ bindings should not
987 * match the name in the C bindings. We do this for example to avoid
990 std::string
cpp_generator::rename_method(std::string name
)
992 for (size_t i
= 0; i
< sizeof(rename_map
) / sizeof(rename_map
[0]); i
++)
993 if (name
.compare(rename_map
[i
][0]) == 0)
994 return rename_map
[i
][1];
999 /* Translate isl class "clazz" to its corresponding C++ type.
1001 string
cpp_generator::type2cpp(const isl_class
&clazz
)
1003 return type2cpp(clazz
.name
);
1006 /* Translate type string "type_str" to its C++ name counterpart.
1008 string
cpp_generator::type2cpp(string type_str
)
1010 return type_str
.substr(4);
1013 /* Translate QualType "type" to its C++ name counterpart.
1015 string
cpp_generator::type2cpp(QualType type
)
1017 if (is_isl_type(type
))
1018 return "isl::" + type2cpp(type
->getPointeeType().getAsString());
1020 if (is_isl_bool(type
))
1021 return "isl::boolean";
1023 if (is_isl_stat(type
))
1026 if (type
->isIntegerType())
1027 return type
.getAsString();
1029 if (is_string(type
))
1030 return "std::string";
1032 if (is_callback(type
))
1033 return generate_callback_type(type
);
1035 die("Cannot convert type to C++ type");
1038 /* Check if "subclass_type" is a subclass of "class_type".
1040 bool cpp_generator::is_subclass(QualType subclass_type
,
1041 const isl_class
&class_type
)
1043 std::string type_str
= subclass_type
->getPointeeType().getAsString();
1044 std::vector
<std::string
> superclasses
;
1045 std::vector
<const isl_class
*> parents
;
1046 std::vector
<std::string
>::iterator ci
;
1048 superclasses
= generator::find_superclasses(classes
[type_str
].type
);
1050 for (ci
= superclasses
.begin(); ci
< superclasses
.end(); ci
++)
1051 parents
.push_back(&classes
[*ci
]);
1053 while (!parents
.empty()) {
1054 const isl_class
*candidate
= parents
.back();
1058 if (&class_type
== candidate
)
1061 superclasses
= generator::find_superclasses(candidate
->type
);
1063 for (ci
= superclasses
.begin(); ci
< superclasses
.end(); ci
++)
1064 parents
.push_back(&classes
[*ci
]);
1070 /* Check if "cons" is an implicit conversion constructor of class "clazz".
1072 * An implicit conversion constructor is generated in case "cons" has a single
1073 * parameter, where the parameter type is a subclass of the class that is
1074 * currently being generated.
1076 bool cpp_generator::is_implicit_conversion(const isl_class
&clazz
,
1079 ParmVarDecl
*param
= cons
->getParamDecl(0);
1080 QualType type
= param
->getOriginalType();
1082 int num_params
= cons
->getNumParams();
1083 if (num_params
!= 1)
1086 if (is_isl_type(type
) && !is_isl_ctx(type
) && is_subclass(type
, clazz
))
1092 /* Get kind of "method" in "clazz".
1094 * Given the declaration of a static or member method, returns its kind.
1096 cpp_generator::function_kind
cpp_generator::get_method_kind(
1097 const isl_class
&clazz
, FunctionDecl
*method
)
1099 if (is_static(clazz
, method
))
1100 return function_kind_static_method
;
1102 return function_kind_member_method
;