| 1 | #ifndef FUNCTION_HPP |
| 2 | #define FUNCTION_HPP |
| 3 | |
| 4 | #include "genbase.hpp" |
| 5 | |
| 6 | #include <iosfwd> |
| 7 | #include <map> |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | struct ElementFunction |
| 13 | { |
| 14 | // type of function; method, etc |
| 15 | std::string kind; |
| 16 | // GIR name |
| 17 | std::string name; |
| 18 | // otherwise descriptive name (e.g. original C name) |
| 19 | std::string c_id; |
| 20 | // expression defining function |
| 21 | std::string functionexp; |
| 22 | bool throws{}; |
| 23 | // represents symbol in lib |
| 24 | // (which can be linked to or dlsym'ed) |
| 25 | bool lib_symbol{}; |
| 26 | std::string shadows; |
| 27 | }; |
| 28 | |
| 29 | // parameter data |
| 30 | constexpr static const int INDEX_DEFAULT = -10; |
| 31 | struct FunctionParameter |
| 32 | { |
| 33 | std::string name; |
| 34 | GeneratorBase::ArgInfo tinfo{}; |
| 35 | // deduced C type |
| 36 | std::string ptype; |
| 37 | bool instance{}; |
| 38 | std::string direction; |
| 39 | std::string transfer{TRANSFER_NOTHING}; |
| 40 | // index |
| 41 | int closure{INDEX_DEFAULT}, destroy{INDEX_DEFAULT}; |
| 42 | int callerallocates{}; |
| 43 | std::string scope; |
| 44 | bool optional{}; |
| 45 | bool nullable{}; |
| 46 | }; |
| 47 | using Parameter = FunctionParameter; |
| 48 | |
| 49 | // info defining function to construct declaration/definition |
| 50 | // (some parts not relevant for signal/callback) |
| 51 | struct FunctionDefinition |
| 52 | { |
| 53 | // return (and optionally additional outputs) |
| 54 | struct Output |
| 55 | { |
| 56 | // cpp type of output |
| 57 | std::string type; |
| 58 | // expression (value of output) |
| 59 | std::string value; |
| 60 | }; |
| 61 | |
| 62 | struct ArgTrait |
| 63 | { |
| 64 | // transfer (original attribute) |
| 65 | std::string transfer; |
| 66 | // inout parameter |
| 67 | bool inout{}; |
| 68 | // applicable/relevant arguments (indexed as usual; see below) |
| 69 | // usually only 1 (to 1) |
| 70 | // for callback; function, userdata[, destroy] |
| 71 | // for sized arrays; data, size |
| 72 | // (other containers only need 1 and handled as usual) |
| 73 | std::vector<int> args; |
| 74 | // custom type trait |
| 75 | std::string custom{}; |
| 76 | }; |
| 77 | |
| 78 | // GIR name (empty if not valid) |
| 79 | std::string name; |
| 80 | // statements preceding wrapped call (excluding final ;) |
| 81 | std::vector<std::string> pre_call; |
| 82 | // idem, post call |
| 83 | std::vector<std::string> post_call; |
| 84 | // assembled outputs (first one is return value, if any, could be empty) |
| 85 | std::vector<Output> cpp_outputs; |
| 86 | // parts that make up the C call (...) |
| 87 | // indexed by param number (instance = -1) |
| 88 | std::map<int, std::string> c_call; |
| 89 | // parts that make up the ( ... ) decl/def |
| 90 | // (similarly indexed/sorted) |
| 91 | std::map<int, std::string> cpp_decl; |
| 92 | // callee; trait info of (callback) parameters |
| 93 | // (lowest one is for return; always present) |
| 94 | // (except if fallback virtual method) |
| 95 | std::map<int, ArgTrait> arg_traits; |
| 96 | // extra parameters in a callee (= cb) declaration not present in callforward |
| 97 | // (to deal with cb sized array output0 |
| 98 | std::map<int, std::string> cpp_decl_extra; |
| 99 | // function (expression) to call |
| 100 | std::string c_callee; |
| 101 | // format with single placeholder for call (constructed based on all above) |
| 102 | std::string ret_format; |
| 103 | // parts used for callforward generation of callback type |
| 104 | // (callforward = C++ signature which then calls a C function) |
| 105 | // (callback = C signature which then calls a C++ function) |
| 106 | // typedef of C function to call |
| 107 | std::string cf_ctype; |
| 108 | // (derived) C signature (without instance parameter if virtual method) |
| 109 | std::string c_sig; |
| 110 | }; |
| 111 | |
| 112 | std::string make_arg_traits( |
| 113 | const std::map<int, FunctionDefinition::ArgTrait> &traits, |
| 114 | const std::string &c_sig); |
| 115 | |
| 116 | FunctionDefinition process_element_function(GeneratorContext &_ctx, |
| 117 | const std::string _ns, const pt::ptree::value_type &entry, |
| 118 | std::ostream &out, std::ostream &impl, const std::string &klass, |
| 119 | const std::string &klasstype, GeneratorBase::DepsSet &deps, |
| 120 | std::ostream *call_args, bool allow_deprecated); |
| 121 | |
| 122 | FunctionDefinition process_element_function(GeneratorContext &_ctx, |
| 123 | const std::string _ns, const ElementFunction &func, |
| 124 | const std::vector<Parameter> ¶ms, std::ostream &out, std::ostream &impl, |
| 125 | const std::string &klass, const std::string &klasstype, |
| 126 | GeneratorBase::DepsSet &deps); |
| 127 | |
| 128 | #endif // FUNCTION_HPP |