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