/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <string>
#include <fstream>
#include <iostream>
#include <vector>

#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sstream>
#include "thrift/platform.h"
#include "thrift/version.h"
#include "thrift/generate/t_oop_generator.h"

using std::ios;
using std::map;
using std::ostream;
using std::ostringstream;
using std::string;
using std::stringstream;
using std::vector;

/**
 * OCaml code generator.
 *
 */
class t_ocaml_generator : public t_oop_generator {
public:
  t_ocaml_generator(t_program* program,
                    const std::map<std::string, std::string>& parsed_options,
                    const std::string& option_string)
    : t_oop_generator(program) {
    (void)option_string;
    std::map<std::string, std::string>::const_iterator iter;

    /* no options yet */
    for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
      throw "unknown option ocaml:" + iter->first;
    }

    out_dir_base_ = "gen-ocaml";
  }

  ~t_ocaml_generator() override;

  void init_generator() override;
  std::string display_name() const override;

  /**
   * Program-level generation functions
   */

  void generate_program() override;
  void generate_typedef(t_typedef* ttypedef) override;
  void generate_enum(t_enum* tenum) override;
  void generate_const(t_const* tconst) override;
  void generate_struct(t_struct* tstruct) override;
  void generate_xception(t_struct* txception) override;
  void generate_service(t_service* tservice) override;

  std::string render_const_value(t_type* type, t_const_value* value);
  bool struct_member_persistent(t_field* tmember);
  bool struct_member_omitable(t_field* tmember);
  bool struct_member_default_cheaply_comparable(t_field* tmember);
  std::string struct_member_copy_of(t_type* type, string what);

  /**
   * Struct generation code
   */

  void generate_ocaml_struct(t_struct* tstruct, bool is_exception);
  void generate_ocaml_struct_definition(std::ostream& out,
                                        t_struct* tstruct,
                                        bool is_xception = false);
  void generate_ocaml_struct_member(std::ostream& out, string tname, t_field* tmember);
  void generate_ocaml_struct_sig(std::ostream& out, t_struct* tstruct, bool is_exception);
  void generate_ocaml_struct_reader(std::ostream& out, t_struct* tstruct);
  void generate_ocaml_struct_writer(std::ostream& out, t_struct* tstruct);
  void generate_ocaml_function_helpers(t_function* tfunction);
  void generate_ocaml_method_copy(std::ostream& out, const vector<t_field*>& members);
  void generate_ocaml_member_copy(std::ostream& out, t_field* member);

  /**
   * Service-level generation functions
   */

  void generate_service_helpers(t_service* tservice);
  void generate_service_interface(t_service* tservice);
  void generate_service_client(t_service* tservice);
  void generate_service_server(t_service* tservice);
  void generate_process_function(t_service* tservice, t_function* tfunction);

  /**
   * Serialization constructs
   */

  void generate_deserialize_field(std::ostream& out, t_field* tfield, std::string prefix);

  void generate_deserialize_struct(std::ostream& out, t_struct* tstruct);

  void generate_deserialize_container(std::ostream& out, t_type* ttype);

  void generate_deserialize_set_element(std::ostream& out, t_set* tset);

  void generate_deserialize_list_element(std::ostream& out,
                                         t_list* tlist,
                                         std::string prefix = "");
  void generate_deserialize_type(std::ostream& out, t_type* type);

  void generate_serialize_field(std::ostream& out, t_field* tfield, std::string name = "");

  void generate_serialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = "");

  void generate_serialize_container(std::ostream& out, t_type* ttype, std::string prefix = "");

  void generate_serialize_map_element(std::ostream& out,
                                      t_map* tmap,
                                      std::string kiter,
                                      std::string viter);

  void generate_serialize_set_element(std::ostream& out, t_set* tmap, std::string iter);

  void generate_serialize_list_element(std::ostream& out, t_list* tlist, std::string iter);

  /**
   * Helper rendering functions
   */

  /** Need to disable codegen comment for unit tests to be version-agnostic */
  virtual std::string ocaml_autogen_comment();

  std::string ocaml_imports();
  std::string type_name(t_type* ttype);
  std::string exception_ctor(t_type* ttype);
  std::string function_signature(t_function* tfunction, std::string prefix = "");
  std::string function_type(t_function* tfunc, bool method = false, bool options = false);
  std::string argument_list(t_struct* tstruct);
  std::string type_to_enum(t_type* ttype);
  std::string render_ocaml_type(t_type* type);

// Need access to output file streams for testing.
protected:
  /**
   * File streams
   */

  ofstream_with_content_based_conditional_update f_types_;
  ofstream_with_content_based_conditional_update f_consts_;
  ofstream_with_content_based_conditional_update f_service_;

  ofstream_with_content_based_conditional_update f_types_i_;
  ofstream_with_content_based_conditional_update f_service_i_;
};

/*
 * This is necessary because we want typedefs to appear later,
 * after all the types have been declared.
 */
void t_ocaml_generator::generate_program() {
  // Initialize the generator
  init_generator();

  // Generate enums
  vector<t_enum*> enums = program_->get_enums();
  vector<t_enum*>::iterator en_iter;
  for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
    generate_enum(*en_iter);
  }

  // Generate structs
  vector<t_struct*> structs = program_->get_structs();
  vector<t_struct*>::iterator st_iter;
  for (st_iter = structs.begin(); st_iter != structs.end(); ++st_iter) {
    generate_struct(*st_iter);
  }

  // Generate xceptions
  vector<t_struct*> xceptions = program_->get_xceptions();
  vector<t_struct*>::iterator x_iter;
  for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
    generate_xception(*x_iter);
  }

  // Generate typedefs
  vector<t_typedef*> typedefs = program_->get_typedefs();
  vector<t_typedef*>::iterator td_iter;
  for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
    generate_typedef(*td_iter);
  }

  // Generate services
  vector<t_service*> services = program_->get_services();
  vector<t_service*>::iterator sv_iter;
  for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
    service_name_ = get_service_name(*sv_iter);
    generate_service(*sv_iter);
  }

  // Generate constants
  vector<t_const*> consts = program_->get_consts();
  generate_consts(consts);
}

/**
 * Prepares for file generation by opening up the necessary file output
 * streams.
 *
 * @param tprogram The program to generate
 */
void t_ocaml_generator::init_generator() {
  // Make output directory
  MKDIR(get_out_dir().c_str());

  // Make output file
  string f_types_name = get_out_dir() + program_name_ + "_types.ml";
  f_types_.open(f_types_name.c_str());
  string f_types_i_name = get_out_dir() + program_name_ + "_types.mli";
  f_types_i_.open(f_types_i_name.c_str());

  string f_consts_name = get_out_dir() + program_name_ + "_consts.ml";
  f_consts_.open(f_consts_name.c_str());

  // Print header
  f_types_ << ocaml_autogen_comment() << '\n' << ocaml_imports() << '\n';
  f_types_i_ << ocaml_autogen_comment() << '\n' << ocaml_imports() << '\n';
  f_consts_ << ocaml_autogen_comment() << '\n' << ocaml_imports() << '\n' << "open "
            << capitalize(program_name_) << "_types" << '\n';
}

/**
 * Autogen'd comment
 */
string t_ocaml_generator::ocaml_autogen_comment() {
  return std::string("(*\n") + " Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + "\n"
         + " DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING\n" + "*)\n";
}

/**
 * Prints standard thrift imports
 */
string t_ocaml_generator::ocaml_imports() {
  return "open Thrift";
}

t_ocaml_generator::~t_ocaml_generator() {
  f_consts_.close();
  f_types_.close();
  f_types_i_.close();
  f_service_.close();
  f_service_i_.close();
}

/**
 * Generates a typedef. Ez.
 *
 * @param ttypedef The type definition
 */
void t_ocaml_generator::generate_typedef(t_typedef* ttypedef) {
  f_types_ << indent() << "type " << decapitalize(ttypedef->get_symbolic()) << " = "
           << render_ocaml_type(ttypedef->get_type()) << '\n' << '\n';
  f_types_i_ << indent() << "type " << decapitalize(ttypedef->get_symbolic()) << " = "
             << render_ocaml_type(ttypedef->get_type()) << '\n' << '\n';
}

/**
 * Generates code for an enumerated type.
 * the values.
 *
 * @param tenum The enumeration
 */
void t_ocaml_generator::generate_enum(t_enum* tenum) {
  indent(f_types_) << "module " << capitalize(tenum->get_name()) << " = " << '\n' << "struct"
                   << '\n';
  indent(f_types_i_) << "module " << capitalize(tenum->get_name()) << " : " << '\n' << "sig"
                     << '\n';
  indent_up();
  indent(f_types_) << "type t = " << '\n';
  indent(f_types_i_) << "type t = " << '\n';
  indent_up();
  vector<t_enum_value*> constants = tenum->get_constants();
  vector<t_enum_value*>::iterator c_iter;
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    string name = capitalize((*c_iter)->get_name());
    indent(f_types_) << "| " << name << '\n';
    indent(f_types_i_) << "| " << name << '\n';
  }
  indent_down();

  indent(f_types_) << "let to_i = function" << '\n';
  indent(f_types_i_) << "val to_i : t -> Int32.t" << '\n';
  indent_up();
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    int value = (*c_iter)->get_value();
    string name = capitalize((*c_iter)->get_name());
    indent(f_types_) << "| " << name << " -> " << value << "l" << '\n';
  }
  indent_down();

  indent(f_types_) << "let of_i = function" << '\n';
  indent(f_types_i_) << "val of_i : Int32.t -> t" << '\n';
  indent_up();
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
    int value = (*c_iter)->get_value();
    string name = capitalize((*c_iter)->get_name());
    indent(f_types_) << "| " << value << "l -> " << name << '\n';
  }
  indent(f_types_) << "| _ -> raise Thrift_error" << '\n';
  indent_down();
  indent_down();
  indent(f_types_) << "end" << '\n';
  indent(f_types_i_) << "end" << '\n';
}

/**
 * Generate a constant value
 */
void t_ocaml_generator::generate_const(t_const* tconst) {
  t_type* type = tconst->get_type();
  string name = decapitalize(tconst->get_name());
  t_const_value* value = tconst->get_value();

  indent(f_consts_) << "let " << name << " = " << render_const_value(type, value) << '\n' << '\n';
}

/**
 * Prints the value of a constant with the given type. Note that type checking
 * is NOT performed in this function as it is always run beforehand using the
 * validate_types method in main.cc
 */
string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value) {
  type = get_true_type(type);
  std::ostringstream out;
  // OCaml requires all floating point numbers contain a decimal point
  out.setf(ios::showpoint);
  if (type->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_STRING:
      out << '"' << get_escaped_string(value) << '"';
      break;
    case t_base_type::TYPE_BOOL:
      out << (value->get_integer() > 0 ? "true" : "false");
      break;
    case t_base_type::TYPE_I8:
    case t_base_type::TYPE_I16:
      out << value->get_integer();
      break;
    case t_base_type::TYPE_I32:
      out << value->get_integer() << "l";
      break;
    case t_base_type::TYPE_I64:
      out << value->get_integer() << "L";
      break;
    case t_base_type::TYPE_DOUBLE:
      if (value->get_type() == t_const_value::CV_INTEGER) {
        out << value->get_integer() << ".0";
      } else {
        out << value->get_double();
      }
      break;
    default:
      throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
    }
  } else if (type->is_enum()) {
    t_enum* tenum = (t_enum*)type;
    vector<t_enum_value*> constants = tenum->get_constants();
    vector<t_enum_value*>::iterator c_iter;
    for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
      int val = (*c_iter)->get_value();
      if (val == value->get_integer()) {
        indent(out) << capitalize(tenum->get_name()) << "." << capitalize((*c_iter)->get_name());
        break;
      }
    }
  } else if (type->is_struct() || type->is_xception()) {
    string cname = type_name(type);
    string ct = tmp("_c");
    out << '\n';
    indent_up();
    indent(out) << "(let " << ct << " = new " << cname << " in" << '\n';
    indent_up();
    const vector<t_field*>& fields = ((t_struct*)type)->get_members();
    vector<t_field*>::const_iterator f_iter;
    const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
    map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      t_type* field_type = nullptr;
      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
        if ((*f_iter)->get_name() == v_iter->first->get_string()) {
          field_type = (*f_iter)->get_type();
        }
      }
      if (field_type == nullptr) {
        throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
      }
      string fname = v_iter->first->get_string();
      out << indent();
      out << ct << "#set_" << fname << " ";
      out << render_const_value(field_type, v_iter->second);
      out << ";" << '\n';
    }
    indent(out) << ct << ")";
    indent_down();
    indent_down();
  } else if (type->is_map()) {
    t_type* ktype = ((t_map*)type)->get_key_type();
    t_type* vtype = ((t_map*)type)->get_val_type();
    const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
    map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
    string hm = tmp("_hm");
    out << '\n';
    indent_up();
    indent(out) << "(let " << hm << " = Hashtbl.create " << val.size() << " in" << '\n';
    indent_up();
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      string key = render_const_value(ktype, v_iter->first);
      string val = render_const_value(vtype, v_iter->second);
      indent(out) << "Hashtbl.add " << hm << " " << key << " " << val << ";" << '\n';
    }
    indent(out) << hm << ")";
    indent_down();
    indent_down();
  } else if (type->is_list()) {
    t_type* etype;
    etype = ((t_list*)type)->get_elem_type();
    out << "[" << '\n';
    indent_up();
    const vector<t_const_value*>& val = value->get_list();
    vector<t_const_value*>::const_iterator v_iter;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      out << indent();
      out << render_const_value(etype, *v_iter);
      out << ";" << '\n';
    }
    indent_down();
    indent(out) << "]";
  } else if (type->is_set()) {
    t_type* etype = ((t_set*)type)->get_elem_type();
    const vector<t_const_value*>& val = value->get_list();
    vector<t_const_value*>::const_iterator v_iter;
    string hm = tmp("_hm");
    indent(out) << "(let " << hm << " = Hashtbl.create " << val.size() << " in" << '\n';
    indent_up();
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      string val = render_const_value(etype, *v_iter);
      indent(out) << "Hashtbl.add " << hm << " " << val << " true;" << '\n';
    }
    indent(out) << hm << ")" << '\n';
    indent_down();
    out << '\n';
  } else {
    throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
  }
  return out.str();
}

/**
 * Generates a "struct"
 */
void t_ocaml_generator::generate_struct(t_struct* tstruct) {
  generate_ocaml_struct(tstruct, false);
}

/**
 * Generates a struct definition for a thrift exception. Basically the same
 * as a struct, but also has an exception declaration.
 *
 * @param txception The struct definition
 */
void t_ocaml_generator::generate_xception(t_struct* txception) {
  generate_ocaml_struct(txception, true);
}

/**
 * Generates an OCaml struct
 */
void t_ocaml_generator::generate_ocaml_struct(t_struct* tstruct, bool is_exception) {
  generate_ocaml_struct_definition(f_types_, tstruct, is_exception);
  generate_ocaml_struct_sig(f_types_i_, tstruct, is_exception);
}

void t_ocaml_generator::generate_ocaml_method_copy(ostream& out, const vector<t_field*>& members) {
  vector<t_field*>::const_iterator m_iter;

  /* Create a copy of the current object */
  indent(out) << "method copy =" << '\n';
  indent_up();
  indent_up();
  indent(out) << "let _new = Oo.copy self in" << '\n';
  for (m_iter = members.begin(); m_iter != members.end(); ++m_iter)
    generate_ocaml_member_copy(out, *m_iter);

  indent_down();
  indent(out) << "_new" << '\n';
  indent_down();
}

string t_ocaml_generator::struct_member_copy_of(t_type* type, string what) {
  if (type->is_struct() || type->is_xception()) {
    return what + string("#copy");
  }
  if (type->is_map()) {
    string copy_of_k = struct_member_copy_of(((t_map*)type)->get_key_type(), "k");
    string copy_of_v = struct_member_copy_of(((t_map*)type)->get_val_type(), "v");

    if (copy_of_k == "k" && copy_of_v == "v") {
      return string("(Hashtbl.copy ") + what + string(")");
    } else {
      return string(
                 "((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v "
                 "-> Hashtbl.add nh ") + copy_of_k + string(" ") + copy_of_v + string(") oh; nh) ")
             + what + ")";
    }
  }
  if (type->is_set()) {
    string copy_of = struct_member_copy_of(((t_set*)type)->get_elem_type(), "k");

    if (copy_of == "k") {
      return string("(Hashtbl.copy ") + what + string(")");
    } else {
      return string(
                 "((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v "
                 "-> Hashtbl.add nh ") + copy_of + string(" true") + string(") oh; nh) ") + what
             + ")";
    }
  }
  if (type->is_list()) {
    string copy_of = struct_member_copy_of(((t_list*)type)->get_elem_type(), "x");
    if (copy_of != "x") {
      return string("(List.map (fun x -> ") + copy_of + string(") ") + what + string(")");
    } else {
      return what;
    }
  }
  return what;
}

void t_ocaml_generator::generate_ocaml_member_copy(ostream& out, t_field* tmember) {
  string mname = decapitalize(tmember->get_name());
  t_type* type = get_true_type(tmember->get_type());

  string grab_field = string("self#grab_") + mname;
  string copy_of = struct_member_copy_of(type, grab_field);
  if (copy_of != grab_field) {
    indent(out);
    if (!struct_member_persistent(tmember)) {
      out << "if _" << mname << " <> None then" << '\n';
      indent(out) << "  ";
    }
    out << "_new#set_" << mname << " " << copy_of << ";" << '\n';
  }
}

/**
 * Generates a struct definition for a thrift data type.
 *
 * @param tstruct The struct definition
 */
void t_ocaml_generator::generate_ocaml_struct_definition(ostream& out,
                                                         t_struct* tstruct,
                                                         bool is_exception) {
  const vector<t_field*>& members = tstruct->get_members();
  vector<t_field*>::const_iterator m_iter;
  string tname = type_name(tstruct);
  indent(out) << "class " << tname << " =" << '\n';
  indent(out) << "object (self)" << '\n';

  indent_up();

  if (members.size() > 0) {
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
      generate_ocaml_struct_member(out, tname, (*m_iter));
      out << '\n';
    }
  }
  generate_ocaml_method_copy(out, members);
  generate_ocaml_struct_writer(out, tstruct);
  indent_down();
  indent(out) << "end" << '\n';

  if (is_exception) {
    indent(out) << "exception " << capitalize(tname) << " of " << tname << '\n';
  }

  generate_ocaml_struct_reader(out, tstruct);
}

/**
 * Generates a structure member for a thrift data type.
 *
 * @param tname Name of the parent structure for the member
 * @param tmember Member definition
 */
void t_ocaml_generator::generate_ocaml_struct_member(ostream& out,
                                                     string tname,
                                                     t_field* tmember) {
  string x = tmp("_x");
  string mname = decapitalize(tmember->get_name());

  indent(out) << "val mutable _" << mname << " : " << render_ocaml_type(tmember->get_type());
  t_const_value* val = tmember->get_value();
  if (val) {
    if (struct_member_persistent(tmember))
      out << " = " << render_const_value(tmember->get_type(), tmember->get_value()) << '\n';
    else
      out << " option = Some " << render_const_value(tmember->get_type(), tmember->get_value())
          << '\n';
  } else {
    // assert(!struct_member_persistent(tmember))
    out << " option = None" << '\n';
  }

  if (struct_member_persistent(tmember)) {
    indent(out) << "method get_" << mname << " = Some _" << mname << '\n';
    indent(out) << "method grab_" << mname << " = _" << mname << '\n';
    indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- " << x << '\n';
  } else {
    indent(out) << "method get_" << mname << " = _" << mname << '\n';
    indent(out) << "method grab_" << mname << " = match _" << mname
                << " with None->raise (Field_empty \"" << tname << "." << mname << "\") | Some "
                << x << " -> " << x << '\n';
    indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- Some " << x
                << '\n';
    indent(out) << "method unset_" << mname << " = _" << mname << " <- None" << '\n';
  }

  indent(out) << "method reset_" << mname << " = _" << mname << " <- ";
  if (val) {
    if (struct_member_persistent(tmember))
      out << render_const_value(tmember->get_type(), tmember->get_value()) << '\n';
    else
      out << "Some " << render_const_value(tmember->get_type(), tmember->get_value()) << '\n';
  } else {
    out << "None" << '\n';
  }
}

/**
 * Check whether a member of the structure can not have undefined value
 *
 * @param tmember Member definition
 */
bool t_ocaml_generator::struct_member_persistent(t_field* tmember) {
  t_const_value* val = tmember->get_value();
  return (val ? true : false);
}

/**
 * Check whether a member of the structure can be skipped during encoding
 *
 * @param tmember Member definition
 */
bool t_ocaml_generator::struct_member_omitable(t_field* tmember) {
  return (tmember->get_req() != t_field::T_REQUIRED);
}

/**
 * Figure out whether a member of the structure has
 * a cheaply comparable default value.
 *
 * @param tmember Member definition
 */
bool t_ocaml_generator::struct_member_default_cheaply_comparable(t_field* tmember) {
  t_type* type = get_true_type(tmember->get_type());
  t_const_value* val = tmember->get_value();
  if (!val) {
    return false;
  } else if (type->is_base_type()) {
    // Base types are generally cheaply compared for structural equivalence.
    switch (((t_base_type*)type)->get_base()) {
    case t_base_type::TYPE_DOUBLE:
      if (val->get_double() == 0.0)
        return true;
      else
        return false;
    default:
      return true;
    }
  } else if (type->is_list()) {
    // Empty lists are cheaply compared for structural equivalence.
    // Is empty list?
    if (val->get_list().size() == 0)
      return true;
    else
      return false;
  } else {
    return false;
  }
}

/**
 * Generates a struct definition for a thrift data type.
 *
 * @param tstruct The struct definition
 */
void t_ocaml_generator::generate_ocaml_struct_sig(ostream& out,
                                                  t_struct* tstruct,
                                                  bool is_exception) {
  const vector<t_field*>& members = tstruct->get_members();
  vector<t_field*>::const_iterator m_iter;
  string tname = type_name(tstruct);
  indent(out) << "class " << tname << " :" << '\n';
  indent(out) << "object ('a)" << '\n';

  indent_up();

  string x = tmp("_x");
  if (members.size() > 0) {
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
      string mname = decapitalize((*m_iter)->get_name());
      string type = render_ocaml_type((*m_iter)->get_type());
      indent(out) << "method get_" << mname << " : " << type << " option" << '\n';
      indent(out) << "method grab_" << mname << " : " << type << '\n';
      indent(out) << "method set_" << mname << " : " << type << " -> unit" << '\n';
      if (!struct_member_persistent(*m_iter))
        indent(out) << "method unset_" << mname << " : unit" << '\n';
      indent(out) << "method reset_" << mname << " : unit" << '\n';
    }
  }
  indent(out) << "method copy : 'a" << '\n';
  indent(out) << "method write : Protocol.t -> unit" << '\n';
  indent_down();
  indent(out) << "end" << '\n';

  if (is_exception) {
    indent(out) << "exception " << capitalize(tname) << " of " << tname << '\n';
  }

  indent(out) << "val read_" << tname << " : Protocol.t -> " << tname << '\n';
}

/**
 * Generates the read method for a struct
 */
void t_ocaml_generator::generate_ocaml_struct_reader(ostream& out, t_struct* tstruct) {
  const vector<t_field*>& fields = tstruct->get_members();
  vector<t_field*>::const_iterator f_iter;
  string sname = type_name(tstruct);
  string str = tmp("_str");
  string t = tmp("_t");
  string id = tmp("_id");
  indent(out) << "let rec read_" << sname << " (iprot : Protocol.t) =" << '\n';
  indent_up();
  indent(out) << "let " << str << " = new " << sname << " in" << '\n';
  indent_up();
  indent(out) << "ignore(iprot#readStructBegin);" << '\n';

  // Loop over reading in fields
  indent(out) << "(try while true do" << '\n';
  indent_up();
  indent_up();

  // Read beginning field marker
  indent(out) << "let (_," << t << "," << id << ") = iprot#readFieldBegin in" << '\n';

  // Check for field STOP marker and break
  indent(out) << "if " << t << " = Protocol.T_STOP then" << '\n';
  indent_up();
  indent(out) << "raise Break" << '\n';
  indent_down();
  indent(out) << "else ();" << '\n';

  indent(out) << "(match " << id << " with " << '\n';
  indent_up();
  // Generate deserialization code for known cases
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    indent(out) << "| " << (*f_iter)->get_key() << " -> (";
    out << "if " << t << " = " << type_to_enum((*f_iter)->get_type()) << " then" << '\n';
    indent_up();
    indent_up();
    generate_deserialize_field(out, *f_iter, str);
    indent_down();
    out << indent() << "else" << '\n' << indent() << "  iprot#skip " << t << ")" << '\n';
    indent_down();
  }

  // In the default case we skip the field
  out << indent() << "| _ -> "
      << "iprot#skip " << t << ");" << '\n';
  indent_down();
  // Read field end marker
  indent(out) << "iprot#readFieldEnd;" << '\n';
  indent_down();
  indent(out) << "done; ()" << '\n';
  indent_down();
  indent(out) << "with Break -> ());" << '\n';

  indent(out) << "iprot#readStructEnd;" << '\n';

  indent(out) << str << '\n' << '\n';
  indent_down();
  indent_down();
}

void t_ocaml_generator::generate_ocaml_struct_writer(ostream& out, t_struct* tstruct) {
  string name = tstruct->get_name();
  const vector<t_field*>& fields = tstruct->get_sorted_members();
  vector<t_field*>::const_iterator f_iter;
  string str = tmp("_str");
  string f = tmp("_f");

  indent(out) << "method write (oprot : Protocol.t) =" << '\n';
  indent_up();
  indent(out) << "oprot#writeStructBegin \"" << name << "\";" << '\n';

  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    t_field* tmember = (*f_iter);
    string mname = "_" + decapitalize(tmember->get_name());
    string _v;

    if (struct_member_persistent(tmember)) {

      if (struct_member_omitable(tmember) && struct_member_default_cheaply_comparable(tmember)) {
        _v = "_v";
        // Avoid redundant encoding of members having default values.
        indent(out) << "(match " << mname << " with "
                    << render_const_value(tmember->get_type(), tmember->get_value()) << " -> () | "
                    << _v << " -> " << '\n';
      } else {
        _v = mname;
        indent(out) << "(" << '\n';
      }

    } else {

      indent(out) << "(match " << mname << " with ";

      if (struct_member_omitable(tmember)) {
        out << "None -> ()";

        if (struct_member_default_cheaply_comparable(tmember)) {
          // Avoid redundant encoding of members having default values.
          out << " | Some " << render_const_value(tmember->get_type(), tmember->get_value())
              << " -> ()";
        }
        out << " | Some _v -> " << '\n';
      } else {
        out << '\n';
        indent(out) << "| None -> raise (Field_empty \"" << type_name(tstruct) << "." << mname
                    << "\")" << '\n';
        indent(out) << "| Some _v -> " << '\n';
      }

      _v = "_v";
    }
    indent_up();
    // Write field header
    indent(out) << "oprot#writeFieldBegin(\"" << tmember->get_name() << "\","
                << type_to_enum(tmember->get_type()) << "," << tmember->get_key() << ");" << '\n';

    // Write field contents
    generate_serialize_field(out, tmember, _v);

    // Write field closer
    indent(out) << "oprot#writeFieldEnd" << '\n';

    indent_down();
    indent(out) << ");" << '\n';
  }

  // Write the struct map
  out << indent() << "oprot#writeFieldStop;" << '\n' << indent() << "oprot#writeStructEnd" << '\n';

  indent_down();
}

/**
 * Generates a thrift service.
 *
 * @param tservice The service definition
 */
void t_ocaml_generator::generate_service(t_service* tservice) {
  string f_service_name = get_out_dir() + capitalize(service_name_) + ".ml";
  f_service_.open(f_service_name.c_str());
  string f_service_i_name = get_out_dir() + capitalize(service_name_) + ".mli";
  f_service_i_.open(f_service_i_name.c_str());

  f_service_ << ocaml_autogen_comment() << '\n' << ocaml_imports() << '\n';
  f_service_i_ << ocaml_autogen_comment() << '\n' << ocaml_imports() << '\n';

  /* if (tservice->get_extends() != nullptr) {
    f_service_ <<
      "open " << capitalize(tservice->get_extends()->get_name()) << '\n';
    f_service_i_ <<
      "open " << capitalize(tservice->get_extends()->get_name()) << '\n';
  }
  */
  f_service_ << "open " << capitalize(program_name_) << "_types" << '\n' << '\n';

  f_service_i_ << "open " << capitalize(program_name_) << "_types" << '\n' << '\n';

  // Generate the three main parts of the service
  generate_service_helpers(tservice);
  generate_service_interface(tservice);
  generate_service_client(tservice);
  generate_service_server(tservice);
}

/**
 * Generates helper functions for a service.
 *
 * @param tservice The service to generate a header definition for
 */
void t_ocaml_generator::generate_service_helpers(t_service* tservice) {
  vector<t_function*> functions = tservice->get_functions();
  vector<t_function*>::iterator f_iter;

  indent(f_service_) << "(* HELPER FUNCTIONS AND STRUCTURES *)" << '\n' << '\n';

  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    t_struct* ts = (*f_iter)->get_arglist();
    generate_ocaml_struct_definition(f_service_, ts, false);
    generate_ocaml_function_helpers(*f_iter);
  }
}

/**
 * Generates a struct and helpers for a function.
 *
 * @param tfunction The function
 */
void t_ocaml_generator::generate_ocaml_function_helpers(t_function* tfunction) {
  t_struct result(program_, decapitalize(tfunction->get_name()) + "_result");
  t_field success(tfunction->get_returntype(), "success", 0);
  if (!tfunction->get_returntype()->is_void()) {
    result.append(&success);
  }

  t_struct* xs = tfunction->get_xceptions();
  const vector<t_field*>& fields = xs->get_members();
  vector<t_field*>::const_iterator f_iter;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    result.append(*f_iter);
  }
  generate_ocaml_struct_definition(f_service_, &result, false);
}

/**
 * Generates a service interface definition.
 *
 * @param tservice The service to generate a header definition for
 */
void t_ocaml_generator::generate_service_interface(t_service* tservice) {
  f_service_ << indent() << "class virtual iface =" << '\n' << "object (self)" << '\n';
  f_service_i_ << indent() << "class virtual iface :" << '\n' << "object" << '\n';

  indent_up();

  if (tservice->get_extends() != nullptr) {
    string extends = type_name(tservice->get_extends());
    indent(f_service_) << "inherit " << extends << ".iface" << '\n';
    indent(f_service_i_) << "inherit " << extends << ".iface" << '\n';
  }

  vector<t_function*> functions = tservice->get_functions();
  vector<t_function*>::iterator f_iter;
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    string ft = function_type(*f_iter, true, true);
    f_service_ << indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : "
               << ft << '\n';
    f_service_i_ << indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : "
                 << ft << '\n';
  }
  indent_down();
  indent(f_service_) << "end" << '\n' << '\n';
  indent(f_service_i_) << "end" << '\n' << '\n';
}

/**
 * Generates a service client definition. Note that in OCaml, the client doesn't implement iface.
 *This is because
 * The client does not (and should not have to) deal with arguments being None.
 *
 * @param tservice The service to generate a server for.
 */
void t_ocaml_generator::generate_service_client(t_service* tservice) {
  string extends = "";
  indent(f_service_) << "class client (iprot : Protocol.t) (oprot : Protocol.t) =" << '\n'
                     << "object (self)" << '\n';
  indent(f_service_i_) << "class client : Protocol.t -> Protocol.t -> " << '\n' << "object" << '\n';
  indent_up();

  if (tservice->get_extends() != nullptr) {
    extends = type_name(tservice->get_extends());
    indent(f_service_) << "inherit " << extends << ".client iprot oprot as super" << '\n';
    indent(f_service_i_) << "inherit " << extends << ".client" << '\n';
  }
  indent(f_service_) << "val mutable seqid = 0" << '\n';

  // Generate client method implementations
  vector<t_function*> functions = tservice->get_functions();
  vector<t_function*>::const_iterator f_iter;
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    t_struct* arg_struct = (*f_iter)->get_arglist();
    const vector<t_field*>& fields = arg_struct->get_members();
    vector<t_field*>::const_iterator fld_iter;
    string funname = (*f_iter)->get_name();

    // Open function
    indent(f_service_) << "method " << function_signature(*f_iter) << " = " << '\n';
    indent(f_service_i_) << "method " << decapitalize((*f_iter)->get_name()) << " : "
                         << function_type(*f_iter, true, false) << '\n';
    indent_up();
    indent(f_service_) << "self#send_" << funname;

    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
      f_service_ << " " << decapitalize((*fld_iter)->get_name());
    }
    f_service_ << ";" << '\n';

    if (!(*f_iter)->is_oneway()) {
      f_service_ << indent();
      f_service_ << "self#recv_" << funname << '\n';
    }
    indent_down();

    indent(f_service_) << "method private send_" << function_signature(*f_iter) << " = " << '\n';
    indent_up();

    std::string argsname = decapitalize((*f_iter)->get_name() + "_args");

    // Serialize the request header
    f_service_ << indent() << "oprot#writeMessageBegin (\"" << (*f_iter)->get_name() << "\", "
               << ((*f_iter)->is_oneway() ? "Protocol.ONEWAY" : "Protocol.CALL") << ", seqid);"
               << '\n';

    f_service_ << indent() << "let args = new " << argsname << " in" << '\n';
    indent_up();

    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
      f_service_ << indent() << "args#set_" << (*fld_iter)->get_name() << " "
                 << (*fld_iter)->get_name() << ";" << '\n';
    }

    // Write to the stream
    f_service_ << indent() << "args#write oprot;" << '\n' << indent() << "oprot#writeMessageEnd;"
               << '\n' << indent() << "oprot#getTransport#flush" << '\n';

    indent_down();
    indent_down();

    if (!(*f_iter)->is_oneway()) {
      std::string resultname = decapitalize((*f_iter)->get_name() + "_result");
      t_struct noargs(program_);

      t_function recv_function((*f_iter)->get_returntype(),
                               string("recv_") + (*f_iter)->get_name(),
                               &noargs);
      // Open function
      f_service_ << indent() << "method private " << function_signature(&recv_function) << " ="
                 << '\n';
      indent_up();

      // TODO(mcslee): Validate message reply here, seq ids etc.

      f_service_ << indent() << "let (fname, mtype, rseqid) = iprot#readMessageBegin in" << '\n';
      indent_up();
      f_service_ << indent() << "(if mtype = Protocol.EXCEPTION then" << '\n' << indent()
                 << "  let x = Application_Exn.read iprot in" << '\n';
      indent_up();
      f_service_ << indent() << "  (iprot#readMessageEnd;" << indent()
                 << "   raise (Application_Exn.E x))" << '\n';
      indent_down();
      f_service_ << indent() << "else ());" << '\n';
      string res = "_";

      t_struct* xs = (*f_iter)->get_xceptions();
      const std::vector<t_field*>& xceptions = xs->get_members();

      if (!(*f_iter)->get_returntype()->is_void() || xceptions.size() > 0) {
        res = "result";
      }
      f_service_ << indent() << "let " << res << " = read_" << resultname << " iprot in" << '\n';
      indent_up();
      f_service_ << indent() << "iprot#readMessageEnd;" << '\n';

      // Careful, only return _result if not a void function
      if (!(*f_iter)->get_returntype()->is_void()) {
        f_service_ << indent() << "match result#get_success with Some v -> v | None -> (" << '\n';
        indent_up();
      }

      vector<t_field*>::const_iterator x_iter;
      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
        f_service_ << indent() << "(match result#get_" << (*x_iter)->get_name()
                   << " with None -> () | Some _v ->" << '\n';
        indent(f_service_) << "  raise (" << capitalize(exception_ctor((*x_iter)->get_type()))
                           << " _v));" << '\n';
      }

      // Careful, only return _result if not a void function
      if ((*f_iter)->get_returntype()->is_void()) {
        indent(f_service_) << "()" << '\n';
      } else {
        f_service_
            << indent()
            << "raise (Application_Exn.E (Application_Exn.create Application_Exn.MISSING_RESULT \""
            << (*f_iter)->get_name() << " failed: unknown result\")))" << '\n';
        indent_down();
      }

      // Close function
      indent_down();
      indent_down();
      indent_down();
    }
  }

  indent_down();
  indent(f_service_) << "end" << '\n' << '\n';
  indent(f_service_i_) << "end" << '\n' << '\n';
}

/**
 * Generates a service server definition.
 *
 * @param tservice The service to generate a server for.
 */
void t_ocaml_generator::generate_service_server(t_service* tservice) {
  // Generate the dispatch methods
  vector<t_function*> functions = tservice->get_functions();
  vector<t_function*>::iterator f_iter;

  // Generate the header portion
  indent(f_service_) << "class processor (handler : iface) =" << '\n' << indent() << "object (self)"
                     << '\n';
  indent(f_service_i_) << "class processor : iface ->" << '\n' << indent() << "object" << '\n';
  indent_up();

  f_service_ << indent() << "inherit Processor.t" << '\n' << '\n';
  f_service_i_ << indent() << "inherit Processor.t" << '\n' << '\n';
  string extends = "";

  if (tservice->get_extends() != nullptr) {
    extends = type_name(tservice->get_extends());
    indent(f_service_) << "inherit " + extends + ".processor (handler :> " + extends + ".iface)"
                       << '\n';
    indent(f_service_i_) << "inherit " + extends + ".processor" << '\n';
  }

  if (extends.empty()) {
    indent(f_service_) << "val processMap = Hashtbl.create " << functions.size() << '\n';
  }
  indent(f_service_i_)
      << "val processMap : (string, int * Protocol.t * Protocol.t -> unit) Hashtbl.t" << '\n';

  // Generate the server implementation
  indent(f_service_) << "method process iprot oprot =" << '\n';
  indent(f_service_i_) << "method process : Protocol.t -> Protocol.t -> bool" << '\n';
  indent_up();

  f_service_ << indent() << "let (name, typ, seqid)  = iprot#readMessageBegin in" << '\n';
  indent_up();
  // TODO(mcslee): validate message

  // HOT: dictionary function lookup
  f_service_ << indent() << "if Hashtbl.mem processMap name then" << '\n' << indent()
             << "  (Hashtbl.find processMap name) (seqid, iprot, oprot)" << '\n' << indent()
             << "else (" << '\n' << indent() << "  iprot#skip(Protocol.T_STRUCT);" << '\n'
             << indent() << "  iprot#readMessageEnd;" << '\n' << indent()
             << "  let x = Application_Exn.create Application_Exn.UNKNOWN_METHOD (\"Unknown "
                "function \"^name) in" << '\n' << indent()
             << "    oprot#writeMessageBegin(name, Protocol.EXCEPTION, seqid);" << '\n' << indent()
             << "    x#write oprot;" << '\n' << indent() << "    oprot#writeMessageEnd;" << '\n'
             << indent() << "    oprot#getTransport#flush" << '\n' << indent() << ");" << '\n';

  // Read end of args field, the T_STOP, and the struct close
  f_service_ << indent() << "true" << '\n';
  indent_down();
  indent_down();
  // Generate the process subfunctions
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    generate_process_function(tservice, *f_iter);
  }

  indent(f_service_) << "initializer" << '\n';
  indent_up();
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    f_service_ << indent() << "Hashtbl.add processMap \"" << (*f_iter)->get_name()
               << "\" self#process_" << (*f_iter)->get_name() << ";" << '\n';
  }
  indent_down();

  indent_down();
  indent(f_service_) << "end" << '\n' << '\n';
  indent(f_service_i_) << "end" << '\n' << '\n';
}

/**
 * Generates a process function definition.
 *
 * @param tfunction The function to write a dispatcher for
 */
void t_ocaml_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
  (void)tservice;
  // Open function
  indent(f_service_) << "method private process_" << tfunction->get_name()
                     << " (seqid, iprot, oprot) =" << '\n';
  indent_up();

  string argsname = decapitalize(tfunction->get_name()) + "_args";
  string resultname = decapitalize(tfunction->get_name()) + "_result";

  // Generate the function call
  t_struct* arg_struct = tfunction->get_arglist();
  const std::vector<t_field*>& fields = arg_struct->get_members();
  vector<t_field*>::const_iterator f_iter;

  string args = "args";
  if (fields.size() == 0) {
    args = "_";
  }

  f_service_ << indent() << "let " << args << " = read_" << argsname << " iprot in" << '\n';
  indent_up();
  f_service_ << indent() << "iprot#readMessageEnd;" << '\n';

  t_struct* xs = tfunction->get_xceptions();
  const std::vector<t_field*>& xceptions = xs->get_members();
  vector<t_field*>::const_iterator x_iter;

  // Declare result for non oneway function
  if (!tfunction->is_oneway()) {
    f_service_ << indent() << "let result = new " << resultname << " in" << '\n';
    indent_up();
  }

  // Try block for a function with exceptions
  if (xceptions.size() > 0) {
    f_service_ << indent() << "(try" << '\n';
    indent_up();
  }

  f_service_ << indent();
  if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
    f_service_ << "result#set_success ";
  }
  f_service_ << "(handler#" << tfunction->get_name();
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    f_service_ << " args#get_" << (*f_iter)->get_name();
  }
  f_service_ << ");" << '\n';

  if (xceptions.size() > 0) {
    indent_down();
    indent(f_service_) << "with" << '\n';
    indent_up();
    for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
      f_service_ << indent() << "| " << capitalize(exception_ctor((*x_iter)->get_type())) << " "
                 << (*x_iter)->get_name() << " -> " << '\n';
      indent_up();
      indent_up();
      if (!tfunction->is_oneway()) {
        f_service_ << indent() << "result#set_" << (*x_iter)->get_name() << " "
                   << (*x_iter)->get_name() << '\n';
      } else {
        indent(f_service_) << "()";
      }
      indent_down();
      indent_down();
    }
    indent_down();
    f_service_ << indent() << ");" << '\n';
  }

  // Shortcut out here for oneway functions
  if (tfunction->is_oneway()) {
    f_service_ << indent() << "()" << '\n';
    indent_down();
    indent_down();
    return;
  }

  f_service_ << indent() << "oprot#writeMessageBegin (\"" << tfunction->get_name()
             << "\", Protocol.REPLY, seqid);" << '\n' << indent() << "result#write oprot;" << '\n'
             << indent() << "oprot#writeMessageEnd;" << '\n' << indent()
             << "oprot#getTransport#flush" << '\n';

  // Close function
  indent_down();
  indent_down();
  indent_down();
}

/**
 * Deserializes a field of any type.
 */
void t_ocaml_generator::generate_deserialize_field(ostream& out, t_field* tfield, string prefix) {
  t_type* type = tfield->get_type();

  string name = decapitalize(tfield->get_name());
  indent(out) << prefix << "#set_" << name << " ";
  generate_deserialize_type(out, type);
  out << '\n';
}

/**
 * Deserializes a field of any type.
 */
void t_ocaml_generator::generate_deserialize_type(ostream& out, t_type* type) {
  type = get_true_type(type);

  if (type->is_void()) {
    throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE";
  }

  if (type->is_struct() || type->is_xception()) {
    generate_deserialize_struct(out, (t_struct*)type);
  } else if (type->is_container()) {
    generate_deserialize_container(out, type);
  } else if (type->is_base_type()) {
    out << "iprot#";
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_VOID:
      throw "compiler error: cannot serialize void field in a struct";
      break;
    case t_base_type::TYPE_STRING:
      out << "readString";
      break;
    case t_base_type::TYPE_BOOL:
      out << "readBool";
      break;
    case t_base_type::TYPE_I8:
      out << "readByte";
      break;
    case t_base_type::TYPE_I16:
      out << "readI16";
      break;
    case t_base_type::TYPE_I32:
      out << "readI32";
      break;
    case t_base_type::TYPE_I64:
      out << "readI64";
      break;
    case t_base_type::TYPE_DOUBLE:
      out << "readDouble";
      break;
    default:
      throw "compiler error: no ocaml name for base type " + t_base_type::t_base_name(tbase);
    }
  } else if (type->is_enum()) {
    string ename = capitalize(type->get_name());
    out << "(" << ename << ".of_i iprot#readI32)";
  } else {
    printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n", type->get_name().c_str());
  }
}

/**
 * Generates an unserializer for a struct, calling read()
 */
void t_ocaml_generator::generate_deserialize_struct(ostream& out, t_struct* tstruct) {
  string prefix = "";
  t_program* program = tstruct->get_program();
  if (program != nullptr && program != program_) {
    prefix = capitalize(program->get_name()) + "_types.";
  }
  string name = decapitalize(tstruct->get_name());
  out << "(" << prefix << "read_" << name << " iprot)";
}

/**
 * Serialize a container by writing out the header followed by
 * data and then a footer.
 */
void t_ocaml_generator::generate_deserialize_container(ostream& out, t_type* ttype) {
  string size = tmp("_size");
  string ktype = tmp("_ktype");
  string vtype = tmp("_vtype");
  string etype = tmp("_etype");
  string con = tmp("_con");

  t_field fsize(g_type_i32, size);
  t_field fktype(g_type_i8, ktype);
  t_field fvtype(g_type_i8, vtype);
  t_field fetype(g_type_i8, etype);

  out << '\n';
  indent_up();
  // Declare variables, read header
  if (ttype->is_map()) {
    indent(out) << "(let (" << ktype << "," << vtype << "," << size << ") = iprot#readMapBegin in"
                << '\n';
    indent(out) << "let " << con << " = Hashtbl.create " << size << " in" << '\n';
    indent_up();
    indent(out) << "for i = 1 to " << size << " do" << '\n';
    indent_up();
    indent(out) << "let _k = ";
    generate_deserialize_type(out, ((t_map*)ttype)->get_key_type());
    out << " in" << '\n';
    indent(out) << "let _v = ";
    generate_deserialize_type(out, ((t_map*)ttype)->get_val_type());
    out << " in" << '\n';
    indent_up();
    indent(out) << "Hashtbl.add " << con << " _k _v" << '\n';
    indent_down();
    indent_down();
    indent(out) << "done; iprot#readMapEnd; " << con << ")";
    indent_down();
  } else if (ttype->is_set()) {
    indent(out) << "(let (" << etype << "," << size << ") = iprot#readSetBegin in" << '\n';
    indent(out) << "let " << con << " = Hashtbl.create " << size << " in" << '\n';
    indent_up();
    indent(out) << "for i = 1 to " << size << " do" << '\n';
    indent_up();
    indent(out) << "Hashtbl.add " << con << " ";
    generate_deserialize_type(out, ((t_set*)ttype)->get_elem_type());
    out << " true" << '\n';
    indent_down();
    indent(out) << "done; iprot#readSetEnd; " << con << ")";
    indent_down();
  } else if (ttype->is_list()) {
    indent(out) << "(let (" << etype << "," << size << ") = iprot#readListBegin in" << '\n';
    indent_up();
    indent(out) << "let " << con << " = (Array.to_list (Array.init " << size << " (fun _ -> ";
    generate_deserialize_type(out, ((t_list*)ttype)->get_elem_type());
    out << "))) in" << '\n';
    indent_up();
    indent(out) << "iprot#readListEnd; " << con << ")";
    indent_down();
    indent_down();
  }
  indent_down();
}

/**
 * Serializes a field of any type.
 *
 * @param tfield The field to serialize
 * @param prefix Name to prepend to field name
 */
void t_ocaml_generator::generate_serialize_field(ostream& out, t_field* tfield, string name) {
  t_type* type = get_true_type(tfield->get_type());

  // Do nothing for void types
  if (type->is_void()) {
    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + tfield->get_name();
  }

  if (name.length() == 0) {
    name = decapitalize(tfield->get_name());
  }

  if (type->is_struct() || type->is_xception()) {
    generate_serialize_struct(out, (t_struct*)type, name);
  } else if (type->is_container()) {
    generate_serialize_container(out, type, name);
  } else if (type->is_base_type() || type->is_enum()) {

    indent(out) << "oprot#";

    if (type->is_base_type()) {
      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
      switch (tbase) {
      case t_base_type::TYPE_VOID:
        throw "compiler error: cannot serialize void field in a struct: " + name;
        break;
      case t_base_type::TYPE_STRING:
        out << "writeString(" << name << ")";
        break;
      case t_base_type::TYPE_BOOL:
        out << "writeBool(" << name << ")";
        break;
      case t_base_type::TYPE_I8:
        out << "writeByte(" << name << ")";
        break;
      case t_base_type::TYPE_I16:
        out << "writeI16(" << name << ")";
        break;
      case t_base_type::TYPE_I32:
        out << "writeI32(" << name << ")";
        break;
      case t_base_type::TYPE_I64:
        out << "writeI64(" << name << ")";
        break;
      case t_base_type::TYPE_DOUBLE:
        out << "writeDouble(" << name << ")";
        break;
      default:
        throw "compiler error: no ocaml name for base type " + t_base_type::t_base_name(tbase);
      }
    } else if (type->is_enum()) {
      string ename = capitalize(type->get_name());
      out << "writeI32(" << ename << ".to_i " << name << ")";
    }

  } else {
    printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
           tfield->get_name().c_str(),
           type->get_name().c_str());
  }
  out << ";" << '\n';
}

/**
 * Serializes all the members of a struct.
 *
 * @param tstruct The struct to serialize
 * @param prefix  String prefix to attach to all fields
 */
void t_ocaml_generator::generate_serialize_struct(ostream& out, t_struct* tstruct, string prefix) {
  (void)tstruct;
  indent(out) << prefix << "#write(oprot)";
}

void t_ocaml_generator::generate_serialize_container(ostream& out, t_type* ttype, string prefix) {
  if (ttype->is_map()) {
    indent(out) << "oprot#writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ",";
    out << type_to_enum(((t_map*)ttype)->get_val_type()) << ",";
    out << "Hashtbl.length " << prefix << ");" << '\n';
  } else if (ttype->is_set()) {
    indent(out) << "oprot#writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ",";
    out << "Hashtbl.length " << prefix << ");" << '\n';
  } else if (ttype->is_list()) {
    indent(out) << "oprot#writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
                << ",";
    out << "List.length " << prefix << ");" << '\n';
  }

  if (ttype->is_map()) {
    string kiter = tmp("_kiter");
    string viter = tmp("_viter");
    indent(out) << "Hashtbl.iter (fun " << kiter << " -> fun " << viter << " -> " << '\n';
    indent_up();
    generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
    indent_down();
    indent(out) << ") " << prefix << ";" << '\n';
  } else if (ttype->is_set()) {
    string iter = tmp("_iter");
    indent(out) << "Hashtbl.iter (fun " << iter << " -> fun _ -> ";
    indent_up();
    generate_serialize_set_element(out, (t_set*)ttype, iter);
    indent_down();
    indent(out) << ") " << prefix << ";" << '\n';
  } else if (ttype->is_list()) {
    string iter = tmp("_iter");
    indent(out) << "List.iter (fun " << iter << " -> ";
    indent_up();
    generate_serialize_list_element(out, (t_list*)ttype, iter);
    indent_down();
    indent(out) << ") " << prefix << ";" << '\n';
  }

  if (ttype->is_map()) {
    indent(out) << "oprot#writeMapEnd";
  } else if (ttype->is_set()) {
    indent(out) << "oprot#writeSetEnd";
  } else if (ttype->is_list()) {
    indent(out) << "oprot#writeListEnd";
  }
}

/**
 * Serializes the members of a map.
 *
 */
void t_ocaml_generator::generate_serialize_map_element(ostream& out,
                                                       t_map* tmap,
                                                       string kiter,
                                                       string viter) {
  t_field kfield(tmap->get_key_type(), kiter);
  generate_serialize_field(out, &kfield);

  t_field vfield(tmap->get_val_type(), viter);
  generate_serialize_field(out, &vfield);
}

/**
 * Serializes the members of a set.
 */
void t_ocaml_generator::generate_serialize_set_element(ostream& out, t_set* tset, string iter) {
  t_field efield(tset->get_elem_type(), iter);
  generate_serialize_field(out, &efield);
}

/**
 * Serializes the members of a list.
 */
void t_ocaml_generator::generate_serialize_list_element(ostream& out, t_list* tlist, string iter) {
  t_field efield(tlist->get_elem_type(), iter);
  generate_serialize_field(out, &efield);
}

/**
 * Renders a function signature of the form 'name args'
 *
 * @param tfunction Function definition
 * @return String of rendered function definition
 */
string t_ocaml_generator::function_signature(t_function* tfunction, string prefix) {
  return prefix + decapitalize(tfunction->get_name()) + " "
         + argument_list(tfunction->get_arglist());
}

string t_ocaml_generator::function_type(t_function* tfunc, bool method, bool options) {
  string result = "";

  const vector<t_field*>& fields = tfunc->get_arglist()->get_members();
  vector<t_field*>::const_iterator f_iter;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    result += render_ocaml_type((*f_iter)->get_type());
    if (options)
      result += " option";
    result += " -> ";
  }
  if (fields.empty() && !method) {
    result += "unit -> ";
  }
  result += render_ocaml_type(tfunc->get_returntype());
  return result;
}

/**
 * Renders a field list
 */
string t_ocaml_generator::argument_list(t_struct* tstruct) {
  string result = "";

  const vector<t_field*>& fields = tstruct->get_members();
  vector<t_field*>::const_iterator f_iter;
  bool first = true;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    if (first) {
      first = false;
    } else {
      result += " ";
    }
    result += (*f_iter)->get_name();
  }
  return result;
}

string t_ocaml_generator::type_name(t_type* ttype) {
  string prefix = "";
  t_program* program = ttype->get_program();
  if (program != nullptr && program != program_) {
    if (!ttype->is_service()) {
      prefix = capitalize(program->get_name()) + "_types.";
    }
  }

  string name = ttype->get_name();
  if (ttype->is_service()) {
    name = capitalize(name);
  } else {
    name = decapitalize(name);
  }
  return prefix + name;
}

string t_ocaml_generator::exception_ctor(t_type* ttype) {
  string prefix = "";
  t_program* program = ttype->get_program();
  if (program != nullptr && program != program_) {
    if (!ttype->is_service()) {
      prefix = capitalize(program->get_name()) + "_types.";
    }
  }

  return prefix + capitalize(ttype->get_name());
}

/**
 * Converts the parse type to a Protocol.t_type enum
 */
string t_ocaml_generator::type_to_enum(t_type* type) {
  type = get_true_type(type);

  if (type->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_VOID:
      return "Protocol.T_VOID";
    case t_base_type::TYPE_STRING:
      return "Protocol.T_STRING";
    case t_base_type::TYPE_BOOL:
      return "Protocol.T_BOOL";
    case t_base_type::TYPE_I8:
      return "Protocol.T_BYTE";
    case t_base_type::TYPE_I16:
      return "Protocol.T_I16";
    case t_base_type::TYPE_I32:
      return "Protocol.T_I32";
    case t_base_type::TYPE_I64:
      return "Protocol.T_I64";
    case t_base_type::TYPE_DOUBLE:
      return "Protocol.T_DOUBLE";
    default:
      throw "compiler error: unhandled type";
    }
  } else if (type->is_enum()) {
    return "Protocol.T_I32";
  } else if (type->is_struct() || type->is_xception()) {
    return "Protocol.T_STRUCT";
  } else if (type->is_map()) {
    return "Protocol.T_MAP";
  } else if (type->is_set()) {
    return "Protocol.T_SET";
  } else if (type->is_list()) {
    return "Protocol.T_LIST";
  }

  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}

/**
 * Converts the parse type to an ocaml type
 */
string t_ocaml_generator::render_ocaml_type(t_type* type) {
  type = get_true_type(type);

  if (type->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_VOID:
      return "unit";
    case t_base_type::TYPE_STRING:
      return "string";
    case t_base_type::TYPE_BOOL:
      return "bool";
    case t_base_type::TYPE_I8:
      return "int";
    case t_base_type::TYPE_I16:
      return "int";
    case t_base_type::TYPE_I32:
      return "Int32.t";
    case t_base_type::TYPE_I64:
      return "Int64.t";
    case t_base_type::TYPE_DOUBLE:
      return "float";
    default:
      throw "compiler error: unhandled type";
    }
  } else if (type->is_enum()) {
    return capitalize(((t_enum*)type)->get_name()) + ".t";
  } else if (type->is_struct() || type->is_xception()) {
    return type_name((t_struct*)type);
  } else if (type->is_map()) {
    t_type* ktype = ((t_map*)type)->get_key_type();
    t_type* vtype = ((t_map*)type)->get_val_type();
    return "(" + render_ocaml_type(ktype) + "," + render_ocaml_type(vtype) + ") Hashtbl.t";
  } else if (type->is_set()) {
    t_type* etype = ((t_set*)type)->get_elem_type();
    return "(" + render_ocaml_type(etype) + ",bool) Hashtbl.t";
  } else if (type->is_list()) {
    t_type* etype = ((t_list*)type)->get_elem_type();
    return render_ocaml_type(etype) + " list";
  }

  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}

std::string t_ocaml_generator::display_name() const {
  return "OCaml";
}


THRIFT_REGISTER_GENERATOR(ocaml, "OCaml", "")
