David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 19 | |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <fstream> |
| 22 | #include <iostream> |
| 23 | #include <vector> |
| 24 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sstream> |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 29 | #include "t_oop_generator.h" |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 30 | #include "platform.h" |
Roger Meier | 08d4681 | 2011-04-12 19:08:21 +0000 | [diff] [blame] | 31 | #include "version.h" |
| 32 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * OCaml code generator. |
| 38 | * |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 39 | */ |
| 40 | class t_ocaml_generator : public t_oop_generator { |
| 41 | public: |
| 42 | t_ocaml_generator( |
| 43 | t_program* program, |
| 44 | const std::map<std::string, std::string>& parsed_options, |
| 45 | const std::string& option_string) |
| 46 | : t_oop_generator(program) |
| 47 | { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 48 | (void) parsed_options; |
| 49 | (void) option_string; |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 50 | out_dir_base_ = "gen-ocaml"; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Init and close methods |
| 55 | */ |
| 56 | |
| 57 | void init_generator(); |
| 58 | void close_generator(); |
| 59 | |
| 60 | /** |
| 61 | * Program-level generation functions |
| 62 | */ |
| 63 | void generate_program (); |
| 64 | void generate_typedef (t_typedef* ttypedef); |
| 65 | void generate_enum (t_enum* tenum); |
| 66 | void generate_const (t_const* tconst); |
| 67 | void generate_struct (t_struct* tstruct); |
| 68 | void generate_xception (t_struct* txception); |
| 69 | void generate_service (t_service* tservice); |
| 70 | |
| 71 | std::string render_const_value(t_type* type, t_const_value* value); |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 72 | bool struct_member_persistent(t_field *tmember); |
| 73 | bool struct_member_omitable(t_field *tmember); |
| 74 | bool struct_member_default_cheaply_comparable(t_field *tmember); |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 75 | std::string struct_member_copy_of(t_type *type, string what); |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Struct generation code |
| 79 | */ |
| 80 | |
| 81 | void generate_ocaml_struct(t_struct* tstruct, bool is_exception); |
| 82 | void generate_ocaml_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false); |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 83 | void generate_ocaml_struct_member(std::ofstream& out, string tname, t_field* tmember); |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 84 | void generate_ocaml_struct_sig(std::ofstream& out, t_struct* tstruct, bool is_exception); |
| 85 | void generate_ocaml_struct_reader(std::ofstream& out, t_struct* tstruct); |
| 86 | void generate_ocaml_struct_writer(std::ofstream& out, t_struct* tstruct); |
| 87 | void generate_ocaml_function_helpers(t_function* tfunction); |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 88 | void generate_ocaml_method_copy(std::ofstream& out, const vector<t_field *>& members); |
| 89 | void generate_ocaml_member_copy(std::ofstream& out, t_field *member); |
David Reiss | 7bb71df | 2008-03-27 21:40:08 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Service-level generation functions |
| 93 | */ |
| 94 | |
| 95 | void generate_service_helpers (t_service* tservice); |
| 96 | void generate_service_interface (t_service* tservice); |
| 97 | void generate_service_client (t_service* tservice); |
| 98 | void generate_service_server (t_service* tservice); |
| 99 | void generate_process_function (t_service* tservice, t_function* tfunction); |
| 100 | |
| 101 | /** |
| 102 | * Serialization constructs |
| 103 | */ |
| 104 | |
| 105 | void generate_deserialize_field (std::ofstream &out, |
| 106 | t_field* tfield, |
| 107 | std::string prefix); |
| 108 | |
| 109 | void generate_deserialize_struct (std::ofstream &out, |
| 110 | t_struct* tstruct); |
| 111 | |
| 112 | void generate_deserialize_container (std::ofstream &out, |
| 113 | t_type* ttype); |
| 114 | |
| 115 | void generate_deserialize_set_element (std::ofstream &out, |
| 116 | t_set* tset); |
| 117 | |
| 118 | |
| 119 | void generate_deserialize_list_element (std::ofstream &out, |
| 120 | t_list* tlist, |
| 121 | std::string prefix=""); |
| 122 | void generate_deserialize_type (std::ofstream &out, |
| 123 | t_type* type); |
| 124 | |
| 125 | void generate_serialize_field (std::ofstream &out, |
| 126 | t_field* tfield, |
| 127 | std::string name= ""); |
| 128 | |
| 129 | void generate_serialize_struct (std::ofstream &out, |
| 130 | t_struct* tstruct, |
| 131 | std::string prefix=""); |
| 132 | |
| 133 | void generate_serialize_container (std::ofstream &out, |
| 134 | t_type* ttype, |
| 135 | std::string prefix=""); |
| 136 | |
| 137 | void generate_serialize_map_element (std::ofstream &out, |
| 138 | t_map* tmap, |
| 139 | std::string kiter, |
| 140 | std::string viter); |
| 141 | |
| 142 | void generate_serialize_set_element (std::ofstream &out, |
| 143 | t_set* tmap, |
| 144 | std::string iter); |
| 145 | |
| 146 | void generate_serialize_list_element (std::ofstream &out, |
| 147 | t_list* tlist, |
| 148 | std::string iter); |
| 149 | |
| 150 | /** |
| 151 | * Helper rendering functions |
| 152 | */ |
| 153 | |
| 154 | std::string ocaml_autogen_comment(); |
| 155 | std::string ocaml_imports(); |
| 156 | std::string type_name(t_type* ttype); |
| 157 | std::string function_signature(t_function* tfunction, std::string prefix=""); |
| 158 | std::string function_type(t_function* tfunc, bool method=false, bool options = false); |
| 159 | std::string argument_list(t_struct* tstruct); |
| 160 | std::string type_to_enum(t_type* ttype); |
| 161 | std::string render_ocaml_type(t_type* type); |
| 162 | |
| 163 | |
| 164 | private: |
| 165 | |
| 166 | /** |
| 167 | * File streams |
| 168 | */ |
| 169 | |
| 170 | std::ofstream f_types_; |
| 171 | std::ofstream f_consts_; |
| 172 | std::ofstream f_service_; |
| 173 | |
| 174 | std::ofstream f_types_i_; |
| 175 | std::ofstream f_service_i_; |
| 176 | |
| 177 | }; |
| 178 | |
| 179 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 180 | /* |
| 181 | * This is necessary because we want typedefs to appear later, |
| 182 | * after all the types have been declared. |
| 183 | */ |
| 184 | void t_ocaml_generator::generate_program() { |
| 185 | // Initialize the generator |
| 186 | init_generator(); |
| 187 | |
| 188 | // Generate enums |
| 189 | vector<t_enum*> enums = program_->get_enums(); |
| 190 | vector<t_enum*>::iterator en_iter; |
| 191 | for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) { |
| 192 | generate_enum(*en_iter); |
| 193 | } |
| 194 | |
| 195 | // Generate structs |
| 196 | vector<t_struct*> structs = program_->get_structs(); |
| 197 | vector<t_struct*>::iterator st_iter; |
| 198 | for (st_iter = structs.begin(); st_iter != structs.end(); ++st_iter) { |
| 199 | generate_struct(*st_iter); |
| 200 | } |
| 201 | |
| 202 | // Generate xceptions |
| 203 | vector<t_struct*> xceptions = program_->get_xceptions(); |
| 204 | vector<t_struct*>::iterator x_iter; |
| 205 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 206 | generate_xception(*x_iter); |
| 207 | } |
| 208 | |
| 209 | // Generate typedefs |
| 210 | vector<t_typedef*> typedefs = program_->get_typedefs(); |
| 211 | vector<t_typedef*>::iterator td_iter; |
| 212 | for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) { |
| 213 | generate_typedef(*td_iter); |
| 214 | } |
| 215 | |
| 216 | // Generate services |
| 217 | vector<t_service*> services = program_->get_services(); |
| 218 | vector<t_service*>::iterator sv_iter; |
| 219 | for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) { |
| 220 | service_name_ = get_service_name(*sv_iter); |
| 221 | generate_service(*sv_iter); |
| 222 | } |
| 223 | |
| 224 | // Generate constants |
| 225 | vector<t_const*> consts = program_->get_consts(); |
| 226 | generate_consts(consts); |
| 227 | |
| 228 | // Close the generator |
| 229 | close_generator(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Prepares for file generation by opening up the necessary file output |
| 235 | * streams. |
| 236 | * |
| 237 | * @param tprogram The program to generate |
| 238 | */ |
| 239 | void t_ocaml_generator::init_generator() { |
| 240 | // Make output directory |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 241 | MKDIR(get_out_dir().c_str()); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 242 | |
| 243 | // Make output file |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 244 | string f_types_name = get_out_dir()+program_name_+"_types.ml"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 245 | f_types_.open(f_types_name.c_str()); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 246 | string f_types_i_name = get_out_dir()+program_name_+"_types.mli"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 247 | f_types_i_.open(f_types_i_name.c_str()); |
| 248 | |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 249 | string f_consts_name = get_out_dir()+program_name_+"_consts.ml"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 250 | f_consts_.open(f_consts_name.c_str()); |
| 251 | |
| 252 | // Print header |
| 253 | f_types_ << |
| 254 | ocaml_autogen_comment() << endl << |
| 255 | ocaml_imports() << endl; |
| 256 | f_types_i_ << |
| 257 | ocaml_autogen_comment() << endl << |
| 258 | ocaml_imports() << endl; |
| 259 | f_consts_ << |
| 260 | ocaml_autogen_comment() << endl << |
| 261 | ocaml_imports() << endl << |
| 262 | "open " << capitalize(program_name_)<<"_types"<< endl; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Autogen'd comment |
| 268 | */ |
| 269 | string t_ocaml_generator::ocaml_autogen_comment() { |
| 270 | return |
| 271 | std::string("(*\n") + |
Roger Meier | 08d4681 | 2011-04-12 19:08:21 +0000 | [diff] [blame] | 272 | " Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 273 | "\n" + |
| 274 | " DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING\n" + |
| 275 | "*)\n"; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Prints standard thrift imports |
| 280 | */ |
| 281 | string t_ocaml_generator::ocaml_imports() { |
| 282 | return "open Thrift"; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Closes the type files |
| 287 | */ |
| 288 | void t_ocaml_generator::close_generator() { |
| 289 | // Close types file |
| 290 | f_types_.close(); |
| 291 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 292 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 293 | /** |
| 294 | * Generates a typedef. Ez. |
| 295 | * |
| 296 | * @param ttypedef The type definition |
| 297 | */ |
| 298 | void t_ocaml_generator::generate_typedef(t_typedef* ttypedef) { |
| 299 | f_types_ << |
| 300 | indent() << "type "<< decapitalize(ttypedef->get_symbolic()) << " = " << render_ocaml_type(ttypedef->get_type()) << endl << endl; |
| 301 | f_types_i_ << |
| 302 | indent() << "type "<< decapitalize(ttypedef->get_symbolic()) << " = " << render_ocaml_type(ttypedef->get_type()) << endl << endl; |
| 303 | } |
| 304 | |
| 305 | /** |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 306 | * Generates code for an enumerated type. |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 307 | * the values. |
| 308 | * |
| 309 | * @param tenum The enumeration |
| 310 | */ |
| 311 | void t_ocaml_generator::generate_enum(t_enum* tenum) { |
| 312 | indent(f_types_) << "module " << capitalize(tenum->get_name()) << " = " << endl << "struct" << endl; |
| 313 | indent(f_types_i_) << "module " << capitalize(tenum->get_name()) << " : " << endl << "sig" << endl; |
| 314 | indent_up(); |
| 315 | indent(f_types_) << "type t = " << endl; |
| 316 | indent(f_types_i_) << "type t = " << endl; |
| 317 | indent_up(); |
| 318 | vector<t_enum_value*> constants = tenum->get_constants(); |
| 319 | vector<t_enum_value*>::iterator c_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 320 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
| 321 | string name = capitalize((*c_iter)->get_name()); |
| 322 | indent(f_types_) << "| " << name << endl; |
| 323 | indent(f_types_i_) << "| " << name << endl; |
| 324 | } |
| 325 | indent_down(); |
| 326 | |
| 327 | indent(f_types_) << "let to_i = function" << endl; |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 328 | indent(f_types_i_) << "val to_i : t -> Int32.t" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 329 | indent_up(); |
| 330 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
Bryan Duxbury | a406b90 | 2010-09-27 23:37:44 +0000 | [diff] [blame] | 331 | int value = (*c_iter)->get_value(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 332 | string name = capitalize((*c_iter)->get_name()); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 333 | indent(f_types_) << "| " << name << " -> " << value << "l" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 334 | } |
| 335 | indent_down(); |
| 336 | |
| 337 | indent(f_types_) << "let of_i = function" << endl; |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 338 | indent(f_types_i_) << "val of_i : Int32.t -> t" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 339 | indent_up(); |
| 340 | for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
Bryan Duxbury | a406b90 | 2010-09-27 23:37:44 +0000 | [diff] [blame] | 341 | int value = (*c_iter)->get_value(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 342 | string name = capitalize((*c_iter)->get_name()); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 343 | indent(f_types_) << "| " << value << "l -> " << name << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 344 | } |
| 345 | indent(f_types_) << "| _ -> raise Thrift_error" << endl; |
| 346 | indent_down(); |
| 347 | indent_down(); |
| 348 | indent(f_types_) << "end" << endl; |
| 349 | indent(f_types_i_) << "end" << endl; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Generate a constant value |
| 354 | */ |
| 355 | void t_ocaml_generator::generate_const(t_const* tconst) { |
| 356 | t_type* type = tconst->get_type(); |
| 357 | string name = decapitalize(tconst->get_name()); |
| 358 | t_const_value* value = tconst->get_value(); |
| 359 | |
| 360 | indent(f_consts_) << "let " << name << " = " << render_const_value(type, value) << endl << endl; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Prints the value of a constant with the given type. Note that type checking |
| 365 | * is NOT performed in this function as it is always run beforehand using the |
| 366 | * validate_types method in main.cc |
| 367 | */ |
| 368 | string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value) { |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 369 | type = get_true_type(type); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 370 | std::ostringstream out; |
Bryan Duxbury | ed2dc53 | 2010-12-13 19:17:22 +0000 | [diff] [blame] | 371 | // OCaml requires all floating point numbers contain a decimal point |
| 372 | out.setf(ios::showpoint); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 373 | if (type->is_base_type()) { |
| 374 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 375 | switch (tbase) { |
| 376 | case t_base_type::TYPE_STRING: |
David Reiss | 82e6fc0 | 2009-03-26 23:32:36 +0000 | [diff] [blame] | 377 | out << '"' << get_escaped_string(value) << '"'; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 378 | break; |
| 379 | case t_base_type::TYPE_BOOL: |
| 380 | out << (value->get_integer() > 0 ? "true" : "false"); |
| 381 | break; |
| 382 | case t_base_type::TYPE_BYTE: |
| 383 | case t_base_type::TYPE_I16: |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 384 | out << value->get_integer(); |
| 385 | break; |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 386 | case t_base_type::TYPE_I32: |
| 387 | out << value->get_integer() << "l"; |
| 388 | break; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 389 | case t_base_type::TYPE_I64: |
| 390 | out << value->get_integer() << "L"; |
| 391 | break; |
| 392 | case t_base_type::TYPE_DOUBLE: |
| 393 | if (value->get_type() == t_const_value::CV_INTEGER) { |
Bryan Duxbury | ed2dc53 | 2010-12-13 19:17:22 +0000 | [diff] [blame] | 394 | out << value->get_integer() << ".0"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 395 | } else { |
| 396 | out << value->get_double(); |
| 397 | } |
| 398 | break; |
| 399 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 400 | throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 401 | } |
| 402 | } else if (type->is_enum()) { |
| 403 | t_enum* tenum = (t_enum*)type; |
| 404 | vector<t_enum_value*> constants = tenum->get_constants(); |
| 405 | vector<t_enum_value*>::iterator c_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 406 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
Bryan Duxbury | a406b90 | 2010-09-27 23:37:44 +0000 | [diff] [blame] | 407 | int val = (*c_iter)->get_value(); |
| 408 | if (val == value->get_integer()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 409 | indent(out) << capitalize(tenum->get_name()) << "." << capitalize((*c_iter)->get_name()); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | } else if (type->is_struct() || type->is_xception()) { |
| 414 | string cname = type_name(type); |
| 415 | string ct = tmp("_c"); |
| 416 | out << endl; |
| 417 | indent_up(); |
| 418 | indent(out) << "(let " << ct << " = new " << cname << " in" << endl; |
| 419 | indent_up(); |
| 420 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 421 | vector<t_field*>::const_iterator f_iter; |
| 422 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 423 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 424 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 425 | t_type* field_type = NULL; |
| 426 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 427 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 428 | field_type = (*f_iter)->get_type(); |
| 429 | } |
| 430 | } |
| 431 | if (field_type == NULL) { |
| 432 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 433 | } |
| 434 | string fname = v_iter->first->get_string(); |
| 435 | out << indent(); |
| 436 | out << ct <<"#set_" << fname << " "; |
| 437 | out << render_const_value(field_type, v_iter->second); |
| 438 | out << ";" << endl; |
| 439 | } |
| 440 | indent(out) << ct << ")"; |
| 441 | indent_down(); |
| 442 | indent_down(); |
| 443 | } else if (type->is_map()) { |
| 444 | t_type* ktype = ((t_map*)type)->get_key_type(); |
| 445 | t_type* vtype = ((t_map*)type)->get_val_type(); |
| 446 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 447 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 448 | string hm = tmp("_hm"); |
| 449 | out << endl; |
| 450 | indent_up(); |
| 451 | indent(out) << "(let " << hm << " = Hashtbl.create " << val.size() << " in" << endl; |
| 452 | indent_up(); |
| 453 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 454 | string key = render_const_value(ktype, v_iter->first); |
| 455 | string val = render_const_value(vtype, v_iter->second); |
| 456 | indent(out) << "Hashtbl.add " << hm << " " << key << " " << val << ";" << endl; |
| 457 | } |
| 458 | indent(out) << hm << ")"; |
| 459 | indent_down(); |
| 460 | indent_down(); |
| 461 | } else if (type->is_list()) { |
| 462 | t_type* etype; |
| 463 | etype = ((t_list*)type)->get_elem_type(); |
| 464 | out << "[" << endl; |
| 465 | indent_up(); |
| 466 | const vector<t_const_value*>& val = value->get_list(); |
| 467 | vector<t_const_value*>::const_iterator v_iter; |
| 468 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 469 | out << indent(); |
| 470 | out << render_const_value(etype, *v_iter); |
| 471 | out << ";" << endl; |
| 472 | } |
| 473 | indent_down(); |
| 474 | indent(out) << "]"; |
| 475 | } else if (type->is_set()) { |
| 476 | t_type* etype = ((t_set*)type)->get_elem_type(); |
| 477 | const vector<t_const_value*>& val = value->get_list(); |
| 478 | vector<t_const_value*>::const_iterator v_iter; |
| 479 | string hm = tmp("_hm"); |
| 480 | indent(out) << "(let " << hm << " = Hashtbl.create " << val.size() << " in" << endl; |
| 481 | indent_up(); |
| 482 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 483 | string val = render_const_value(etype, *v_iter); |
| 484 | indent(out) << "Hashtbl.add " << hm << " " << val << " true;" << endl; |
| 485 | } |
| 486 | indent(out) << hm << ")" << endl; |
| 487 | indent_down(); |
| 488 | out << endl; |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 489 | } else { |
| 490 | throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 491 | } |
| 492 | return out.str(); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Generates a "struct" |
| 497 | */ |
| 498 | void t_ocaml_generator::generate_struct(t_struct* tstruct) { |
| 499 | generate_ocaml_struct(tstruct, false); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Generates a struct definition for a thrift exception. Basically the same |
| 504 | * as a struct, but also has an exception declaration. |
| 505 | * |
| 506 | * @param txception The struct definition |
| 507 | */ |
| 508 | void t_ocaml_generator::generate_xception(t_struct* txception) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 509 | generate_ocaml_struct(txception, true); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Generates an OCaml struct |
| 514 | */ |
| 515 | void t_ocaml_generator::generate_ocaml_struct(t_struct* tstruct, |
| 516 | bool is_exception) { |
| 517 | generate_ocaml_struct_definition(f_types_, tstruct, is_exception); |
| 518 | generate_ocaml_struct_sig(f_types_i_,tstruct,is_exception); |
| 519 | } |
| 520 | |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 521 | void t_ocaml_generator::generate_ocaml_method_copy(ofstream& out, |
| 522 | const vector<t_field *>& members) { |
| 523 | vector<t_field*>::const_iterator m_iter; |
| 524 | |
| 525 | /* Create a copy of the current object */ |
| 526 | indent(out) << "method copy =" << endl; |
| 527 | indent_up(); indent_up(); |
| 528 | indent(out) << "let _new = Oo.copy self in" << endl; |
| 529 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) |
| 530 | generate_ocaml_member_copy(out, *m_iter); |
| 531 | |
| 532 | indent_down(); |
| 533 | indent(out) << "_new" << endl; |
| 534 | indent_down(); |
| 535 | } |
| 536 | |
| 537 | string t_ocaml_generator::struct_member_copy_of(t_type *type, string what) { |
| 538 | if (type->is_struct() || type->is_xception()) { |
| 539 | return what + string ("#copy"); |
| 540 | } if (type->is_map()) { |
| 541 | string copy_of_k = struct_member_copy_of(((t_map *)type)->get_key_type(), "k"); |
| 542 | string copy_of_v = struct_member_copy_of(((t_map *)type)->get_val_type(), "v"); |
| 543 | |
| 544 | if(copy_of_k == "k" && copy_of_v == "v") { |
| 545 | return string ("(Hashtbl.copy ") + what + string(")"); |
| 546 | } else { |
| 547 | return string ("((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v -> Hashtbl.add nh ") |
| 548 | + copy_of_k + string(" ") + copy_of_v |
| 549 | + string(") oh; nh) ") |
| 550 | + what + ")"; |
| 551 | } |
| 552 | } if (type->is_set()) { |
| 553 | string copy_of = struct_member_copy_of(((t_set *)type)->get_elem_type(), "k"); |
| 554 | |
| 555 | if(copy_of == "k") { |
| 556 | return string ("(Hashtbl.copy ") + what + string(")"); |
| 557 | } else { |
| 558 | return string ("((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v -> Hashtbl.add nh ") |
| 559 | + copy_of + string(" true") |
| 560 | + string(") oh; nh) ") |
| 561 | + what + ")"; |
| 562 | } |
| 563 | } if (type->is_list()) { |
| 564 | string copy_of = struct_member_copy_of(((t_list *)type)->get_elem_type(), "x"); |
| 565 | if(copy_of != "x") { |
| 566 | return string("(List.map (fun x -> ") |
| 567 | + copy_of + string (") ") |
| 568 | + what + string(")"); |
| 569 | } else { |
| 570 | return what; |
| 571 | } |
| 572 | } |
| 573 | return what; |
| 574 | } |
| 575 | |
| 576 | void t_ocaml_generator::generate_ocaml_member_copy(ofstream& out, |
| 577 | t_field *tmember) { |
| 578 | string mname = decapitalize(tmember->get_name()); |
| 579 | t_type* type = get_true_type(tmember->get_type()); |
| 580 | |
| 581 | string grab_field = string("self#grab_") + mname; |
| 582 | string copy_of = struct_member_copy_of(type, grab_field); |
| 583 | if(copy_of != grab_field) { |
| 584 | indent(out); |
| 585 | if(!struct_member_persistent(tmember)) { |
| 586 | out << "if _" << mname << " <> None then" << endl; |
| 587 | indent(out) << " "; |
| 588 | } |
| 589 | out << "_new#set_" << mname << " " << copy_of << ";" << endl; |
| 590 | } |
| 591 | } |
| 592 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 593 | /** |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 594 | * Generates a struct definition for a thrift data type. |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 595 | * |
| 596 | * @param tstruct The struct definition |
| 597 | */ |
| 598 | void t_ocaml_generator::generate_ocaml_struct_definition(ofstream& out, |
| 599 | t_struct* tstruct, |
| 600 | bool is_exception) { |
| 601 | const vector<t_field*>& members = tstruct->get_members(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 602 | vector<t_field*>::const_iterator m_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 603 | string tname = type_name(tstruct); |
| 604 | indent(out) << "class " << tname << " =" << endl; |
| 605 | indent(out) << "object (self)" << endl; |
| 606 | |
| 607 | indent_up(); |
| 608 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 609 | if (members.size() > 0) { |
| 610 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 611 | generate_ocaml_struct_member(out, tname, (*m_iter)); |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 612 | out << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 615 | generate_ocaml_method_copy(out, members); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 616 | generate_ocaml_struct_writer(out, tstruct); |
| 617 | indent_down(); |
| 618 | indent(out) << "end" << endl; |
| 619 | |
| 620 | if(is_exception){ |
| 621 | indent(out) << "exception " << capitalize(tname) <<" of " << tname << endl; |
| 622 | } |
| 623 | |
| 624 | generate_ocaml_struct_reader(out, tstruct); |
| 625 | } |
| 626 | |
| 627 | /** |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 628 | * Generates a structure member for a thrift data type. |
| 629 | * |
| 630 | * @param tname Name of the parent structure for the member |
| 631 | * @param tmember Member definition |
| 632 | */ |
| 633 | void t_ocaml_generator::generate_ocaml_struct_member(ofstream& out, |
| 634 | string tname, |
| 635 | t_field* tmember) { |
| 636 | string x = tmp("_x"); |
| 637 | string mname = decapitalize(tmember->get_name()); |
| 638 | |
| 639 | indent(out) << "val mutable _" << mname << " : " << render_ocaml_type(tmember->get_type()); |
| 640 | t_const_value *val = tmember->get_value(); |
| 641 | if(val) { |
| 642 | if(struct_member_persistent(tmember)) |
| 643 | out << " = " << render_const_value(tmember->get_type(), tmember->get_value()) << endl; |
| 644 | else |
| 645 | out << " option = Some " << render_const_value(tmember->get_type(), tmember->get_value()) << endl; |
| 646 | } else { |
| 647 | // assert(!struct_member_persistent(tmember)) |
| 648 | out << " option = None" << endl; |
| 649 | } |
| 650 | |
| 651 | if(struct_member_persistent(tmember)) { |
| 652 | indent(out) << "method get_" << mname << " = Some _" << mname << endl; |
| 653 | indent(out) << "method grab_" << mname << " = _" << mname << endl; |
| 654 | indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- " << x << endl; |
| 655 | } else { |
| 656 | indent(out) << "method get_" << mname << " = _" << mname << endl; |
| 657 | indent(out) << "method grab_" << mname << " = match _"<<mname<<" with None->raise (Field_empty \""<<tname<<"."<<mname<<"\") | Some " << x <<" -> " << x << endl; |
| 658 | indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- Some " << x << endl; |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 659 | indent(out) << "method unset_" << mname << " = _" << mname << " <- None" << endl; |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 660 | } |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 661 | |
| 662 | indent(out) << "method reset_" << mname << " = _" << mname << " <- "; |
| 663 | if(val) { |
| 664 | if(struct_member_persistent(tmember)) |
| 665 | out << render_const_value(tmember->get_type(), tmember->get_value()) << endl; |
| 666 | else |
| 667 | out << "Some " << render_const_value(tmember->get_type(), tmember->get_value()) << endl; |
| 668 | } else { |
| 669 | out << "None" << endl; |
| 670 | } |
| 671 | |
| 672 | |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Check whether a member of the structure can not have undefined value |
| 677 | * |
| 678 | * @param tmember Member definition |
| 679 | */ |
| 680 | bool t_ocaml_generator::struct_member_persistent(t_field *tmember) { |
| 681 | t_const_value *val = tmember->get_value(); |
| 682 | return (val ? true : false); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Check whether a member of the structure can be skipped during encoding |
| 687 | * |
| 688 | * @param tmember Member definition |
| 689 | */ |
| 690 | bool t_ocaml_generator::struct_member_omitable(t_field *tmember) { |
| 691 | return (tmember->get_req() != t_field::T_REQUIRED); |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Figure out whether a member of the structure has |
| 696 | * a cheaply comparable default value. |
| 697 | * |
| 698 | * @param tmember Member definition |
| 699 | */ |
| 700 | bool t_ocaml_generator::struct_member_default_cheaply_comparable(t_field *tmember) { |
| 701 | t_type* type = get_true_type(tmember->get_type()); |
| 702 | t_const_value *val = tmember->get_value(); |
| 703 | if(!val) { |
| 704 | return false; |
| 705 | } else if(type->is_base_type()) { |
| 706 | // Base types are generally cheaply compared for structural equivalence. |
| 707 | switch(((t_base_type*)type)->get_base()) { |
| 708 | case t_base_type::TYPE_DOUBLE: |
| 709 | if(val->get_double() == 0.0) |
| 710 | return true; |
| 711 | else |
| 712 | return false; |
| 713 | default: |
| 714 | return true; |
| 715 | } |
| 716 | } else if(type->is_list()) { |
| 717 | // Empty lists are cheaply compared for structural equivalence. |
| 718 | // Is empty list? |
| 719 | if(val->get_list().size() == 0) |
| 720 | return true; |
| 721 | else |
| 722 | return false; |
| 723 | } else { |
| 724 | return false; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /** |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 729 | * Generates a struct definition for a thrift data type. |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 730 | * |
| 731 | * @param tstruct The struct definition |
| 732 | */ |
| 733 | void t_ocaml_generator::generate_ocaml_struct_sig(ofstream& out, |
| 734 | t_struct* tstruct, |
| 735 | bool is_exception) { |
| 736 | const vector<t_field*>& members = tstruct->get_members(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 737 | vector<t_field*>::const_iterator m_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 738 | string tname = type_name(tstruct); |
| 739 | indent(out) << "class " << tname << " :" << endl; |
Bryan Duxbury | ed2dc53 | 2010-12-13 19:17:22 +0000 | [diff] [blame] | 740 | indent(out) << "object ('a)" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 741 | |
| 742 | indent_up(); |
| 743 | |
| 744 | string x = tmp("_x"); |
| 745 | if (members.size() > 0) { |
| 746 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 747 | string mname = decapitalize((*m_iter)->get_name()); |
| 748 | string type = render_ocaml_type((*m_iter)->get_type()); |
| 749 | indent(out) << "method get_" << mname << " : " << type << " option" << endl; |
| 750 | indent(out) << "method grab_" << mname << " : " << type << endl; |
| 751 | indent(out) << "method set_" << mname << " : " << type << " -> unit" << endl; |
Bryan Duxbury | 0f4078d | 2010-09-02 00:07:20 +0000 | [diff] [blame] | 752 | if(!struct_member_persistent(*m_iter)) |
| 753 | indent(out) << "method unset_" << mname << " : unit" << endl; |
| 754 | indent(out) << "method reset_" << mname << " : unit" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
Bryan Duxbury | ed2dc53 | 2010-12-13 19:17:22 +0000 | [diff] [blame] | 757 | indent(out) << "method copy : 'a" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 758 | indent(out) << "method write : Protocol.t -> unit" << endl; |
| 759 | indent_down(); |
| 760 | indent(out) << "end" << endl; |
| 761 | |
| 762 | if(is_exception){ |
| 763 | indent(out) << "exception " << capitalize(tname) <<" of " << tname << endl; |
| 764 | } |
| 765 | |
| 766 | indent(out) << "val read_" << tname << " : Protocol.t -> " << tname << endl; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Generates the read method for a struct |
| 771 | */ |
| 772 | void t_ocaml_generator::generate_ocaml_struct_reader(ofstream& out, t_struct* tstruct) { |
| 773 | const vector<t_field*>& fields = tstruct->get_members(); |
| 774 | vector<t_field*>::const_iterator f_iter; |
| 775 | string sname = type_name(tstruct); |
| 776 | string str = tmp("_str"); |
| 777 | string t = tmp("_t"); |
| 778 | string id = tmp("_id"); |
| 779 | indent(out) << |
| 780 | "let rec read_" << sname << " (iprot : Protocol.t) =" << endl; |
| 781 | indent_up(); |
| 782 | indent(out) << "let " << str << " = new " << sname << " in" << endl; |
| 783 | indent_up(); |
| 784 | indent(out) << |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 785 | "ignore(iprot#readStructBegin);" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 786 | |
| 787 | // Loop over reading in fields |
| 788 | indent(out) << |
| 789 | "(try while true do" << endl; |
| 790 | indent_up(); |
| 791 | indent_up(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 792 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 793 | // Read beginning field marker |
| 794 | indent(out) << |
| 795 | "let (_," << t <<","<<id<<") = iprot#readFieldBegin in" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 796 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 797 | // Check for field STOP marker and break |
| 798 | indent(out) << |
| 799 | "if " << t <<" = Protocol.T_STOP then" << endl; |
| 800 | indent_up(); |
| 801 | indent(out) << |
| 802 | "raise Break" << endl; |
| 803 | indent_down(); |
| 804 | indent(out) << "else ();" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 805 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 806 | indent(out) << "(match " << id<<" with " << endl; |
| 807 | indent_up(); |
| 808 | // Generate deserialization code for known cases |
| 809 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 810 | indent(out) << "| " << (*f_iter)->get_key() << " -> ("; |
| 811 | out << "if " << t <<" = " << type_to_enum((*f_iter)->get_type()) << " then" << endl; |
| 812 | indent_up(); |
| 813 | indent_up(); |
| 814 | generate_deserialize_field(out, *f_iter,str); |
| 815 | indent_down(); |
| 816 | out << |
| 817 | indent() << "else" << endl << |
| 818 | indent() << " iprot#skip "<< t << ")" << endl; |
| 819 | indent_down(); |
| 820 | } |
| 821 | |
| 822 | // In the default case we skip the field |
| 823 | out << |
| 824 | indent() << "| _ -> " << "iprot#skip "<<t<<");" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 825 | indent_down(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 826 | // Read field end marker |
| 827 | indent(out) << "iprot#readFieldEnd;" << endl; |
| 828 | indent_down(); |
| 829 | indent(out) << "done; ()" << endl; |
| 830 | indent_down(); |
| 831 | indent(out) << "with Break -> ());" << endl; |
| 832 | |
| 833 | indent(out) << |
| 834 | "iprot#readStructEnd;" << endl; |
| 835 | |
| 836 | indent(out) << str << endl << endl; |
| 837 | indent_down(); |
| 838 | indent_down(); |
| 839 | } |
| 840 | |
| 841 | void t_ocaml_generator::generate_ocaml_struct_writer(ofstream& out, |
| 842 | t_struct* tstruct) { |
| 843 | string name = tstruct->get_name(); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 844 | const vector<t_field*>& fields = tstruct->get_sorted_members(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 845 | vector<t_field*>::const_iterator f_iter; |
| 846 | string str = tmp("_str"); |
| 847 | string f = tmp("_f"); |
| 848 | |
| 849 | indent(out) << |
| 850 | "method write (oprot : Protocol.t) =" << endl; |
| 851 | indent_up(); |
| 852 | indent(out) << |
| 853 | "oprot#writeStructBegin \""<<name<<"\";" << endl; |
| 854 | |
| 855 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 856 | t_field *tmember = (*f_iter); |
| 857 | string mname = "_"+decapitalize(tmember->get_name()); |
| 858 | string _v; |
| 859 | |
| 860 | if(struct_member_persistent(tmember)) { |
| 861 | |
| 862 | if(struct_member_omitable(tmember) |
| 863 | && struct_member_default_cheaply_comparable(tmember)) { |
| 864 | _v = "_v"; |
| 865 | // Avoid redundant encoding of members having default values. |
| 866 | indent(out) << "(match " << mname << " with " |
| 867 | << render_const_value(tmember->get_type(), tmember->get_value()) |
| 868 | << " -> () | " << _v << " -> " << endl; |
| 869 | } else { |
| 870 | _v = mname; |
| 871 | indent(out) << "(" << endl; |
| 872 | } |
| 873 | |
| 874 | } else { |
| 875 | |
| 876 | indent(out) << "(match " << mname << " with "; |
| 877 | |
| 878 | if(struct_member_omitable(tmember)) { |
| 879 | out << "None -> ()"; |
| 880 | |
| 881 | if(struct_member_default_cheaply_comparable(tmember)) { |
| 882 | // Avoid redundant encoding of members having default values. |
| 883 | out << " | Some " |
| 884 | << render_const_value(tmember->get_type(), tmember->get_value()) |
| 885 | << " -> ()"; |
| 886 | } |
| 887 | out << " | Some _v -> " << endl; |
| 888 | } else { |
| 889 | out << endl; |
| 890 | indent(out) << "| None -> raise (Field_empty \"" |
| 891 | << type_name(tstruct) << "." << mname << "\")" << endl; |
| 892 | indent(out) << "| Some _v -> " << endl; |
| 893 | } |
| 894 | |
| 895 | _v = "_v"; |
| 896 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 897 | indent_up(); |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 898 | // Write field header |
| 899 | indent(out) << "oprot#writeFieldBegin(\""<< tmember->get_name()<<"\"," |
| 900 | << type_to_enum(tmember->get_type()) << "," |
| 901 | << tmember->get_key()<<");" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 902 | |
| 903 | // Write field contents |
Bryan Duxbury | 66c3347 | 2010-08-12 23:37:47 +0000 | [diff] [blame] | 904 | generate_serialize_field(out, tmember, _v); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 905 | |
| 906 | // Write field closer |
| 907 | indent(out) << "oprot#writeFieldEnd" << endl; |
| 908 | |
| 909 | indent_down(); |
| 910 | indent(out) << ");" << endl; |
| 911 | } |
| 912 | |
| 913 | // Write the struct map |
| 914 | out << |
| 915 | indent() << "oprot#writeFieldStop;" << endl << |
| 916 | indent() << "oprot#writeStructEnd" << endl; |
| 917 | |
| 918 | indent_down(); |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Generates a thrift service. |
| 923 | * |
| 924 | * @param tservice The service definition |
| 925 | */ |
| 926 | void t_ocaml_generator::generate_service(t_service* tservice) { |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 927 | string f_service_name = get_out_dir()+capitalize(service_name_)+".ml"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 928 | f_service_.open(f_service_name.c_str()); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 929 | string f_service_i_name = get_out_dir()+capitalize(service_name_)+".mli"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 930 | f_service_i_.open(f_service_i_name.c_str()); |
| 931 | |
| 932 | f_service_ << |
| 933 | ocaml_autogen_comment() << endl << |
| 934 | ocaml_imports() << endl; |
| 935 | f_service_i_ << |
| 936 | ocaml_autogen_comment() << endl << |
| 937 | ocaml_imports() << endl; |
| 938 | |
| 939 | /* if (tservice->get_extends() != NULL) { |
| 940 | f_service_ << |
| 941 | "open " << capitalize(tservice->get_extends()->get_name()) << endl; |
| 942 | f_service_i_ << |
| 943 | "open " << capitalize(tservice->get_extends()->get_name()) << endl; |
| 944 | } |
| 945 | */ |
| 946 | f_service_ << |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 947 | "open " << capitalize(program_name_) << "_types" << endl << |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 948 | endl; |
| 949 | |
| 950 | f_service_i_ << |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 951 | "open " << capitalize(program_name_) << "_types" << endl << |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 952 | endl; |
| 953 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 954 | // Generate the three main parts of the service |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 955 | generate_service_helpers(tservice); |
| 956 | generate_service_interface(tservice); |
| 957 | generate_service_client(tservice); |
| 958 | generate_service_server(tservice); |
| 959 | |
| 960 | |
| 961 | // Close service file |
| 962 | f_service_.close(); |
| 963 | f_service_i_.close(); |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Generates helper functions for a service. |
| 968 | * |
| 969 | * @param tservice The service to generate a header definition for |
| 970 | */ |
| 971 | void t_ocaml_generator::generate_service_helpers(t_service* tservice) { |
| 972 | vector<t_function*> functions = tservice->get_functions(); |
| 973 | vector<t_function*>::iterator f_iter; |
| 974 | |
| 975 | indent(f_service_) << |
| 976 | "(* HELPER FUNCTIONS AND STRUCTURES *)" << endl << endl; |
| 977 | |
| 978 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 979 | t_struct* ts = (*f_iter)->get_arglist(); |
| 980 | generate_ocaml_struct_definition(f_service_, ts, false); |
| 981 | generate_ocaml_function_helpers(*f_iter); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Generates a struct and helpers for a function. |
| 987 | * |
| 988 | * @param tfunction The function |
| 989 | */ |
| 990 | void t_ocaml_generator::generate_ocaml_function_helpers(t_function* tfunction) { |
| 991 | t_struct result(program_, decapitalize(tfunction->get_name()) + "_result"); |
| 992 | t_field success(tfunction->get_returntype(), "success", 0); |
| 993 | if (!tfunction->get_returntype()->is_void()) { |
| 994 | result.append(&success); |
| 995 | } |
| 996 | |
| 997 | t_struct* xs = tfunction->get_xceptions(); |
| 998 | const vector<t_field*>& fields = xs->get_members(); |
| 999 | vector<t_field*>::const_iterator f_iter; |
| 1000 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1001 | result.append(*f_iter); |
| 1002 | } |
| 1003 | generate_ocaml_struct_definition(f_service_, &result, false); |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Generates a service interface definition. |
| 1008 | * |
| 1009 | * @param tservice The service to generate a header definition for |
| 1010 | */ |
| 1011 | void t_ocaml_generator::generate_service_interface(t_service* tservice) { |
| 1012 | f_service_ << |
| 1013 | indent() << "class virtual iface =" << endl << "object (self)" << endl; |
| 1014 | f_service_i_ << |
| 1015 | indent() << "class virtual iface :" << endl << "object" << endl; |
| 1016 | |
| 1017 | indent_up(); |
| 1018 | |
| 1019 | if (tservice->get_extends() != NULL) { |
| 1020 | string extends = type_name(tservice->get_extends()); |
| 1021 | indent(f_service_) << "inherit " << extends << ".iface" << endl; |
| 1022 | indent(f_service_i_) << "inherit " << extends << ".iface" << endl; |
| 1023 | } |
| 1024 | |
| 1025 | vector<t_function*> functions = tservice->get_functions(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1026 | vector<t_function*>::iterator f_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1027 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 1028 | string ft = function_type(*f_iter,true,true); |
| 1029 | f_service_ << |
| 1030 | indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : " << ft << endl; |
| 1031 | f_service_i_ << |
| 1032 | indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : " << ft << endl; |
| 1033 | } |
| 1034 | indent_down(); |
| 1035 | indent(f_service_) << "end" << endl << endl; |
| 1036 | indent(f_service_i_) << "end" << endl << endl; |
| 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * Generates a service client definition. Note that in OCaml, the client doesn't implement iface. This is because |
| 1041 | * The client does not (and should not have to) deal with arguments being None. |
| 1042 | * |
| 1043 | * @param tservice The service to generate a server for. |
| 1044 | */ |
| 1045 | void t_ocaml_generator::generate_service_client(t_service* tservice) { |
| 1046 | string extends = ""; |
| 1047 | indent(f_service_) << |
| 1048 | "class client (iprot : Protocol.t) (oprot : Protocol.t) =" << endl << "object (self)" << endl; |
| 1049 | indent(f_service_i_) << |
| 1050 | "class client : Protocol.t -> Protocol.t -> " << endl << "object" << endl; |
| 1051 | indent_up(); |
| 1052 | |
| 1053 | |
| 1054 | if (tservice->get_extends() != NULL) { |
| 1055 | extends = type_name(tservice->get_extends()); |
| 1056 | indent(f_service_) << "inherit " << extends << ".client iprot oprot as super" << endl; |
| 1057 | indent(f_service_i_) << "inherit " << extends << ".client" << endl; |
| 1058 | } |
| 1059 | indent(f_service_) << "val mutable seqid = 0" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1060 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1061 | |
| 1062 | // Generate client method implementations |
| 1063 | vector<t_function*> functions = tservice->get_functions(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1064 | vector<t_function*>::const_iterator f_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1065 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 1066 | t_struct* arg_struct = (*f_iter)->get_arglist(); |
| 1067 | const vector<t_field*>& fields = arg_struct->get_members(); |
| 1068 | vector<t_field*>::const_iterator fld_iter; |
| 1069 | string funname = (*f_iter)->get_name(); |
| 1070 | |
| 1071 | // Open function |
| 1072 | indent(f_service_) << |
| 1073 | "method " << function_signature(*f_iter) << " = " << endl; |
| 1074 | indent(f_service_i_) << |
| 1075 | "method " << decapitalize((*f_iter)->get_name()) << " : " << function_type(*f_iter,true,false) << endl; |
| 1076 | indent_up(); |
| 1077 | indent(f_service_) << |
| 1078 | "self#send_" << funname; |
| 1079 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1080 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1081 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 1082 | f_service_ << " " << decapitalize((*fld_iter)->get_name()); |
| 1083 | } |
| 1084 | f_service_ << ";" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1085 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1086 | if (!(*f_iter)->is_oneway()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1087 | f_service_ << indent(); |
| 1088 | f_service_ << |
| 1089 | "self#recv_" << funname << endl; |
| 1090 | } |
| 1091 | indent_down(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1092 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1093 | indent(f_service_) << |
| 1094 | "method private send_" << function_signature(*f_iter) << " = " << endl; |
| 1095 | indent_up(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1096 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1097 | std::string argsname = decapitalize((*f_iter)->get_name() + "_args"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1098 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1099 | // Serialize the request header |
| 1100 | f_service_ << |
| 1101 | indent() << "oprot#writeMessageBegin (\"" << (*f_iter)->get_name() << "\", Protocol.CALL, seqid);" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1102 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1103 | f_service_ << |
| 1104 | indent() << "let args = new " << argsname << " in" << endl; |
| 1105 | indent_up(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1106 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1107 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 1108 | f_service_ << |
| 1109 | indent() << "args#set_" << (*fld_iter)->get_name() << " " << (*fld_iter)->get_name() << ";" << endl; |
| 1110 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1111 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1112 | // Write to the stream |
| 1113 | f_service_ << |
| 1114 | indent() << "args#write oprot;" << endl << |
| 1115 | indent() << "oprot#writeMessageEnd;" << endl << |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1116 | indent() << "oprot#getTransport#flush" << endl; |
| 1117 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1118 | indent_down(); |
| 1119 | indent_down(); |
| 1120 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1121 | if (!(*f_iter)->is_oneway()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1122 | std::string resultname = decapitalize((*f_iter)->get_name() + "_result"); |
| 1123 | t_struct noargs(program_); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1124 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1125 | t_function recv_function((*f_iter)->get_returntype(), |
| 1126 | string("recv_") + (*f_iter)->get_name(), |
| 1127 | &noargs); |
| 1128 | // Open function |
| 1129 | f_service_ << |
| 1130 | indent() << "method private " << function_signature(&recv_function) << " =" << endl; |
| 1131 | indent_up(); |
| 1132 | |
| 1133 | // TODO(mcslee): Validate message reply here, seq ids etc. |
| 1134 | |
| 1135 | f_service_ << |
| 1136 | indent() << "let (fname, mtype, rseqid) = iprot#readMessageBegin in" << endl; |
| 1137 | indent_up(); |
| 1138 | f_service_ << |
| 1139 | indent() << "(if mtype = Protocol.EXCEPTION then" << endl << |
| 1140 | indent() << " let x = Application_Exn.read iprot in" << endl; |
| 1141 | indent_up(); |
| 1142 | f_service_ << |
iproctor | 8361bf0 | 2008-04-10 00:31:55 +0000 | [diff] [blame] | 1143 | indent() << " (iprot#readMessageEnd;" << |
| 1144 | indent() << " raise (Application_Exn.E x))" << endl; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1145 | indent_down(); |
| 1146 | f_service_ << |
| 1147 | indent() << "else ());" << endl; |
| 1148 | string res = "_"; |
| 1149 | |
| 1150 | t_struct* xs = (*f_iter)->get_xceptions(); |
| 1151 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 1152 | |
| 1153 | if (!(*f_iter)->get_returntype()->is_void() || xceptions.size() > 0) { |
| 1154 | res = "result"; |
| 1155 | } |
| 1156 | f_service_ << |
| 1157 | indent() << "let "<<res<<" = read_" << resultname << " iprot in" << endl; |
| 1158 | indent_up(); |
| 1159 | f_service_ << |
| 1160 | indent() << "iprot#readMessageEnd;" << endl; |
| 1161 | |
| 1162 | // Careful, only return _result if not a void function |
| 1163 | if (!(*f_iter)->get_returntype()->is_void()) { |
| 1164 | f_service_ << |
| 1165 | indent() << "match result#get_success with Some v -> v | None -> (" << endl; |
| 1166 | indent_up(); |
| 1167 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1168 | |
| 1169 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1170 | vector<t_field*>::const_iterator x_iter; |
| 1171 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 1172 | f_service_ << |
| 1173 | indent() << "(match result#get_" << (*x_iter)->get_name() << " with None -> () | Some _v ->" << endl; |
| 1174 | indent(f_service_) << " raise (" << capitalize(type_name((*x_iter)->get_type())) << " _v));" << endl; |
| 1175 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1176 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1177 | // Careful, only return _result if not a void function |
| 1178 | if ((*f_iter)->get_returntype()->is_void()) { |
| 1179 | indent(f_service_) << |
| 1180 | "()" << endl; |
| 1181 | } else { |
| 1182 | f_service_ << |
| 1183 | indent() << "raise (Application_Exn.E (Application_Exn.create Application_Exn.MISSING_RESULT \"" << (*f_iter)->get_name() << " failed: unknown result\")))" << endl; |
| 1184 | indent_down(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1185 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1186 | |
| 1187 | // Close function |
| 1188 | indent_down(); |
| 1189 | indent_down(); |
| 1190 | indent_down(); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | indent_down(); |
| 1195 | indent(f_service_) << "end" << endl << endl; |
| 1196 | indent(f_service_i_) << "end" << endl << endl; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * Generates a service server definition. |
| 1201 | * |
| 1202 | * @param tservice The service to generate a server for. |
| 1203 | */ |
| 1204 | void t_ocaml_generator::generate_service_server(t_service* tservice) { |
| 1205 | // Generate the dispatch methods |
| 1206 | vector<t_function*> functions = tservice->get_functions(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1207 | vector<t_function*>::iterator f_iter; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1208 | |
| 1209 | |
| 1210 | // Generate the header portion |
| 1211 | indent(f_service_) << |
| 1212 | "class processor (handler : iface) =" << endl << indent() << "object (self)" << endl; |
| 1213 | indent(f_service_i_) << |
| 1214 | "class processor : iface ->" << endl << indent() << "object" << endl; |
| 1215 | indent_up(); |
| 1216 | |
| 1217 | f_service_ << |
| 1218 | indent() << "inherit Processor.t" << endl << |
| 1219 | endl; |
| 1220 | f_service_i_ << |
| 1221 | indent() << "inherit Processor.t" << endl << |
| 1222 | endl; |
| 1223 | string extends = ""; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1224 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1225 | if (tservice->get_extends() != NULL) { |
| 1226 | extends = type_name(tservice->get_extends()); |
| 1227 | indent(f_service_) << "inherit " + extends + ".processor (handler :> " + extends + ".iface)" << endl; |
| 1228 | indent(f_service_i_) << "inherit " + extends + ".processor" << endl; |
| 1229 | } |
| 1230 | |
| 1231 | if (extends.empty()) { |
| 1232 | indent(f_service_) << "val processMap = Hashtbl.create " << functions.size() << endl; |
| 1233 | } |
| 1234 | indent(f_service_i_) << "val processMap : (string, int * Protocol.t * Protocol.t -> unit) Hashtbl.t" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1235 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1236 | // Generate the server implementation |
| 1237 | indent(f_service_) << |
| 1238 | "method process iprot oprot =" << endl; |
| 1239 | indent(f_service_i_) << |
| 1240 | "method process : Protocol.t -> Protocol.t -> bool" << endl; |
| 1241 | indent_up(); |
| 1242 | |
| 1243 | f_service_ << |
| 1244 | indent() << "let (name, typ, seqid) = iprot#readMessageBegin in" << endl; |
| 1245 | indent_up(); |
| 1246 | // TODO(mcslee): validate message |
| 1247 | |
| 1248 | // HOT: dictionary function lookup |
| 1249 | f_service_ << |
| 1250 | indent() << "if Hashtbl.mem processMap name then" << endl << |
| 1251 | indent() << " (Hashtbl.find processMap name) (seqid, iprot, oprot)" << endl << |
| 1252 | indent() << "else (" << endl << |
| 1253 | indent() << " iprot#skip(Protocol.T_STRUCT);" << endl << |
| 1254 | indent() << " iprot#readMessageEnd;" << endl << |
| 1255 | indent() << " let x = Application_Exn.create Application_Exn.UNKNOWN_METHOD (\"Unknown function \"^name) in" << endl << |
| 1256 | indent() << " oprot#writeMessageBegin(name, Protocol.EXCEPTION, seqid);" << endl << |
| 1257 | indent() << " x#write oprot;" << endl << |
| 1258 | indent() << " oprot#writeMessageEnd;" << endl << |
| 1259 | indent() << " oprot#getTransport#flush" << endl << |
| 1260 | indent() << ");" << endl; |
| 1261 | |
| 1262 | // Read end of args field, the T_STOP, and the struct close |
| 1263 | f_service_ << |
| 1264 | indent() << "true" << endl; |
| 1265 | indent_down(); |
| 1266 | indent_down(); |
| 1267 | // Generate the process subfunctions |
| 1268 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 1269 | generate_process_function(tservice, *f_iter); |
| 1270 | } |
| 1271 | |
| 1272 | indent(f_service_) << "initializer" << endl; |
| 1273 | indent_up(); |
| 1274 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 1275 | f_service_ << |
| 1276 | indent() << "Hashtbl.add processMap \"" << (*f_iter)->get_name() << "\" self#process_" << (*f_iter)->get_name() << ";" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1277 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1278 | indent_down(); |
| 1279 | |
| 1280 | indent_down(); |
| 1281 | indent(f_service_) << "end" << endl << endl; |
| 1282 | indent(f_service_i_) << "end" << endl << endl; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * Generates a process function definition. |
| 1287 | * |
| 1288 | * @param tfunction The function to write a dispatcher for |
| 1289 | */ |
| 1290 | void t_ocaml_generator::generate_process_function(t_service* tservice, |
| 1291 | t_function* tfunction) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 1292 | (void) tservice; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1293 | // Open function |
| 1294 | indent(f_service_) << |
| 1295 | "method private process_" << tfunction->get_name() << |
| 1296 | " (seqid, iprot, oprot) =" << endl; |
| 1297 | indent_up(); |
| 1298 | |
| 1299 | string argsname = decapitalize(tfunction->get_name()) + "_args"; |
| 1300 | string resultname = decapitalize(tfunction->get_name()) + "_result"; |
| 1301 | |
| 1302 | // Generate the function call |
| 1303 | t_struct* arg_struct = tfunction->get_arglist(); |
| 1304 | const std::vector<t_field*>& fields = arg_struct->get_members(); |
| 1305 | vector<t_field*>::const_iterator f_iter; |
| 1306 | |
| 1307 | string args = "args"; |
| 1308 | if(fields.size() == 0){ |
| 1309 | args="_"; |
| 1310 | } |
| 1311 | |
| 1312 | f_service_ << |
| 1313 | indent() << "let "<<args<<" = read_" << argsname << " iprot in" << endl; |
| 1314 | indent_up(); |
| 1315 | f_service_ << |
| 1316 | indent() << "iprot#readMessageEnd;" << endl; |
| 1317 | |
| 1318 | t_struct* xs = tfunction->get_xceptions(); |
| 1319 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 1320 | vector<t_field*>::const_iterator x_iter; |
| 1321 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 1322 | // Declare result for non oneway function |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1323 | if (!tfunction->is_oneway()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1324 | f_service_ << |
| 1325 | indent() << "let result = new " << resultname << " in" << endl; |
| 1326 | indent_up(); |
| 1327 | } |
| 1328 | |
| 1329 | // Try block for a function with exceptions |
| 1330 | if (xceptions.size() > 0) { |
| 1331 | f_service_ << |
| 1332 | indent() << "(try" << endl; |
| 1333 | indent_up(); |
| 1334 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1335 | |
| 1336 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1337 | |
| 1338 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1339 | f_service_ << indent(); |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1340 | if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1341 | f_service_ << "result#set_success "; |
| 1342 | } |
| 1343 | f_service_ << |
| 1344 | "(handler#" << tfunction->get_name(); |
| 1345 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1346 | f_service_ << " args#get_" << (*f_iter)->get_name(); |
| 1347 | } |
| 1348 | f_service_ << ");" << endl; |
| 1349 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1350 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1351 | if (xceptions.size() > 0) { |
| 1352 | indent_down(); |
| 1353 | indent(f_service_) << "with" <<endl; |
| 1354 | indent_up(); |
| 1355 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 1356 | f_service_ << |
| 1357 | indent() << "| " << capitalize(type_name((*x_iter)->get_type())) << " " << (*x_iter)->get_name() << " -> " << endl; |
| 1358 | indent_up(); |
| 1359 | indent_up(); |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1360 | if(!tfunction->is_oneway()){ |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1361 | f_service_ << |
| 1362 | indent() << "result#set_" << (*x_iter)->get_name() << " " << (*x_iter)->get_name() << endl; |
| 1363 | } else { |
| 1364 | indent(f_service_) << "()"; |
| 1365 | } |
| 1366 | indent_down(); |
| 1367 | indent_down(); |
| 1368 | } |
| 1369 | indent_down(); |
| 1370 | f_service_ << indent() << ");" << endl; |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 1375 | // Shortcut out here for oneway functions |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 1376 | if (tfunction->is_oneway()) { |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1377 | f_service_ << |
| 1378 | indent() << "()" << endl; |
| 1379 | indent_down(); |
| 1380 | indent_down(); |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | f_service_ << |
| 1385 | indent() << "oprot#writeMessageBegin (\"" << tfunction->get_name() << "\", Protocol.REPLY, seqid);" << endl << |
| 1386 | indent() << "result#write oprot;" << endl << |
| 1387 | indent() << "oprot#writeMessageEnd;" << endl << |
| 1388 | indent() << "oprot#getTransport#flush" << endl; |
| 1389 | |
| 1390 | // Close function |
| 1391 | indent_down(); |
| 1392 | indent_down(); |
| 1393 | indent_down(); |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * Deserializes a field of any type. |
| 1398 | */ |
| 1399 | void t_ocaml_generator::generate_deserialize_field(ofstream &out, |
| 1400 | t_field* tfield, |
| 1401 | string prefix){ |
| 1402 | t_type* type = tfield->get_type(); |
| 1403 | |
| 1404 | |
| 1405 | string name = decapitalize(tfield->get_name()); |
| 1406 | indent(out) << prefix << "#set_"<<name << " "; |
| 1407 | generate_deserialize_type(out,type); |
| 1408 | out << endl; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | /** |
| 1413 | * Deserializes a field of any type. |
| 1414 | */ |
| 1415 | void t_ocaml_generator::generate_deserialize_type(ofstream &out, |
| 1416 | t_type* type){ |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 1417 | type = get_true_type(type); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (type->is_void()) { |
| 1420 | throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE"; |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | if (type->is_struct() || type->is_xception()) { |
| 1425 | generate_deserialize_struct(out, |
| 1426 | (t_struct*)type); |
| 1427 | } else if (type->is_container()) { |
| 1428 | generate_deserialize_container(out, type); |
| 1429 | } else if (type->is_base_type()) { |
| 1430 | out << "iprot#"; |
| 1431 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1432 | switch (tbase) { |
| 1433 | case t_base_type::TYPE_VOID: |
| 1434 | throw "compiler error: cannot serialize void field in a struct"; |
| 1435 | break; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1436 | case t_base_type::TYPE_STRING: |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1437 | out << "readString"; |
| 1438 | break; |
| 1439 | case t_base_type::TYPE_BOOL: |
| 1440 | out << "readBool"; |
| 1441 | break; |
| 1442 | case t_base_type::TYPE_BYTE: |
| 1443 | out << "readByte"; |
| 1444 | break; |
| 1445 | case t_base_type::TYPE_I16: |
| 1446 | out << "readI16"; |
| 1447 | break; |
| 1448 | case t_base_type::TYPE_I32: |
| 1449 | out << "readI32"; |
| 1450 | break; |
| 1451 | case t_base_type::TYPE_I64: |
| 1452 | out << "readI64"; |
| 1453 | break; |
| 1454 | case t_base_type::TYPE_DOUBLE: |
| 1455 | out << "readDouble"; |
| 1456 | break; |
| 1457 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 1458 | throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } else if (type->is_enum()) { |
| 1461 | string ename = capitalize(type->get_name()); |
| 1462 | out << "(" <<ename << ".of_i iprot#readI32)"; |
| 1463 | } else { |
| 1464 | printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n", |
| 1465 | type->get_name().c_str()); |
| 1466 | } |
| 1467 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1468 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1469 | |
| 1470 | /** |
| 1471 | * Generates an unserializer for a struct, calling read() |
| 1472 | */ |
| 1473 | void t_ocaml_generator::generate_deserialize_struct(ofstream &out, |
| 1474 | t_struct* tstruct) { |
Bryan Duxbury | f51a4ce | 2011-03-23 17:57:21 +0000 | [diff] [blame] | 1475 | string prefix = ""; |
| 1476 | t_program* program = tstruct->get_program(); |
| 1477 | if (program != NULL && program != program_) { |
| 1478 | prefix = capitalize(program->get_name()) + "_types."; |
| 1479 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1480 | string name = decapitalize(tstruct->get_name()); |
Bryan Duxbury | f51a4ce | 2011-03-23 17:57:21 +0000 | [diff] [blame] | 1481 | out << "(" << prefix << "read_" << name << " iprot)"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1482 | |
| 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * Serialize a container by writing out the header followed by |
| 1487 | * data and then a footer. |
| 1488 | */ |
| 1489 | void t_ocaml_generator::generate_deserialize_container(ofstream &out, |
| 1490 | t_type* ttype) { |
| 1491 | string size = tmp("_size"); |
| 1492 | string ktype = tmp("_ktype"); |
| 1493 | string vtype = tmp("_vtype"); |
| 1494 | string etype = tmp("_etype"); |
| 1495 | string con = tmp("_con"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1496 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1497 | t_field fsize(g_type_i32, size); |
| 1498 | t_field fktype(g_type_byte, ktype); |
| 1499 | t_field fvtype(g_type_byte, vtype); |
| 1500 | t_field fetype(g_type_byte, etype); |
| 1501 | |
| 1502 | out << endl; |
| 1503 | indent_up(); |
| 1504 | // Declare variables, read header |
| 1505 | if (ttype->is_map()) { |
| 1506 | indent(out) << "(let ("<<ktype<<","<<vtype<<","<<size<<") = iprot#readMapBegin in" << endl; |
| 1507 | indent(out) << "let "<<con<<" = Hashtbl.create "<<size<<" in" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1508 | indent_up(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1509 | indent(out) << "for i = 1 to "<<size<<" do" <<endl; |
| 1510 | indent_up(); |
| 1511 | indent(out) << "let _k = "; |
| 1512 | generate_deserialize_type(out,((t_map*)ttype)->get_key_type()); |
| 1513 | out << " in" << endl; |
| 1514 | indent(out) << "let _v = "; |
| 1515 | generate_deserialize_type(out,((t_map*)ttype)->get_val_type()); |
| 1516 | out << " in" << endl; |
| 1517 | indent_up(); |
| 1518 | indent(out) << "Hashtbl.add "<<con<< " _k _v" << endl; |
| 1519 | indent_down(); |
| 1520 | indent_down(); |
| 1521 | indent(out) << "done; iprot#readMapEnd; "<<con<<")"; |
| 1522 | indent_down(); |
| 1523 | } else if (ttype->is_set()) { |
| 1524 | indent(out) << "(let ("<<etype<<","<<size<<") = iprot#readSetBegin in" << endl; |
| 1525 | indent(out) << "let "<<con<<" = Hashtbl.create "<<size<<" in" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1526 | indent_up(); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1527 | indent(out) << "for i = 1 to "<<size<<" do" <<endl; |
| 1528 | indent_up(); |
| 1529 | indent(out) << "Hashtbl.add "<<con<<" "; |
| 1530 | generate_deserialize_type(out,((t_set*)ttype)->get_elem_type()); |
| 1531 | out << " true" << endl; |
| 1532 | indent_down(); |
| 1533 | indent(out) << "done; iprot#readSetEnd; "<<con<<")"; |
| 1534 | indent_down(); |
| 1535 | } else if (ttype->is_list()) { |
| 1536 | indent(out) << "(let ("<<etype<<","<<size<<") = iprot#readListBegin in" << endl; |
| 1537 | indent_up(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1538 | indent(out) << "let "<<con<<" = (Array.to_list (Array.init "<<size<<" (fun _ -> "; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1539 | generate_deserialize_type(out,((t_list*)ttype)->get_elem_type()); |
| 1540 | out << "))) in" << endl; |
| 1541 | indent_up(); |
| 1542 | indent(out) << "iprot#readListEnd; "<<con<<")"; |
| 1543 | indent_down(); |
| 1544 | indent_down(); |
| 1545 | } |
| 1546 | indent_down(); |
| 1547 | } |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | /** |
| 1552 | * Serializes a field of any type. |
| 1553 | * |
| 1554 | * @param tfield The field to serialize |
| 1555 | * @param prefix Name to prepend to field name |
| 1556 | */ |
| 1557 | void t_ocaml_generator::generate_serialize_field(ofstream &out, |
| 1558 | t_field* tfield, |
| 1559 | string name) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 1560 | t_type* type = get_true_type(tfield->get_type()); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1561 | |
| 1562 | // Do nothing for void types |
| 1563 | if (type->is_void()) { |
| 1564 | throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + |
| 1565 | tfield->get_name(); |
| 1566 | } |
| 1567 | |
| 1568 | if(name.length() == 0){ |
| 1569 | name = decapitalize(tfield->get_name()); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1570 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (type->is_struct() || type->is_xception()) { |
| 1573 | generate_serialize_struct(out, |
| 1574 | (t_struct*)type, |
| 1575 | name); |
| 1576 | } else if (type->is_container()) { |
| 1577 | generate_serialize_container(out, |
| 1578 | type, |
| 1579 | name); |
| 1580 | } else if (type->is_base_type() || type->is_enum()) { |
| 1581 | |
| 1582 | |
| 1583 | indent(out) << |
| 1584 | "oprot#"; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1585 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1586 | if (type->is_base_type()) { |
| 1587 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1588 | switch (tbase) { |
| 1589 | case t_base_type::TYPE_VOID: |
| 1590 | throw |
| 1591 | "compiler error: cannot serialize void field in a struct: " + name; |
| 1592 | break; |
| 1593 | case t_base_type::TYPE_STRING: |
| 1594 | out << "writeString(" << name << ")"; |
| 1595 | break; |
| 1596 | case t_base_type::TYPE_BOOL: |
| 1597 | out << "writeBool(" << name << ")"; |
| 1598 | break; |
| 1599 | case t_base_type::TYPE_BYTE: |
| 1600 | out << "writeByte(" << name << ")"; |
| 1601 | break; |
| 1602 | case t_base_type::TYPE_I16: |
| 1603 | out << "writeI16(" << name << ")"; |
| 1604 | break; |
| 1605 | case t_base_type::TYPE_I32: |
| 1606 | out << "writeI32(" << name << ")"; |
| 1607 | break; |
| 1608 | case t_base_type::TYPE_I64: |
| 1609 | out << "writeI64(" << name << ")"; |
| 1610 | break; |
| 1611 | case t_base_type::TYPE_DOUBLE: |
| 1612 | out << "writeDouble(" << name << ")"; |
| 1613 | break; |
| 1614 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 1615 | throw "compiler error: no ocaml name for base type " + t_base_type::t_base_name(tbase); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } else if (type->is_enum()) { |
| 1618 | string ename = capitalize(type->get_name()); |
| 1619 | out << "writeI32("<<ename<<".to_i " << name << ")"; |
| 1620 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1621 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1622 | } else { |
| 1623 | printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n", |
| 1624 | tfield->get_name().c_str(), |
| 1625 | type->get_name().c_str()); |
| 1626 | } |
| 1627 | out << ";" << endl; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * Serializes all the members of a struct. |
| 1632 | * |
| 1633 | * @param tstruct The struct to serialize |
| 1634 | * @param prefix String prefix to attach to all fields |
| 1635 | */ |
| 1636 | void t_ocaml_generator::generate_serialize_struct(ofstream &out, |
| 1637 | t_struct* tstruct, |
| 1638 | string prefix) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 1639 | (void) tstruct; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1640 | indent(out) << prefix << "#write(oprot)"; |
| 1641 | } |
| 1642 | |
| 1643 | void t_ocaml_generator::generate_serialize_container(ofstream &out, |
| 1644 | t_type* ttype, |
| 1645 | string prefix) { |
| 1646 | if (ttype->is_map()) { |
| 1647 | indent(out) << "oprot#writeMapBegin("<< type_to_enum(((t_map*)ttype)->get_key_type()) << ","; |
| 1648 | out << type_to_enum(((t_map*)ttype)->get_val_type()) << ","; |
| 1649 | out << "Hashtbl.length " << prefix << ");" << endl; |
| 1650 | } else if (ttype->is_set()) { |
| 1651 | indent(out) << |
| 1652 | "oprot#writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ","; |
| 1653 | out << "Hashtbl.length " << prefix << ");" << endl; |
| 1654 | } else if (ttype->is_list()) { |
| 1655 | indent(out) << |
| 1656 | "oprot#writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type()) << ","; |
| 1657 | out << "List.length " << prefix << ");" << endl; |
| 1658 | } |
| 1659 | |
| 1660 | if (ttype->is_map()) { |
| 1661 | string kiter = tmp("_kiter"); |
| 1662 | string viter = tmp("_viter"); |
| 1663 | indent(out) << "Hashtbl.iter (fun "<<kiter<<" -> fun " << viter << " -> " << endl; |
| 1664 | indent_up(); |
| 1665 | generate_serialize_map_element(out, (t_map*)ttype, kiter, viter); |
| 1666 | indent_down(); |
| 1667 | indent(out) << ") " << prefix << ";" << endl; |
| 1668 | } else if (ttype->is_set()) { |
| 1669 | string iter = tmp("_iter"); |
| 1670 | indent(out) << "Hashtbl.iter (fun "<<iter<<" -> fun _ -> "; |
| 1671 | indent_up(); |
| 1672 | generate_serialize_set_element(out, (t_set*)ttype, iter); |
| 1673 | indent_down(); |
| 1674 | indent(out) << ") " << prefix << ";" << endl; |
| 1675 | } else if (ttype->is_list()) { |
| 1676 | string iter = tmp("_iter"); |
| 1677 | indent(out) << "List.iter (fun "<<iter<<" -> "; |
| 1678 | indent_up(); |
| 1679 | generate_serialize_list_element(out, (t_list*)ttype, iter); |
| 1680 | indent_down(); |
| 1681 | indent(out) << ") " << prefix << ";" << endl; |
| 1682 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1683 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1684 | if (ttype->is_map()) { |
| 1685 | indent(out) << |
| 1686 | "oprot#writeMapEnd"; |
| 1687 | } else if (ttype->is_set()) { |
| 1688 | indent(out) << |
| 1689 | "oprot#writeSetEnd"; |
| 1690 | } else if (ttype->is_list()) { |
| 1691 | indent(out) << |
| 1692 | "oprot#writeListEnd"; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | /** |
| 1697 | * Serializes the members of a map. |
| 1698 | * |
| 1699 | */ |
| 1700 | void t_ocaml_generator::generate_serialize_map_element(ofstream &out, |
| 1701 | t_map* tmap, |
| 1702 | string kiter, |
| 1703 | string viter) { |
| 1704 | t_field kfield(tmap->get_key_type(), kiter); |
| 1705 | generate_serialize_field(out, &kfield); |
| 1706 | |
| 1707 | t_field vfield(tmap->get_val_type(), viter); |
| 1708 | generate_serialize_field(out, &vfield); |
| 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * Serializes the members of a set. |
| 1713 | */ |
| 1714 | void t_ocaml_generator::generate_serialize_set_element(ofstream &out, |
| 1715 | t_set* tset, |
| 1716 | string iter) { |
| 1717 | t_field efield(tset->get_elem_type(), iter); |
| 1718 | generate_serialize_field(out, &efield); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * Serializes the members of a list. |
| 1723 | */ |
| 1724 | void t_ocaml_generator::generate_serialize_list_element(ofstream &out, |
| 1725 | t_list* tlist, |
| 1726 | string iter) { |
| 1727 | t_field efield(tlist->get_elem_type(), iter); |
| 1728 | generate_serialize_field(out, &efield); |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | /** |
| 1734 | * Renders a function signature of the form 'name args' |
| 1735 | * |
| 1736 | * @param tfunction Function definition |
| 1737 | * @return String of rendered function definition |
| 1738 | */ |
| 1739 | string t_ocaml_generator::function_signature(t_function* tfunction, |
| 1740 | string prefix) { |
| 1741 | return |
| 1742 | prefix + decapitalize(tfunction->get_name()) + |
| 1743 | " " + argument_list(tfunction->get_arglist()); |
| 1744 | } |
| 1745 | |
| 1746 | string t_ocaml_generator::function_type(t_function* tfunc, bool method, bool options){ |
| 1747 | string result=""; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1748 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1749 | const vector<t_field*>& fields = tfunc->get_arglist()->get_members(); |
| 1750 | vector<t_field*>::const_iterator f_iter; |
| 1751 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1752 | result += render_ocaml_type((*f_iter)->get_type()); |
| 1753 | if(options) |
| 1754 | result += " option"; |
| 1755 | result += " -> "; |
| 1756 | } |
| 1757 | if(fields.empty() && !method){ |
| 1758 | result += "unit -> "; |
| 1759 | } |
| 1760 | result += render_ocaml_type(tfunc->get_returntype()); |
| 1761 | return result; |
| 1762 | } |
| 1763 | |
| 1764 | /** |
| 1765 | * Renders a field list |
| 1766 | */ |
| 1767 | string t_ocaml_generator::argument_list(t_struct* tstruct) { |
| 1768 | string result = ""; |
| 1769 | |
| 1770 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1771 | vector<t_field*>::const_iterator f_iter; |
| 1772 | bool first = true; |
| 1773 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1774 | if (first) { |
| 1775 | first = false; |
| 1776 | } else { |
| 1777 | result += " "; |
| 1778 | } |
| 1779 | result += (*f_iter)->get_name(); |
| 1780 | } |
| 1781 | return result; |
| 1782 | } |
| 1783 | |
| 1784 | string t_ocaml_generator::type_name(t_type* ttype) { |
| 1785 | string prefix = ""; |
| 1786 | t_program* program = ttype->get_program(); |
| 1787 | if (program != NULL && program != program_) { |
| 1788 | if (!ttype->is_service()) { |
| 1789 | prefix = capitalize(program->get_name()) + "_types."; |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | string name = ttype->get_name(); |
| 1794 | if(ttype->is_service()){ |
| 1795 | name = capitalize(name); |
| 1796 | } else { |
| 1797 | name = decapitalize(name); |
| 1798 | } |
| 1799 | return prefix + name; |
| 1800 | } |
| 1801 | |
| 1802 | /** |
| 1803 | * Converts the parse type to a Protocol.t_type enum |
| 1804 | */ |
| 1805 | string t_ocaml_generator::type_to_enum(t_type* type) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 1806 | type = get_true_type(type); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1807 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1808 | if (type->is_base_type()) { |
| 1809 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1810 | switch (tbase) { |
| 1811 | case t_base_type::TYPE_VOID: |
| 1812 | return "Protocol.T_VOID"; |
| 1813 | case t_base_type::TYPE_STRING: |
| 1814 | return "Protocol.T_STRING"; |
| 1815 | case t_base_type::TYPE_BOOL: |
| 1816 | return "Protocol.T_BOOL"; |
| 1817 | case t_base_type::TYPE_BYTE: |
| 1818 | return "Protocol.T_BYTE"; |
| 1819 | case t_base_type::TYPE_I16: |
| 1820 | return "Protocol.T_I16"; |
| 1821 | case t_base_type::TYPE_I32: |
| 1822 | return "Protocol.T_I32"; |
| 1823 | case t_base_type::TYPE_I64: |
| 1824 | return "Protocol.T_I64"; |
| 1825 | case t_base_type::TYPE_DOUBLE: |
| 1826 | return "Protocol.T_DOUBLE"; |
| 1827 | } |
| 1828 | } else if (type->is_enum()) { |
| 1829 | return "Protocol.T_I32"; |
| 1830 | } else if (type->is_struct() || type->is_xception()) { |
| 1831 | return "Protocol.T_STRUCT"; |
| 1832 | } else if (type->is_map()) { |
| 1833 | return "Protocol.T_MAP"; |
| 1834 | } else if (type->is_set()) { |
| 1835 | return "Protocol.T_SET"; |
| 1836 | } else if (type->is_list()) { |
| 1837 | return "Protocol.T_LIST"; |
| 1838 | } |
| 1839 | |
| 1840 | throw "INVALID TYPE IN type_to_enum: " + type->get_name(); |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * Converts the parse type to an ocaml type |
| 1845 | */ |
| 1846 | string t_ocaml_generator::render_ocaml_type(t_type* type) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 1847 | type = get_true_type(type); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1848 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1849 | if (type->is_base_type()) { |
| 1850 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1851 | switch (tbase) { |
| 1852 | case t_base_type::TYPE_VOID: |
| 1853 | return "unit"; |
| 1854 | case t_base_type::TYPE_STRING: |
| 1855 | return "string"; |
| 1856 | case t_base_type::TYPE_BOOL: |
| 1857 | return "bool"; |
| 1858 | case t_base_type::TYPE_BYTE: |
| 1859 | return "int"; |
| 1860 | case t_base_type::TYPE_I16: |
| 1861 | return "int"; |
| 1862 | case t_base_type::TYPE_I32: |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 1863 | return "Int32.t"; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1864 | case t_base_type::TYPE_I64: |
| 1865 | return "Int64.t"; |
| 1866 | case t_base_type::TYPE_DOUBLE: |
| 1867 | return "float"; |
| 1868 | } |
| 1869 | } else if (type->is_enum()) { |
| 1870 | return capitalize(((t_enum*)type)->get_name())+".t"; |
| 1871 | } else if (type->is_struct() || type->is_xception()) { |
| 1872 | return type_name((t_struct*)type); |
| 1873 | } else if (type->is_map()) { |
| 1874 | t_type* ktype = ((t_map*)type)->get_key_type(); |
| 1875 | t_type* vtype = ((t_map*)type)->get_val_type(); |
| 1876 | return "("+render_ocaml_type(ktype)+","+render_ocaml_type(vtype)+") Hashtbl.t"; |
| 1877 | } else if (type->is_set()) { |
| 1878 | t_type* etype = ((t_set*)type)->get_elem_type(); |
| 1879 | return "("+render_ocaml_type(etype)+",bool) Hashtbl.t"; |
| 1880 | } else if (type->is_list()) { |
| 1881 | t_type* etype = ((t_list*)type)->get_elem_type(); |
| 1882 | return render_ocaml_type(etype)+" list"; |
| 1883 | } |
| 1884 | |
| 1885 | throw "INVALID TYPE IN type_to_enum: " + type->get_name(); |
| 1886 | } |
David Reiss | f0521b1 | 2008-03-27 21:40:05 +0000 | [diff] [blame] | 1887 | |
| 1888 | |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 1889 | THRIFT_REGISTER_GENERATOR(ocaml, "OCaml", "") |
| 1890 | |