00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "interfaceMakerC.h"
00020 #include "interrogateBuilder.h"
00021 #include "interrogate.h"
00022 #include "functionRemap.h"
00023 #include "parameterRemapUnchanged.h"
00024 #include "typeManager.h"
00025
00026 #include "interrogateDatabase.h"
00027 #include "interrogateType.h"
00028 #include "interrogateFunction.h"
00029 #include "cppFunctionType.h"
00030
00031
00032
00033
00034
00035
00036 InterfaceMakerC::
00037 InterfaceMakerC(InterrogateModuleDef *def) :
00038 InterfaceMaker(def)
00039 {
00040 }
00041
00042
00043
00044
00045
00046
00047 InterfaceMakerC::
00048 ~InterfaceMakerC() {
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058 void InterfaceMakerC::
00059 write_prototypes(ostream &out) {
00060 Functions::iterator fi;
00061 for (fi = _functions.begin(); fi != _functions.end(); ++fi) {
00062 Function *func = (*fi);
00063 write_prototype_for(out, func);
00064 }
00065
00066 out << "\n";
00067 InterfaceMaker::write_prototypes(out);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077 void InterfaceMakerC::
00078 write_functions(ostream &out) {
00079 Functions::iterator fi;
00080 for (fi = _functions.begin(); fi != _functions.end(); ++fi) {
00081 Function *func = (*fi);
00082 write_function_for(out, func);
00083 }
00084
00085 InterfaceMaker::write_functions(out);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 bool InterfaceMakerC::
00097 synthesize_this_parameter() {
00098 return true;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 string InterfaceMakerC::
00108 get_wrapper_prefix() {
00109 return "_inC";
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119 string InterfaceMakerC::
00120 get_unique_prefix() {
00121 return "c";
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 void InterfaceMakerC::
00131 record_function_wrapper(InterrogateFunction &ifunc,
00132 FunctionWrapperIndex wrapper_index) {
00133 ifunc._c_wrappers.push_back(wrapper_index);
00134 }
00135
00136
00137
00138
00139
00140
00141 void InterfaceMakerC::
00142 write_prototype_for(ostream &out, InterfaceMaker::Function *func) {
00143 Function::Remaps::const_iterator ri;
00144
00145 for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
00146 FunctionRemap *remap = (*ri);
00147 if (output_function_names) {
00148 out << "extern \"C\" ";
00149 }
00150 write_function_header(out, func, remap, false);
00151 out << ";\n";
00152 }
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 void InterfaceMakerC::
00162 write_function_for(ostream &out, InterfaceMaker::Function *func) {
00163 Function::Remaps::const_iterator ri;
00164
00165 for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
00166 FunctionRemap *remap = (*ri);
00167 write_function_instance(out, func, remap);
00168 }
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 void InterfaceMakerC::
00178 write_function_instance(ostream &out, InterfaceMaker::Function *func,
00179 FunctionRemap *remap) {
00180 out << "/*\n"
00181 << " * C wrapper for\n"
00182 << " * ";
00183 remap->write_orig_prototype(out, 0);
00184 out << "\n"
00185 << " */\n";
00186
00187 if (!output_function_names) {
00188
00189
00190 out << "static ";
00191 }
00192
00193 write_function_header(out, func, remap, true);
00194 out << " {\n";
00195
00196 if (generate_spam) {
00197 write_spam_message(out, remap);
00198 }
00199
00200 string return_expr =
00201 remap->call_function(out, 2, true, "param0");
00202 return_expr = manage_return_value(out, 2, remap, return_expr);
00203 if (!return_expr.empty()) {
00204 out << " return " << return_expr << ";\n";
00205 }
00206
00207 out << "}\n\n";
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 void InterfaceMakerC::
00217 write_function_header(ostream &out, InterfaceMaker::Function *func,
00218 FunctionRemap *remap, bool newline) {
00219 if (remap->_void_return) {
00220 out << "void";
00221 } else {
00222 out << remap->_return_type->get_new_type()->get_local_name(&parser);
00223 }
00224 if (newline) {
00225 out << "\n";
00226 } else {
00227 out << " ";
00228 }
00229
00230 out << remap->_wrapper_name << "(";
00231 int pn = 0;
00232 if (pn < (int)remap->_parameters.size()) {
00233 remap->_parameters[pn]._remap->get_new_type()->
00234 output_instance(out, remap->get_parameter_name(pn), &parser);
00235 pn++;
00236 while (pn < (int)remap->_parameters.size()) {
00237 out << ", ";
00238 remap->_parameters[pn]._remap->get_new_type()->
00239 output_instance(out, remap->get_parameter_name(pn), &parser);
00240 pn++;
00241 }
00242 }
00243 out << ")";
00244 }