1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#ifndef ISL_INTERFACE_CPP_H
#define ISL_INTERFACE_CPP_H
#include <iostream>
#include <string>
#include <vector>
#include "generator.h"
/* A generated C++ method derived from an isl function.
*
* "clazz" is the class to which the method belongs.
* "fd" is the original isl function.
* "name" is the name of the method, which may be different
* from the default name derived from "fd".
* "kind" is the type of the method.
* "callbacks" stores the callback arguments.
*/
struct Method {
enum Kind {
static_method,
member_method,
constructor,
};
struct list_combiner;
static list_combiner print_combiner(std::ostream &os);
static list_combiner empty_combiner();
Method(const isl_class &clazz, FunctionDecl *fd,
const std::string &name);
Method(const isl_class &clazz, FunctionDecl *fd);
int c_num_params() const;
virtual int num_params() const;
virtual bool param_needs_copy(int pos) const;
virtual clang::ParmVarDecl *get_param(int pos) const;
virtual void print_param_use(ostream &os, int pos) const;
bool is_subclass_mutator() const;
static void on_arg_list(int start, int end,
const list_combiner &combiner,
const std::function<bool(int i)> &on_arg_skip_next);
static void print_arg_list(std::ostream &os, int start, int end,
const std::function<bool(int i)> &print_arg_skip_next);
void on_fd_arg_list(int start, int end,
const list_combiner &combiner,
const std::function<void(int i, int arg)> &on_arg) const;
void print_fd_arg_list(std::ostream &os, int start, int end,
const std::function<void(int i, int arg)> &print_arg) const;
void on_cpp_arg_list(const list_combiner &combiner,
const std::function<void(int i, int arg)> &on_arg) const;
void on_cpp_arg_list(
const std::function<void(int i, int arg)> &on_arg) const;
void print_cpp_arg_list(std::ostream &os,
const std::function<void(int i, int arg)> &print_arg) const;
const isl_class &clazz;
FunctionDecl *const fd;
const std::string name;
const enum Kind kind;
const std::vector<ParmVarDecl *> callbacks;
};
/* A data structure expressing how Method::on_arg_list should combine
* the arguments.
*
* In particular, "before" is called before any argument is handled;
* "between" is called between two arguments and
* "after" is called after all arguments have been handled.
*/
struct Method::list_combiner {
const std::function<void()> before;
const std::function<void()> between;
const std::function<void()> after;
};
/* A method that does not require its isl type parameters to be a copy.
*/
struct NoCopyMethod : Method {
NoCopyMethod(const Method &method) : Method(method) {}
virtual bool param_needs_copy(int pos) const override;
};
/* A generated method that performs one or more argument conversions and
* then calls the original method.
*
* A ConversionMethod inherits from a NoCopyMethod, because
* unlike methods that call an isl C function,
* a conversion method never calls release() on an isl type argument,
* so they can all be passed as const references.
*
* "this_type" is the name of the type to which "this" should be converted
* (if different from clazz.name).
* "get_param_fn" returns the method argument at position "pos".
*/
struct ConversionMethod : NoCopyMethod {
ConversionMethod(const Method &method, const std::string &this_type,
const std::function<clang::ParmVarDecl *(int pos)> &get_param);
ConversionMethod(const Method &method, const std::string &this_type);
ConversionMethod(const Method &method,
const std::function<clang::ParmVarDecl *(int pos)> &get_param);
virtual clang::ParmVarDecl *get_param(int pos) const override;
void print_call(std::ostream &os, const std::string &ns) const;
const std::string this_type;
const std::function<clang::ParmVarDecl *(int pos)> get_param_fn;
};
/* A specialized generated C++ method for setting an enum.
*
* "enum_name" is a string representation of the enum value
* set by this method.
*/
struct EnumMethod : public Method {
EnumMethod(const isl_class &clazz, FunctionDecl *fd,
const std::string &method_name, const std::string &enum_name);
virtual int num_params() const override;
virtual void print_param_use(ostream &os, int pos) const override;
std::string enum_name;
};
/* A type printer for converting argument and return types,
* as well as the class type,
* to string representations of the corresponding types
* in the C++ interface.
*/
struct cpp_type_printer {
cpp_type_printer() {}
virtual std::string isl_bool() const;
virtual std::string isl_stat() const;
virtual std::string isl_size() const;
virtual std::string isl_namespace() const;
virtual std::string class_type(const std::string &cpp_name) const;
virtual std::string qualified(int arg, const std::string &cpp_type)
const;
std::string isl_type(int arg, QualType type) const;
std::string generate_callback_args(int arg, QualType type, bool cpp)
const;
std::string generate_callback_type(int arg, QualType type) const;
std::string param(int arg, QualType type) const;
std::string return_type(const Method &method) const;
};
/* Generator for C++ bindings.
*/
class cpp_generator : public generator {
protected:
struct class_printer;
public:
cpp_generator(SourceManager &SM, set<RecordDecl *> &exported_types,
set<FunctionDecl *> exported_functions,
set<FunctionDecl *> functions);
private:
void set_class_construction_types(isl_class &clazz);
void set_construction_types();
void copy_methods(isl_class &clazz, const std::string &name,
const isl_class &super, const function_set &methods);
void copy_super_methods(isl_class &clazz, const isl_class &super);
void copy_super_methods(isl_class &clazz, set<string> &done);
void copy_super_methods();
bool is_implicit_conversion(const Method &cons);
bool is_subclass(QualType subclass_type, const isl_class &class_type);
public:
static string type2cpp(const isl_class &clazz);
static string type2cpp(string type_string);
};
/* A helper class for printing method declarations and definitions
* of a class.
*
* "os" is the stream onto which the methods are printed.
* "clazz" describes the methods of the class.
* "cppstring" is the C++ name of the class.
* "generator" is the C++ interface generator printing the classes.
* "declarations" is set if this object is used to print declarations.
*/
struct cpp_generator::class_printer {
std::ostream &os;
const isl_class &clazz;
const std::string cppstring;
cpp_generator &generator;
const bool declarations;
class_printer(std::ostream &os, const isl_class &clazz,
cpp_generator &generator, bool declarations);
void print_constructors();
void print_methods();
bool next_variant(FunctionDecl *fd, std::vector<bool> &convert);
void print_method_variants(FunctionDecl *fd, const std::string &name);
virtual bool want_descendent_overloads(const function_set &methods) = 0;
void print_descendent_overloads(FunctionDecl *fd,
const std::string &name);
void print_method_group(const function_set &methods,
const std::string &name);
virtual void print_method(const Method &method) = 0;
virtual void print_method(const ConversionMethod &method) = 0;
virtual void print_get_method(FunctionDecl *fd) = 0;
void print_set_enums(FunctionDecl *fd);
void print_set_enums();
ParmVarDecl *get_param(FunctionDecl *fd, int pos,
const std::vector<bool> &convert);
void print_method_header(const Method &method,
const cpp_type_printer &type_printer);
};
#endif
|