Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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
 */
19
 
20
#include <string>
21
#include <fstream>
22
#include <iostream>
23
#include <vector>
24
 
25
#include <stdlib.h>
26
#include <sys/stat.h>
27
#include <sys/types.h>
28
#include <sstream>
29
#include "t_oop_generator.h"
30
#include "platform.h"
31
using namespace std;
32
 
33
 
34
/**
35
 * Haskell code generator.
36
 *
37
 */
38
class t_hs_generator : public t_oop_generator {
39
 public:
40
  t_hs_generator(
41
      t_program* program,
42
      const std::map<std::string, std::string>& parsed_options,
43
      const std::string& option_string)
44
    : t_oop_generator(program)
45
  {
46
    out_dir_base_ = "gen-hs";
47
  }
48
 
49
  /**
50
   * Init and close methods
51
   */
52
 
53
  void init_generator();
54
  void close_generator();
55
 
56
  /**
57
   * Program-level generation functions
58
   */
59
  void generate_typedef  (t_typedef*  ttypedef);
60
  void generate_enum     (t_enum*     tenum);
61
  void generate_const    (t_const*    tconst);
62
  void generate_struct   (t_struct*   tstruct);
63
  void generate_xception (t_struct*   txception);
64
  void generate_service  (t_service*  tservice);
65
 
66
  std::string render_const_value(t_type* type, t_const_value* value);
67
 
68
  /**
69
   * Struct generation code
70
   */
71
 
72
  void generate_hs_struct(t_struct* tstruct, bool is_exception);
73
  void generate_hs_struct_definition(std::ofstream &out,t_struct* tstruct, bool is_xception=false,bool helper=false);
74
  void generate_hs_struct_reader(std::ofstream& out, t_struct* tstruct);
75
  void generate_hs_struct_writer(std::ofstream& out, t_struct* tstruct);
76
  void generate_hs_function_helpers(t_function* tfunction);
77
 
78
  /**
79
   * Service-level generation functions
80
   */
81
 
82
  void generate_service_helpers   (t_service*  tservice);
83
  void generate_service_interface (t_service* tservice);
84
  void generate_service_client    (t_service* tservice);
85
  void generate_service_server    (t_service* tservice);
86
  void generate_process_function  (t_service* tservice, t_function* tfunction);
87
 
88
  /**
89
   * Serialization constructs
90
   */
91
 
92
  void generate_deserialize_field        (std::ofstream &out,
93
                                          t_field*    tfield,
94
                                          std::string prefix);
95
 
96
  void generate_deserialize_struct       (std::ofstream &out,
97
                                          t_struct*   tstruct);
98
 
99
  void generate_deserialize_container    (std::ofstream &out,
100
                                          t_type*     ttype);
101
 
102
  void generate_deserialize_set_element  (std::ofstream &out,
103
                                          t_set*      tset);
104
 
105
 
106
  void generate_deserialize_list_element (std::ofstream &out,
107
                                          t_list*     tlist,
108
                                          std::string prefix="");
109
  void generate_deserialize_type          (std::ofstream &out,
110
                                           t_type* type);
111
 
112
  void generate_serialize_field          (std::ofstream &out,
113
                                          t_field*    tfield,
114
                                          std::string name= "");
115
 
116
  void generate_serialize_struct         (std::ofstream &out,
117
                                          t_struct*   tstruct,
118
                                          std::string prefix="");
119
 
120
  void generate_serialize_container      (std::ofstream &out,
121
                                          t_type*     ttype,
122
                                          std::string prefix="");
123
 
124
  void generate_serialize_map_element    (std::ofstream &out,
125
                                          t_map*      tmap,
126
                                          std::string kiter,
127
                                          std::string viter);
128
 
129
  void generate_serialize_set_element    (std::ofstream &out,
130
                                          t_set*      tmap,
131
                                          std::string iter);
132
 
133
  void generate_serialize_list_element   (std::ofstream &out,
134
                                          t_list*     tlist,
135
                                          std::string iter);
136
 
137
  /**
138
   * Helper rendering functions
139
   */
140
 
141
  std::string hs_autogen_comment();
142
  std::string hs_imports();
143
  std::string type_name(t_type* ttype);
144
  std::string function_type(t_function* tfunc, bool options = false, bool io = false, bool method = false);
145
  std::string type_to_enum(t_type* ttype);
146
  std::string render_hs_type(t_type* type, bool needs_parens = true);
147
 
148
 
149
 private:
150
 
151
  /**
152
   * File streams
153
   */
154
 
155
  std::ofstream f_types_;
156
  std::ofstream f_consts_;
157
  std::ofstream f_service_;
158
  std::ofstream f_iface_;
159
  std::ofstream f_client_;
160
 
161
};
162
 
163
 
164
/**
165
 * Prepares for file generation by opening up the necessary file output
166
 * streams.
167
 *
168
 * @param tprogram The program to generate
169
 */
170
void t_hs_generator::init_generator() {
171
  // Make output directory
172
  MKDIR(get_out_dir().c_str());
173
 
174
  // Make output file
175
 
176
  string pname = capitalize(program_name_);
177
  string f_types_name = get_out_dir()+pname+"_Types.hs";
178
  f_types_.open(f_types_name.c_str());
179
 
180
  string f_consts_name = get_out_dir()+pname+"_Consts.hs";
181
  f_consts_.open(f_consts_name.c_str());
182
 
183
  // Print header
184
  f_types_ <<
185
    hs_autogen_comment() << endl <<
186
    "module " << pname <<"_Types where" << endl <<
187
    hs_imports() << endl;
188
 
189
  f_consts_ <<
190
    hs_autogen_comment() << endl <<
191
    "module " << pname <<"_Consts where" << endl <<
192
    hs_imports() << endl <<
193
    "import " << pname<<"_Types"<< endl;
194
 
195
}
196
 
197
 
198
/**
199
 * Autogen'd comment
200
 */
201
string t_hs_generator::hs_autogen_comment() {
202
  return
203
    std::string("-----------------------------------------------------------------\n") +
204
    "-- Autogenerated by Thrift                                     --\n" +
205
    "--                                                             --\n" +
206
    "-- DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING --\n" +
207
    "-----------------------------------------------------------------\n";
208
}
209
 
210
/**
211
 * Prints standard thrift imports
212
 */
213
string t_hs_generator::hs_imports() {
214
  return "import Thrift\nimport Data.Typeable ( Typeable )\nimport Control.Exception\nimport qualified Data.Map as Map\nimport qualified Data.Set as Set\nimport Data.Int";
215
}
216
 
217
/**
218
 * Closes the type files
219
 */
220
void t_hs_generator::close_generator() {
221
  // Close types file
222
  f_types_.close();
223
  f_consts_.close();
224
}
225
 
226
/**
227
 * Generates a typedef. Ez.
228
 *
229
 * @param ttypedef The type definition
230
 */
231
void t_hs_generator::generate_typedef(t_typedef* ttypedef) {
232
  f_types_ <<
233
    indent() << "type "<< capitalize(ttypedef->get_symbolic()) << " = " << render_hs_type(ttypedef->get_type(), false) << endl << endl;
234
}
235
 
236
/**
237
 * Generates code for an enumerated type.
238
 * the values.
239
 *
240
 * @param tenum The enumeration
241
 */
242
void t_hs_generator::generate_enum(t_enum* tenum) {
243
  indent(f_types_) << "data "<<capitalize(tenum->get_name())<<" = ";
244
  indent_up();
245
  vector<t_enum_value*> constants = tenum->get_constants();
246
  vector<t_enum_value*>::iterator c_iter;
247
  bool first = true;
248
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
249
    string name = capitalize((*c_iter)->get_name());
250
    if(first)
251
      first=false;
252
    else
253
      f_types_ << "|";
254
    f_types_ << name;
255
  }
256
  indent(f_types_) << "deriving (Show,Eq, Typeable, Ord)" << endl;
257
  indent_down();
258
 
259
  int value = -1;
260
  indent(f_types_) << "instance Enum " << capitalize(tenum->get_name()) << " where" << endl;
261
  indent_up();
262
  indent(f_types_) << "fromEnum t = case t of" << endl;
263
  indent_up();
264
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
265
    if ((*c_iter)->has_value()) {
266
      value = (*c_iter)->get_value();
267
    } else {
268
      ++value;
269
    }
270
    string name = capitalize((*c_iter)->get_name());
271
 
272
    f_types_ <<
273
      indent() << name << " -> " << value << endl;
274
  }
275
  indent_down();
276
 
277
  indent(f_types_) << "toEnum t = case t of" << endl;
278
  indent_up();
279
  for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
280
    if ((*c_iter)->has_value()) {
281
      value = (*c_iter)->get_value();
282
    } else {
283
      ++value;
284
    }
285
    string name = capitalize((*c_iter)->get_name());
286
 
287
    f_types_ <<
288
      indent() << value << " -> " << name << endl;
289
  }
290
  indent(f_types_) << "_ -> throw ThriftException" << endl;
291
  indent_down();
292
  indent_down();
293
}
294
 
295
/**
296
 * Generate a constant value
297
 */
298
void t_hs_generator::generate_const(t_const* tconst) {
299
  t_type* type = tconst->get_type();
300
  string name = decapitalize(tconst->get_name());
301
  t_const_value* value = tconst->get_value();
302
 
303
  indent(f_consts_) << name << " :: " << render_hs_type(type, false) << endl;
304
  indent(f_consts_) << name << " = " << render_const_value(type, value) << endl << endl;
305
}
306
 
307
/**
308
 * Prints the value of a constant with the given type. Note that type checking
309
 * is NOT performed in this function as it is always run beforehand using the
310
 * validate_types method in main.cc
311
 */
312
string t_hs_generator::render_const_value(t_type* type, t_const_value* value) {
313
  type = get_true_type(type);
314
  std::ostringstream out;
315
  if (type->is_base_type()) {
316
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
317
    switch (tbase) {
318
    case t_base_type::TYPE_STRING:
319
      out << '"' << get_escaped_string(value) << '"';
320
      break;
321
    case t_base_type::TYPE_BOOL:
322
      out << (value->get_integer() > 0 ? "True" : "False");
323
      break;
324
    case t_base_type::TYPE_BYTE:
325
    case t_base_type::TYPE_I16:
326
    case t_base_type::TYPE_I32:
327
    case t_base_type::TYPE_I64:
328
      out << value->get_integer();
329
      break;
330
    case t_base_type::TYPE_DOUBLE:
331
      if (value->get_type() == t_const_value::CV_INTEGER) {
332
        out << value->get_integer();
333
      } else {
334
        out << value->get_double();
335
      }
336
      break;
337
    default:
338
      throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
339
    }
340
  } else if (type->is_enum()) {
341
    t_enum* tenum = (t_enum*)type;
342
    vector<t_enum_value*> constants = tenum->get_constants();
343
    vector<t_enum_value*>::iterator c_iter;
344
    int val = -1;
345
    for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
346
      if ((*c_iter)->has_value()) {
347
        val = (*c_iter)->get_value();
348
      } else {
349
        ++val;
350
      }
351
      if(val == value->get_integer()){
352
        indent(out) << capitalize((*c_iter)->get_name());
353
        break;
354
      }
355
    }
356
  } else if (type->is_struct() || type->is_xception()) {
357
    string cname = type_name(type);
358
    indent(out) << cname << "{";
359
    const vector<t_field*>& fields = ((t_struct*)type)->get_members();
360
    vector<t_field*>::const_iterator f_iter;
361
    const map<t_const_value*, t_const_value*>& val = value->get_map();
362
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
363
    bool first = true;
364
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
365
      t_type* field_type = NULL;
366
      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
367
        if ((*f_iter)->get_name() == v_iter->first->get_string()) {
368
          field_type = (*f_iter)->get_type();
369
        }
370
      }
371
      if (field_type == NULL) {
372
        throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
373
      }
374
      string fname = v_iter->first->get_string();
375
      if(first)
376
        first=false;
377
      else
378
        out << ",";
379
      out << "f_" << cname << "_" << fname << " = Just (" << render_const_value(field_type, v_iter->second) << ")";
380
 
381
    }
382
    indent(out) << "}";
383
  } else if (type->is_map()) {
384
    t_type* ktype = ((t_map*)type)->get_key_type();
385
    t_type* vtype = ((t_map*)type)->get_val_type();
386
    const map<t_const_value*, t_const_value*>& val = value->get_map();
387
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
388
    out << "(Map.fromList [";
389
    bool first=true;
390
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
391
      string key = render_const_value(ktype, v_iter->first);
392
      string val = render_const_value(vtype, v_iter->second);
393
      if(first)
394
        first=false;
395
      else
396
        out << ",";
397
      out << "(" << key << ","<< val << ")";
398
    }
399
    out << "])";
400
  } else if (type->is_list() || type->is_set()) {
401
    t_type* etype;
402
 
403
    if (type->is_list()) {
404
        etype = ((t_list*) type)->get_elem_type();
405
    } else  {
406
        etype = ((t_set*) type)->get_elem_type();
407
    }
408
 
409
    const vector<t_const_value*>& val = value->get_list();
410
    vector<t_const_value*>::const_iterator v_iter;
411
    bool first = true;
412
 
413
    if (type->is_set())
414
        out << "(Set.fromList ";
415
 
416
    out << "[";
417
 
418
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
419
      if(first)
420
        first=false;
421
      else
422
        out << ",";
423
      out << render_const_value(etype, *v_iter);
424
    }
425
 
426
    out << "]";
427
    if (type->is_set())
428
        out << ")";
429
  } else {
430
    throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
431
  }
432
  return out.str();
433
}
434
 
435
/**
436
 * Generates a "struct"
437
 */
438
void t_hs_generator::generate_struct(t_struct* tstruct) {
439
  generate_hs_struct(tstruct, false);
440
}
441
 
442
/**
443
 * Generates a struct definition for a thrift exception. Basically the same
444
 * as a struct, but also has an exception declaration.
445
 *
446
 * @param txception The struct definition
447
 */
448
void t_hs_generator::generate_xception(t_struct* txception) {
449
  generate_hs_struct(txception, true);
450
}
451
 
452
/**
453
 * Generates a Haskell struct
454
 */
455
void t_hs_generator::generate_hs_struct(t_struct* tstruct,
456
                                              bool is_exception) {
457
  generate_hs_struct_definition(f_types_,tstruct, is_exception,false);
458
}
459
 
460
/**
461
 * Generates a struct definition for a thrift data type.
462
 *
463
 * @param tstruct The struct definition
464
 */
465
void t_hs_generator::generate_hs_struct_definition(ofstream& out,
466
                                                   t_struct* tstruct,
467
                                                   bool is_exception,
468
                                                   bool helper) {
469
  string tname = type_name(tstruct);
470
  string name = tstruct->get_name();
471
  const vector<t_field*>& members = tstruct->get_members();
472
  vector<t_field*>::const_iterator m_iter;
473
 
474
  indent(out) << "data "<<tname<<" = "<<tname;
475
  if (members.size() > 0) {
476
    out << "{";
477
    bool first=true;
478
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
479
      if(first)
480
        first=false;
481
      else
482
        out << ",";
483
      string mname = (*m_iter)->get_name();
484
      out << "f_" << tname << "_" << mname << " :: Maybe " << render_hs_type((*m_iter)->get_type());
485
    }
486
    out << "}";
487
  }
488
 
489
  out << " deriving (Show,Eq,Ord,Typeable)" << endl;
490
  if (is_exception) out << "instance Exception " << tname << endl;
491
  generate_hs_struct_writer(out, tstruct);
492
 
493
  generate_hs_struct_reader(out, tstruct);
494
  //f_struct_.close();
495
}
496
 
497
 
498
 
499
/**
500
 * Generates the read method for a struct
501
 */
502
void t_hs_generator::generate_hs_struct_reader(ofstream& out, t_struct* tstruct) {
503
  const vector<t_field*>& fields = tstruct->get_members();
504
  vector<t_field*>::const_iterator f_iter;
505
  string sname = type_name(tstruct);
506
  string str = tmp("_str");
507
  string t = tmp("_t");
508
  string id = tmp("_id");
509
 
510
  indent(out) << "read_" << sname << "_fields iprot rec = do" << endl;
511
  indent_up(); // do
512
 
513
  // Read beginning field marker
514
  indent(out) << "(_," << t <<","<<id<<") <- readFieldBegin iprot" << endl;
515
  // Check for field STOP marker and break
516
  indent(out) <<
517
    "if " << t <<" == T_STOP then return rec else" << endl;
518
  indent_up(); // if
519
  indent(out) << "case " << id<<" of " << endl;
520
  indent_up(); // case
521
  // Generate deserialization code for known cases
522
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
523
    indent(out) << (*f_iter)->get_key() << " -> ";
524
    out << "if " << t <<" == " << type_to_enum((*f_iter)->get_type()) << " then do" << endl;
525
    indent_up(); // if
526
    indent(out) << "s <- ";
527
    generate_deserialize_field(out, *f_iter,str);
528
    out << endl;
529
    indent(out) << "read_"<<sname<<"_fields iprot rec{f_"<<sname<<"_"<< decapitalize((*f_iter)->get_name()) <<"=Just s}" << endl;
530
    out <<
531
      indent() << "else do" << endl;
532
    indent_up();
533
    indent(out) << "skip iprot "<< t << endl;
534
    indent(out) << "read_"<<sname<<"_fields iprot rec" << endl;
535
    indent_down(); // -do
536
    indent_down(); // -if
537
  }
538
 
539
 
540
  // In the default case we skip the field
541
  out <<
542
    indent() << "_ -> do" << endl;
543
  indent_up();
544
  indent(out) << "skip iprot "<<t<< endl;
545
  indent(out) << "readFieldEnd iprot" << endl;
546
  indent(out) << "read_"<<sname<<"_fields iprot rec" << endl;
547
  indent_down(); // -case
548
  indent_down(); // -if
549
  indent_down(); // -do
550
  indent_down();
551
 
552
  // read
553
  indent(out) << "read_"<<sname<<" iprot = do" << endl;
554
  indent_up();
555
  indent(out) << "readStructBegin iprot" << endl;
556
  indent(out) << "rec <- read_"<<sname<<"_fields iprot ("<<sname<<"{";
557
  bool first = true;
558
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
559
    if(first)
560
      first=false;
561
    else
562
      out << ",";
563
    out << "f_" << sname << "_" << decapitalize((*f_iter)->get_name()) << "=Nothing";
564
  }
565
  out << "})" << endl;
566
  indent(out) << "readStructEnd iprot" << endl;
567
  indent(out) << "return rec" << endl;
568
  indent_down();
569
}
570
 
571
void t_hs_generator::generate_hs_struct_writer(ofstream& out,
572
                                               t_struct* tstruct) {
573
  string name = type_name(tstruct);
574
  const vector<t_field*>& fields = tstruct->get_sorted_members();
575
  vector<t_field*>::const_iterator f_iter;
576
  string str = tmp("_str");
577
  string f = tmp("_f");
578
 
579
  indent(out) <<
580
    "write_"<<name<<" oprot rec = do" << endl;
581
  indent_up();
582
  indent(out) <<
583
    "writeStructBegin oprot \""<<name<<"\"" << endl;
584
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
585
    // Write field header
586
    string mname = (*f_iter)->get_name();
587
    indent(out) <<
588
      "case f_" << name << "_" << mname << " rec of {Nothing -> return (); Just _v -> do" << endl;
589
    indent_up();
590
    indent(out) << "writeFieldBegin oprot (\""<< (*f_iter)->get_name()<<"\","
591
                <<type_to_enum((*f_iter)->get_type())<<","
592
                <<(*f_iter)->get_key()<<")" << endl;
593
 
594
    // Write field contents
595
    out << indent();
596
    generate_serialize_field(out, *f_iter, "_v");
597
    out << endl;
598
    // Write field closer
599
    indent(out) << "writeFieldEnd oprot}" << endl;
600
    indent_down();
601
  }
602
 
603
  // Write the struct map
604
  out <<
605
    indent() << "writeFieldStop oprot" << endl <<
606
    indent() << "writeStructEnd oprot" << endl;
607
 
608
  indent_down();
609
}
610
 
611
/**
612
 * Generates a thrift service.
613
 *
614
 * @param tservice The service definition
615
 */
616
void t_hs_generator::generate_service(t_service* tservice) {
617
  string f_service_name = get_out_dir()+capitalize(service_name_)+".hs";
618
  f_service_.open(f_service_name.c_str());
619
 
620
  f_service_ <<
621
    hs_autogen_comment() << endl <<
622
    "module " << capitalize(service_name_) << " where" << endl <<
623
    hs_imports() << endl;
624
 
625
 
626
  if(tservice->get_extends()){
627
    f_service_ <<
628
      "import qualified " << capitalize(tservice->get_extends()->get_name()) << endl;
629
  }
630
 
631
 
632
  f_service_ <<
633
     "import " << capitalize(program_name_) << "_Types" << endl <<
634
    "import qualified " << capitalize(service_name_) << "_Iface as Iface" << endl;
635
 
636
 
637
  // Generate the three main parts of the service
638
  generate_service_helpers(tservice);
639
  generate_service_interface(tservice);
640
  generate_service_client(tservice);
641
  generate_service_server(tservice);
642
 
643
 
644
  // Close service file
645
  f_service_.close();
646
}
647
 
648
/**
649
 * Generates helper functions for a service.
650
 *
651
 * @param tservice The service to generate a header definition for
652
 */
653
void t_hs_generator::generate_service_helpers(t_service* tservice) {
654
  vector<t_function*> functions = tservice->get_functions();
655
  vector<t_function*>::iterator f_iter;
656
 
657
  indent(f_service_) <<
658
    "-- HELPER FUNCTIONS AND STRUCTURES --" << endl << endl;
659
 
660
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
661
    t_struct* ts = (*f_iter)->get_arglist();
662
    generate_hs_struct_definition(f_service_,ts, false);
663
    generate_hs_function_helpers(*f_iter);
664
  }
665
}
666
 
667
/**
668
 * Generates a struct and helpers for a function.
669
 *
670
 * @param tfunction The function
671
 */
672
void t_hs_generator::generate_hs_function_helpers(t_function* tfunction) {
673
  t_struct result(program_, decapitalize(tfunction->get_name()) + "_result");
674
  t_field success(tfunction->get_returntype(), "success", 0);
675
  if (!tfunction->get_returntype()->is_void()) {
676
    result.append(&success);
677
  }
678
 
679
  t_struct* xs = tfunction->get_xceptions();
680
  const vector<t_field*>& fields = xs->get_members();
681
  vector<t_field*>::const_iterator f_iter;
682
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
683
    result.append(*f_iter);
684
  }
685
  generate_hs_struct_definition(f_service_,&result, false);
686
}
687
 
688
/**
689
 * Generates a service interface definition.
690
 *
691
 * @param tservice The service to generate a header definition for
692
 */
693
void t_hs_generator::generate_service_interface(t_service* tservice) {
694
  string f_iface_name = get_out_dir()+capitalize(service_name_)+"_Iface.hs";
695
  f_iface_.open(f_iface_name.c_str());
696
  indent(f_iface_) << "module " << capitalize(service_name_) << "_Iface where" << endl;
697
 
698
  indent(f_iface_) <<
699
    hs_imports() << endl <<
700
    "import " << capitalize(program_name_) << "_Types" << endl <<
701
    endl;
702
 
703
  if (tservice->get_extends() != NULL) {
704
    string extends = type_name(tservice->get_extends());
705
    indent(f_iface_) << "import " << extends <<"_Iface" << endl;
706
    indent(f_iface_) << "class "<< extends << "_Iface a => " << capitalize(service_name_) << "_Iface a where" << endl;
707
  } else {
708
    f_iface_ << indent() << "class " << capitalize(service_name_) << "_Iface a where" << endl;
709
  }
710
  indent_up();
711
 
712
  vector<t_function*> functions = tservice->get_functions();
713
  vector<t_function*>::iterator f_iter;
714
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
715
    string ft = function_type(*f_iter,true,true,true);
716
    f_iface_ <<
717
      indent() << decapitalize((*f_iter)->get_name()) << " :: a -> " << ft  << endl;
718
  }
719
  indent_down();
720
  f_iface_.close();
721
 
722
}
723
 
724
/**
725
 * Generates a service client definition. Note that in Haskell, the client doesn't implement iface. This is because
726
 * The client does not (and should not have to) deal with arguments being Nothing.
727
 *
728
 * @param tservice The service to generate a server for.
729
 */
730
void t_hs_generator::generate_service_client(t_service* tservice) {
731
  string f_client_name = get_out_dir()+capitalize(service_name_)+"_Client.hs";
732
  f_client_.open(f_client_name.c_str());
733
 
734
  vector<t_function*> functions = tservice->get_functions();
735
  vector<t_function*>::const_iterator f_iter;
736
 
737
  string extends = "";
738
  string exports="";
739
  bool first = true;
740
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
741
    if(first)
742
      first=false;
743
    else
744
      exports+=",";
745
    string funname = (*f_iter)->get_name();
746
    exports+=funname;
747
  }
748
  indent(f_client_) << "module " << capitalize(service_name_) << "_Client("<<exports<<") where" << endl;
749
 
750
  if (tservice->get_extends() != NULL) {
751
    extends = type_name(tservice->get_extends());
752
    indent(f_client_) << "import " << extends << "_Client" << endl;
753
  }
754
  indent(f_client_) << "import Data.IORef" << endl;
755
  indent(f_client_) << hs_imports() << endl;
756
  indent(f_client_) << "import " << capitalize(program_name_) << "_Types" << endl;
757
  indent(f_client_) << "import " << capitalize(service_name_) << endl;
758
  // DATS RITE A GLOBAL VAR
759
  indent(f_client_) << "seqid = newIORef 0" << endl;
760
 
761
 
762
  // Generate client method implementations
763
 
764
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
765
    t_struct* arg_struct = (*f_iter)->get_arglist();
766
    const vector<t_field*>& fields = arg_struct->get_members();
767
    vector<t_field*>::const_iterator fld_iter;
768
    string funname = (*f_iter)->get_name();
769
 
770
    string fargs = "";
771
    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
772
      fargs+= " arg_" + decapitalize((*fld_iter)->get_name());
773
    }
774
 
775
    // Open function
776
    indent(f_client_) << funname << " (ip,op)" <<  fargs << " = do" << endl;
777
    indent_up();
778
    indent(f_client_) <<  "send_" << funname << " op" << fargs;
779
 
780
    f_client_ << endl;
781
 
782
    if (!(*f_iter)->is_oneway()) {
783
      f_client_ << indent();
784
      f_client_ <<
785
        "recv_" << funname << " ip" << endl;
786
    }
787
    indent_down();
788
 
789
    indent(f_client_) <<
790
      "send_" << funname << " op" << fargs << " = do" << endl;
791
    indent_up();
792
    indent(f_client_) << "seq <- seqid" << endl;
793
    indent(f_client_) << "seqn <- readIORef seq" << endl;
794
    std::string argsname = capitalize((*f_iter)->get_name() + "_args");
795
 
796
    // Serialize the request header
797
    f_client_ <<
798
      indent() << "writeMessageBegin op (\"" << (*f_iter)->get_name() << "\", M_CALL, seqn)" << endl;
799
    f_client_ << indent() << "write_" << argsname << " op ("<<argsname<<"{";
800
    bool first = true;
801
    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
802
      if(first)
803
        first=false;
804
      else
805
        f_client_ << ",";
806
      f_client_ << "f_" << argsname <<"_" << (*fld_iter)->get_name() << "=Just arg_" << (*fld_iter)->get_name();
807
    }
808
    f_client_ << "})" << endl;
809
 
810
    // Write to the stream
811
    f_client_ <<
812
      indent() << "writeMessageEnd op" << endl <<
813
      indent() << "tFlush (getTransport op)" << endl;
814
 
815
    indent_down();
816
 
817
    if (!(*f_iter)->is_oneway()) {
818
      std::string resultname = capitalize((*f_iter)->get_name() + "_result");
819
      t_struct noargs(program_);
820
 
821
      std::string funname = string("recv_") + (*f_iter)->get_name();
822
 
823
      t_function recv_function((*f_iter)->get_returntype(),
824
                               funname,
825
                               &noargs);
826
      // Open function
827
      f_client_ <<
828
        indent() << funname << " ip = do" << endl;
829
      indent_up(); // fun
830
 
831
      // TODO(mcslee): Validate message reply here, seq ids etc.
832
 
833
      f_client_ <<
834
        indent() << "(fname, mtype, rseqid) <- readMessageBegin ip" << endl;
835
      f_client_ <<
836
        indent() << "if mtype == M_EXCEPTION then do" << endl <<
837
        indent() << "  x <- readAppExn ip" << endl <<
838
        indent() << "  readMessageEnd ip" << endl;
839
      f_client_ <<
840
        indent() << "  throw x" << endl;
841
      f_client_ <<
842
        indent() << "  else return ()" << endl;
843
 
844
      t_struct* xs = (*f_iter)->get_xceptions();
845
      const std::vector<t_field*>& xceptions = xs->get_members();
846
 
847
      f_client_ <<
848
        indent() << "res <- read_" << resultname << " ip" << endl;
849
      f_client_ <<
850
        indent() << "readMessageEnd ip" << endl;
851
 
852
      // Careful, only return _result if not a void function
853
      if (!(*f_iter)->get_returntype()->is_void()) {
854
        f_client_ <<
855
          indent() << "case f_" << resultname << "_success res of" << endl;
856
        indent_up(); // case
857
        indent(f_client_) << "Just v -> return v" << endl;
858
        indent(f_client_) << "Nothing -> do" << endl;
859
        indent_up(); // none
860
      }
861
 
862
 
863
      vector<t_field*>::const_iterator x_iter;
864
      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
865
        f_client_ <<
866
          indent() << "case f_"<< resultname << "_" << (*x_iter)->get_name() << " res of" << endl;
867
        indent_up(); //case
868
        indent(f_client_) << "Nothing -> return ()" << endl;
869
        indent(f_client_) << "Just _v -> throw _v" << endl;
870
        indent_down(); //-case
871
      }
872
 
873
      // Careful, only return _result if not a void function
874
      if ((*f_iter)->get_returntype()->is_void()) {
875
        indent(f_client_) <<
876
          "return ()" << endl;
877
      } else {
878
        f_client_ <<
879
          indent() << "throw (AppExn AE_MISSING_RESULT \"" << (*f_iter)->get_name() << " failed: unknown result\")" << endl;
880
        indent_down(); //-none
881
        indent_down(); //-case
882
      }
883
 
884
      // Close function
885
      indent_down(); //-fun
886
    }
887
  }
888
  f_client_.close();
889
 
890
 
891
}
892
 
893
/**
894
 * Generates a service server definition.
895
 *
896
 * @param tservice The service to generate a server for.
897
 */
898
void t_hs_generator::generate_service_server(t_service* tservice) {
899
  // Generate the dispatch methods
900
  vector<t_function*> functions = tservice->get_functions();
901
  vector<t_function*>::iterator f_iter;
902
 
903
  // Generate the process subfunctions
904
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
905
    generate_process_function(tservice, *f_iter);
906
  }
907
 
908
 
909
  indent(f_service_) << "proc handler (iprot,oprot) (name,typ,seqid) = case name of" << endl;
910
  indent_up();
911
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
912
    string fname = (*f_iter)->get_name();
913
    indent(f_service_) << "\""<<fname<<"\" -> process_" << decapitalize(fname) << " (seqid,iprot,oprot,handler)" << endl;
914
  }
915
  indent(f_service_) << "_ -> ";
916
  if(tservice->get_extends() != NULL){
917
    f_service_ << type_name(tservice->get_extends()) << ".proc handler (iprot,oprot) (name,typ,seqid)" << endl;
918
  } else {
919
    f_service_ << "do" << endl;
920
    indent_up();
921
    indent(f_service_) << "skip iprot T_STRUCT" << endl;
922
    indent(f_service_) << "readMessageEnd iprot" << endl;
923
    indent(f_service_) << "writeMessageBegin oprot (name,M_EXCEPTION,seqid)" << endl;
924
    indent(f_service_) << "writeAppExn oprot (AppExn AE_UNKNOWN_METHOD (\"Unknown function \" ++ name))" << endl;
925
    indent(f_service_) << "writeMessageEnd oprot" << endl;
926
    indent(f_service_) << "tFlush (getTransport oprot)" << endl;
927
    indent_down();
928
  }
929
  indent_down();
930
 
931
  // Generate the server implementation
932
  indent(f_service_) <<
933
    "process handler (iprot, oprot) = do" << endl;
934
  indent_up();
935
 
936
  f_service_ <<
937
    indent() << "(name, typ, seqid) <- readMessageBegin iprot" << endl;
938
  f_service_ << indent() << "proc handler (iprot,oprot) (name,typ,seqid)" << endl;
939
  indent(f_service_) << "return True" << endl;
940
  indent_down();
941
 
942
}
943
 
944
/**
945
 * Generates a process function definition.
946
 *
947
 * @param tfunction The function to write a dispatcher for
948
 */
949
void t_hs_generator::generate_process_function(t_service* tservice,
950
                                               t_function* tfunction) {
951
  // Open function
952
  indent(f_service_) <<
953
    "process_" << tfunction->get_name() << " (seqid, iprot, oprot, handler) = do" << endl;
954
  indent_up();
955
 
956
  string argsname = capitalize(tfunction->get_name()) + "_args";
957
  string resultname = capitalize(tfunction->get_name()) + "_result";
958
 
959
  // Generate the function call
960
  t_struct* arg_struct = tfunction->get_arglist();
961
  const std::vector<t_field*>& fields = arg_struct->get_members();
962
  vector<t_field*>::const_iterator f_iter;
963
 
964
 
965
  f_service_ <<
966
    indent() << "args <- read_" << argsname << " iprot" << endl;
967
  f_service_ <<
968
    indent() << "readMessageEnd iprot" << endl;
969
 
970
  t_struct* xs = tfunction->get_xceptions();
971
  const std::vector<t_field*>& xceptions = xs->get_members();
972
  vector<t_field*>::const_iterator x_iter;
973
  int n = xceptions.size();
974
  if (!tfunction->is_oneway()){
975
    if(!tfunction->get_returntype()->is_void()){
976
      n++;
977
    }
978
    indent(f_service_) << "rs <- return (" << resultname;
979
 
980
    for(int i=0; i<n;i++){
981
      f_service_ << " Nothing";
982
    }
983
    f_service_ << ")" << endl;
984
  }
985
 
986
  indent(f_service_) << "res <- ";
987
  // Try block for a function with exceptions
988
  if (xceptions.size() > 0) {
989
    for(unsigned int i=0;i<xceptions.size();i++){
990
      f_service_ << "(Control.Exception.catch" << endl;
991
      indent_up();
992
      f_service_ << indent();
993
    }
994
  }
995
 
996
  f_service_ << "(do" << endl;
997
  indent_up();
998
  f_service_ << indent();
999
  if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()){
1000
    f_service_ << "res <- ";
1001
  }
1002
  f_service_ << "Iface." << tfunction->get_name() << " handler";
1003
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1004
    f_service_ <<  " (f_" << argsname <<  "_" << (*f_iter)->get_name() << " args)";
1005
  }
1006
 
1007
 
1008
  if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()){
1009
    f_service_ << endl;
1010
    indent(f_service_) << "return rs{f_"<<resultname<<"_success= Just res}";
1011
  } else if (!tfunction->is_oneway()){
1012
    f_service_ << endl;
1013
    indent(f_service_) << "return rs";
1014
  }
1015
  f_service_ << ")" << endl;
1016
  indent_down();
1017
 
1018
  if (xceptions.size() > 0 && !tfunction->is_oneway()) {
1019
    for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1020
      indent(f_service_) << "(\\e  -> " <<endl;
1021
      indent_up();
1022
      if(!tfunction->is_oneway()){
1023
        f_service_ <<
1024
          indent() << "return rs{f_"<<resultname<<"_" << (*x_iter)->get_name() << " =Just e}";
1025
      } else {
1026
        indent(f_service_) << "return ()";
1027
      }
1028
      f_service_ << "))" << endl;
1029
      indent_down();
1030
      indent_down();
1031
    }
1032
  }
1033
 
1034
 
1035
 
1036
  // Shortcut out here for oneway functions
1037
  if (tfunction->is_oneway()) {
1038
    f_service_ <<
1039
      indent() << "return ()" << endl;
1040
    indent_down();
1041
    return;
1042
  }
1043
 
1044
  f_service_ <<
1045
    indent() << "writeMessageBegin oprot (\"" << tfunction->get_name() << "\", M_REPLY, seqid);" << endl <<
1046
    indent() << "write_"<<resultname<<" oprot res" << endl <<
1047
    indent() << "writeMessageEnd oprot" << endl <<
1048
    indent() << "tFlush (getTransport oprot)" << endl;
1049
 
1050
  // Close function
1051
  indent_down();
1052
}
1053
 
1054
/**
1055
 * Deserializes a field of any type.
1056
 */
1057
void t_hs_generator::generate_deserialize_field(ofstream &out,
1058
                                                   t_field* tfield,
1059
                                                   string prefix){
1060
  t_type* type = tfield->get_type();
1061
  generate_deserialize_type(out,type);
1062
}
1063
 
1064
 
1065
/**
1066
 * Deserializes a field of any type.
1067
 */
1068
void t_hs_generator::generate_deserialize_type(ofstream &out,
1069
                                                   t_type* type){
1070
  type = get_true_type(type);
1071
 
1072
  if (type->is_void()) {
1073
    throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE";
1074
  }
1075
 
1076
 
1077
  if (type->is_struct() || type->is_xception()) {
1078
    generate_deserialize_struct(out,
1079
                                (t_struct*)type);
1080
  } else if (type->is_container()) {
1081
    generate_deserialize_container(out, type);
1082
  } else if (type->is_base_type()) {
1083
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1084
    switch (tbase) {
1085
    case t_base_type::TYPE_VOID:
1086
      throw "compiler error: cannot serialize void field in a struct";
1087
      break;
1088
    case t_base_type::TYPE_STRING:
1089
      out << "readString";
1090
      break;
1091
    case t_base_type::TYPE_BOOL:
1092
      out << "readBool";
1093
      break;
1094
    case t_base_type::TYPE_BYTE:
1095
      out << "readByte";
1096
      break;
1097
    case t_base_type::TYPE_I16:
1098
      out << "readI16";
1099
      break;
1100
    case t_base_type::TYPE_I32:
1101
      out << "readI32";
1102
      break;
1103
    case t_base_type::TYPE_I64:
1104
      out << "readI64";
1105
      break;
1106
    case t_base_type::TYPE_DOUBLE:
1107
      out << "readDouble";
1108
      break;
1109
    default:
1110
      throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
1111
    }
1112
    out << " iprot";
1113
  } else if (type->is_enum()) {
1114
    string ename = capitalize(type->get_name());
1115
    out << "(do {i <- readI32 iprot; return (toEnum i :: " << ename << ")})";
1116
  } else {
1117
    printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n",
1118
           type->get_name().c_str());
1119
  }
1120
}
1121
 
1122
 
1123
/**
1124
 * Generates an unserializer for a struct, calling read()
1125
 */
1126
void t_hs_generator::generate_deserialize_struct(ofstream &out,
1127
                                                  t_struct* tstruct) {
1128
  string name = capitalize(tstruct->get_name());
1129
  out << "(read_" << name << " iprot)";
1130
 
1131
}
1132
 
1133
/**
1134
 * Serialize a container by writing out the header followed by
1135
 * data and then a footer.
1136
 */
1137
void t_hs_generator::generate_deserialize_container(ofstream &out,
1138
                                                    t_type* ttype) {
1139
  string size = tmp("_size");
1140
  string ktype = tmp("_ktype");
1141
  string vtype = tmp("_vtype");
1142
  string etype = tmp("_etype");
1143
  string con = tmp("_con");
1144
 
1145
  t_field fsize(g_type_i32, size);
1146
  t_field fktype(g_type_byte, ktype);
1147
  t_field fvtype(g_type_byte, vtype);
1148
  t_field fetype(g_type_byte, etype);
1149
 
1150
  // Declare variables, read header
1151
  if (ttype->is_map()) {
1152
    out << "(let {f 0 = return []; f n = do {k <- ";
1153
    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
1154
    out << "; v <- ";
1155
    generate_deserialize_type(out,((t_map*)ttype)->get_val_type());
1156
    out << ";r <- f (n-1); return $ (k,v):r}} in do {("<<ktype<<","<<vtype<<","<<size<<") <- readMapBegin iprot; l <- f " << size << "; return $ Map.fromList l})";
1157
  } else if (ttype->is_set()) {
1158
    out << "(let {f 0 = return []; f n = do {v <- ";
1159
    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
1160
    out << ";r <- f (n-1); return $ v:r}} in do {("<<etype<<","<<size<<") <- readSetBegin iprot; l <- f " << size << "; return $ Set.fromList l})";
1161
  } else if (ttype->is_list()) {
1162
    out << "(let {f 0 = return []; f n = do {v <- ";
1163
    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
1164
    out << ";r <- f (n-1); return $ v:r}} in do {("<<etype<<","<<size<<") <- readListBegin iprot; f " << size << "})";
1165
  }
1166
}
1167
 
1168
 
1169
/**
1170
 * Serializes a field of any type.
1171
 *
1172
 * @param tfield The field to serialize
1173
 * @param prefix Name to prepend to field name
1174
 */
1175
void t_hs_generator::generate_serialize_field(ofstream &out,
1176
                                                 t_field* tfield,
1177
                                                 string name) {
1178
  t_type* type = get_true_type(tfield->get_type());
1179
 
1180
  // Do nothing for void types
1181
  if (type->is_void()) {
1182
    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
1183
      tfield->get_name();
1184
  }
1185
 
1186
  if(name.length() == 0){
1187
    name = decapitalize(tfield->get_name());
1188
  }
1189
 
1190
  if (type->is_struct() || type->is_xception()) {
1191
    generate_serialize_struct(out,
1192
                              (t_struct*)type,
1193
                              name);
1194
  } else if (type->is_container()) {
1195
    generate_serialize_container(out,
1196
                                 type,
1197
                                 name);
1198
  } else if (type->is_base_type() || type->is_enum()) {
1199
    if (type->is_base_type()) {
1200
      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1201
      switch (tbase) {
1202
      case t_base_type::TYPE_VOID:
1203
        throw
1204
          "compiler error: cannot serialize void field in a struct: " + name;
1205
        break;
1206
      case t_base_type::TYPE_STRING:
1207
        out << "writeString oprot " << name;
1208
        break;
1209
      case t_base_type::TYPE_BOOL:
1210
        out << "writeBool oprot " << name;
1211
       break;
1212
      case t_base_type::TYPE_BYTE:
1213
        out << "writeByte oprot " << name;
1214
        break;
1215
      case t_base_type::TYPE_I16:
1216
        out << "writeI16 oprot " << name;
1217
        break;
1218
      case t_base_type::TYPE_I32:
1219
        out << "writeI32 oprot " << name;
1220
        break;
1221
      case t_base_type::TYPE_I64:
1222
        out << "writeI64 oprot " << name;
1223
        break;
1224
      case t_base_type::TYPE_DOUBLE:
1225
        out << "writeDouble oprot " << name;
1226
        break;
1227
      default:
1228
        throw "compiler error: no hs name for base type " + t_base_type::t_base_name(tbase);
1229
      }
1230
 
1231
    } else if (type->is_enum()) {
1232
      string ename = capitalize(type->get_name());
1233
      out << "writeI32 oprot (fromEnum "<< name << ")";
1234
    }
1235
 
1236
  } else {
1237
    printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
1238
           tfield->get_name().c_str(),
1239
           type->get_name().c_str());
1240
  }
1241
}
1242
 
1243
/**
1244
 * Serializes all the members of a struct.
1245
 *
1246
 * @param tstruct The struct to serialize
1247
 * @param prefix  String prefix to attach to all fields
1248
 */
1249
void t_hs_generator::generate_serialize_struct(ofstream &out,
1250
                                               t_struct* tstruct,
1251
                                               string prefix) {
1252
  out << "write_" << type_name(tstruct) << " oprot " << prefix;
1253
}
1254
 
1255
void t_hs_generator::generate_serialize_container(ofstream &out,
1256
                                                  t_type* ttype,
1257
                                                  string prefix) {
1258
  if (ttype->is_map()) {
1259
    string k = tmp("_kiter");
1260
    string v = tmp("_viter");
1261
    out << "(let {f [] = return (); f (("<<k<<","<<v<<"):t) = do {";
1262
    generate_serialize_map_element(out, (t_map*)ttype, k, v);
1263
    out << ";f t}} in do {writeMapBegin oprot ("<< type_to_enum(((t_map*)ttype)->get_key_type())<<","<< type_to_enum(((t_map*)ttype)->get_val_type())<<",Map.size " << prefix << "); f (Map.toList " << prefix << ");writeMapEnd oprot})";
1264
  } else if (ttype->is_set()) {
1265
    string v = tmp("_viter");
1266
    out << "(let {f [] = return (); f ("<<v<<":t) = do {";
1267
    generate_serialize_set_element(out, (t_set*)ttype, v);
1268
    out << ";f t}} in do {writeSetBegin oprot ("<< type_to_enum(((t_set*)ttype)->get_elem_type())<<",Set.size " << prefix << "); f (Set.toList " << prefix << ");writeSetEnd oprot})";
1269
  } else if (ttype->is_list()) {
1270
    string v = tmp("_viter");
1271
    out << "(let {f [] = return (); f ("<<v<<":t) = do {";
1272
    generate_serialize_list_element(out, (t_list*)ttype, v);
1273
    out << ";f t}} in do {writeListBegin oprot ("<< type_to_enum(((t_list*)ttype)->get_elem_type())<<",length " << prefix << "); f " << prefix << ";writeListEnd oprot})";
1274
  }
1275
 
1276
}
1277
 
1278
/**
1279
 * Serializes the members of a map.
1280
 *
1281
 */
1282
void t_hs_generator::generate_serialize_map_element(ofstream &out,
1283
                                                     t_map* tmap,
1284
                                                     string kiter,
1285
                                                     string viter) {
1286
  t_field kfield(tmap->get_key_type(), kiter);
1287
  out << "do {";
1288
  generate_serialize_field(out, &kfield);
1289
  out << ";";
1290
  t_field vfield(tmap->get_val_type(), viter);
1291
  generate_serialize_field(out, &vfield);
1292
  out << "}";
1293
}
1294
 
1295
/**
1296
 * Serializes the members of a set.
1297
 */
1298
void t_hs_generator::generate_serialize_set_element(ofstream &out,
1299
                                                     t_set* tset,
1300
                                                     string iter) {
1301
  t_field efield(tset->get_elem_type(), iter);
1302
  generate_serialize_field(out, &efield);
1303
}
1304
 
1305
/**
1306
 * Serializes the members of a list.
1307
 */
1308
void t_hs_generator::generate_serialize_list_element(ofstream &out,
1309
                                                      t_list* tlist,
1310
                                                      string iter) {
1311
  t_field efield(tlist->get_elem_type(), iter);
1312
  generate_serialize_field(out, &efield);
1313
}
1314
 
1315
 
1316
string t_hs_generator::function_type(t_function* tfunc, bool options, bool io, bool method){
1317
  string result="";
1318
 
1319
  const vector<t_field*>& fields = tfunc->get_arglist()->get_members();
1320
  vector<t_field*>::const_iterator f_iter;
1321
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1322
    if(options) result += "Maybe ";
1323
    result += render_hs_type((*f_iter)->get_type(), options);
1324
    result += " -> ";
1325
  }
1326
  if(fields.empty() && !method){
1327
    result += "() -> ";
1328
  }
1329
  if(io) result += "IO ";
1330
  result += render_hs_type(tfunc->get_returntype(), io);
1331
  return result;
1332
}
1333
 
1334
 
1335
string t_hs_generator::type_name(t_type* ttype) {
1336
  string prefix = "";
1337
  t_program* program = ttype->get_program();
1338
  if (program != NULL && program != program_) {
1339
    if (!ttype->is_service()) {
1340
      prefix = capitalize(program->get_name()) + "_Types.";
1341
    }
1342
  }
1343
 
1344
  string name = ttype->get_name();
1345
  if(ttype->is_service()){
1346
    name = capitalize(name);
1347
  } else {
1348
    name = capitalize(name);
1349
  }
1350
  return prefix + name;
1351
}
1352
 
1353
/**
1354
 * Converts the parse type to a Protocol.t_type enum
1355
 */
1356
string t_hs_generator::type_to_enum(t_type* type) {
1357
  type = get_true_type(type);
1358
 
1359
  if (type->is_base_type()) {
1360
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1361
    switch (tbase) {
1362
    case t_base_type::TYPE_VOID:
1363
      return "T_VOID";
1364
    case t_base_type::TYPE_STRING:
1365
      return "T_STRING";
1366
    case t_base_type::TYPE_BOOL:
1367
      return "T_BOOL";
1368
    case t_base_type::TYPE_BYTE:
1369
      return "T_BYTE";
1370
    case t_base_type::TYPE_I16:
1371
      return "T_I16";
1372
    case t_base_type::TYPE_I32:
1373
      return "T_I32";
1374
    case t_base_type::TYPE_I64:
1375
      return "T_I64";
1376
    case t_base_type::TYPE_DOUBLE:
1377
      return "T_DOUBLE";
1378
    }
1379
  } else if (type->is_enum()) {
1380
    return "T_I32";
1381
  } else if (type->is_struct() || type->is_xception()) {
1382
    return "T_STRUCT";
1383
  } else if (type->is_map()) {
1384
    return "T_MAP";
1385
  } else if (type->is_set()) {
1386
    return "T_SET";
1387
  } else if (type->is_list()) {
1388
    return "T_LIST";
1389
  }
1390
 
1391
  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
1392
}
1393
 
1394
/**
1395
 * Converts the parse type to an haskell type
1396
 */
1397
string t_hs_generator::render_hs_type(t_type* type, bool needs_parens) {
1398
  type = get_true_type(type);
1399
  string type_repr;
1400
 
1401
  if (type->is_base_type()) {
1402
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1403
    switch (tbase) {
1404
    case t_base_type::TYPE_VOID:
1405
      return "()";
1406
    case t_base_type::TYPE_STRING:
1407
      return "String";
1408
    case t_base_type::TYPE_BOOL:
1409
      return "Bool";
1410
    case t_base_type::TYPE_BYTE:
1411
      return "Int";
1412
    case t_base_type::TYPE_I16:
1413
      return "Int";
1414
    case t_base_type::TYPE_I32:
1415
      return "Int";
1416
    case t_base_type::TYPE_I64:
1417
      return "Int64";
1418
    case t_base_type::TYPE_DOUBLE:
1419
      return "Double";
1420
    }
1421
  } else if (type->is_enum()) {
1422
    return capitalize(((t_enum*)type)->get_name());
1423
  } else if (type->is_struct() || type->is_xception()) {
1424
    return type_name((t_struct*)type);
1425
  } else if (type->is_map()) {
1426
    t_type* ktype = ((t_map*)type)->get_key_type();
1427
    t_type* vtype = ((t_map*)type)->get_val_type();
1428
 
1429
    type_repr = "Map.Map " + render_hs_type(ktype, true) + " " + render_hs_type(vtype, true);
1430
  } else if (type->is_set()) {
1431
    t_type* etype = ((t_set*)type)->get_elem_type();
1432
 
1433
    type_repr = "Set.Set " + render_hs_type(etype, true) ;
1434
  } else if (type->is_list()) {
1435
    t_type* etype = ((t_list*)type)->get_elem_type();
1436
    return "[" + render_hs_type(etype, false) + "]";
1437
  } else {
1438
    throw "INVALID TYPE IN type_to_enum: " + type->get_name();
1439
  }
1440
 
1441
  return needs_parens ? "(" + type_repr + ")" : type_repr;
1442
}
1443
 
1444
 
1445
THRIFT_REGISTER_GENERATOR(hs, "Haskell", "");