summaryrefslogtreecommitdiff
path: root/compiler/cpp/src/thrift/generate/t_st_generator.cc
blob: 5edb8528197726146e69f71542fb29f79532e51d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
/*
 * 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.
 *
 * Contains some contributions under the Thrift Software License.
 * Please see doc/old-thrift-license.txt in the Thrift distribution for
 * details.
 */

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

#include <stdlib.h>
#include <time.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::map;
using std::ofstream;
using std::ostringstream;
using std::string;
using std::stringstream;
using std::vector;

static const string endl = "\n"; // avoid ostream << std::endl flushes

/**
 * Smalltalk code generator.
 *
 */
class t_st_generator : public t_oop_generator {
public:
  t_st_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;
    temporary_var = 0;
    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 st:" + iter->first;
    }

    out_dir_base_ = "gen-st";
  }

  /**
   * Init and close methods
   */

  void init_generator() override;
  void close_generator() override;

  /**
   * Program-level generation functions
   */

  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;
  void generate_class_side_definition();
  void generate_force_consts();

  std::string render_const_value(t_type* type, t_const_value* value);

  /**
   * Struct generation code
   */

  void generate_st_struct(std::ostream& out, t_struct* tstruct, bool is_exception);
  void generate_accessors(std::ostream& out, t_struct* tstruct);

  /**
   * Service-level generation functions
   */

  void generate_service_client(t_service* tservice);

  void generate_send_method(t_function* tfunction);
  void generate_recv_method(t_function* tfunction);

  std::string map_reader(t_map* tmap);
  std::string list_reader(t_list* tlist);
  std::string set_reader(t_set* tset);
  std::string struct_reader(t_struct* tstruct, std::string clsName);

  std::string map_writer(t_map* tmap, std::string name);
  std::string list_writer(t_list* tlist, std::string name);
  std::string set_writer(t_set* tset, std::string name);
  std::string struct_writer(t_struct* tstruct, std::string fname);

  std::string write_val(t_type* t, std::string fname);
  std::string read_val(t_type* t);

  /**
   * Helper rendering functions
   */

  std::string st_autogen_comment();

  void st_class_def(std::ostream& out, std::string name);
  void st_method(std::ostream& out, std::string cls, std::string name);
  void st_method(std::ostream& out, std::string cls, std::string name, std::string category);
  void st_close_method(std::ostream& out);
  void st_class_method(std::ostream& out, std::string cls, std::string name);
  void st_class_method(std::ostream& out, std::string cls, std::string name, std::string category);
  void st_setter(std::ostream& out, std::string cls, std::string name, std::string type);
  void st_getter(std::ostream& out, std::string cls, std::string name);
  void st_accessors(std::ostream& out, std::string cls, std::string name, std::string type);

  std::string class_name();
  static bool is_valid_namespace(const std::string& sub_namespace);
  std::string client_class_name();
  std::string prefix(std::string name);
  std::string declare_field(t_field* tfield);
  std::string type_name(t_type* ttype);

  std::string function_signature(t_function* tfunction);
  std::string argument_list(t_struct* tstruct);
  std::string function_types_comment(t_function* fn);

  std::string type_to_enum(t_type* ttype);
  std::string a_type(t_type* type);
  bool is_vowel(char c);
  std::string temp_name();
  std::string generated_category();

private:
  /**
   * File streams
   */
  int temporary_var;
  ofstream_with_content_based_conditional_update f_;
};

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

  temporary_var = 0;

  // Make output file
  string f_name = get_out_dir() + "/" + program_name_ + ".st";
  f_.open(f_name.c_str());

  // Print header
  f_ << st_autogen_comment() << endl;

  st_class_def(f_, program_name_);
  generate_class_side_definition();

  // 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);
  }
}

string t_st_generator::class_name() {
  return capitalize(program_name_);
}

bool t_st_generator::is_valid_namespace(const std::string& sub_namespace) {
  return sub_namespace == "prefix" || sub_namespace == "category";
}

string t_st_generator::prefix(string class_name) {
  string prefix = program_->get_namespace("smalltalk.prefix");
  string name = capitalize(class_name);
  name = prefix.empty() ? name : (prefix + name);
  return name;
}

string t_st_generator::client_class_name() {
  return capitalize(service_name_) + "Client";
}

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

void t_st_generator::generate_force_consts() {
  f_ << prefix(class_name()) << " enums keysAndValuesDo: [:k :v | " << prefix(class_name())
     << " enums at: k put: v value].!" << endl;

  f_ << prefix(class_name()) << " constants keysAndValuesDo: [:k :v | " << prefix(class_name())
     << " constants at: k put: v value].!" << endl;
}

void t_st_generator::close_generator() {
  generate_force_consts();
  f_.close();
}

string t_st_generator::generated_category() {
  string cat = program_->get_namespace("smalltalk.category");
  // For compatibility with the Thrift grammar, the category must
  // be punctuated by dots.  Replaces them with dashes here.
  for (char & iter : cat) {
    if (iter == '.') {
      iter = '-';
    }
  }
  return cat.size() ? cat : "Generated-" + class_name();
}

/**
 * Generates a typedef. This is not done in Smalltalk, types are all implicit.
 *
 * @param ttypedef The type definition
 */
void t_st_generator::generate_typedef(t_typedef* ttypedef) {
  (void)ttypedef;
}

void t_st_generator::st_class_def(std::ostream& out, string name) {
  out << "Object subclass: #" << prefix(name) << endl;
  indent_up();
  out << indent() << "instanceVariableNames: ''" << endl << indent() << "classVariableNames: ''"
      << endl << indent() << "poolDictionaries: ''" << endl << indent() << "category: '"
      << generated_category() << "'!" << endl << endl;
}

void t_st_generator::st_method(std::ostream& out, string cls, string name) {
  st_method(out, cls, name, "as yet uncategorized");
}

void t_st_generator::st_class_method(std::ostream& out, string cls, string name) {
  st_method(out, cls + " class", name);
}

void t_st_generator::st_class_method(std::ostream& out, string cls, string name, string category) {
  st_method(out, cls, name, category);
}

void t_st_generator::st_method(std::ostream& out, string cls, string name, string category) {
  char timestr[50];
  time_t rawtime;
  struct tm* tinfo;

  time(&rawtime);
  tinfo = localtime(&rawtime);
  strftime(timestr, 50, "%m/%d/%Y %H:%M", tinfo);

  out << "!" << prefix(cls) << " methodsFor: '" + category + "' stamp: 'thrift " << timestr
      << "'!\n" << name << endl;

  indent_up();
  out << indent();
}

void t_st_generator::st_close_method(std::ostream& out) {
  out << "! !" << endl << endl;
  indent_down();
}

void t_st_generator::st_setter(std::ostream& out,
                               string cls,
                               string name,
                               string type = "anObject") {
  st_method(out, cls, name + ": " + type);
  out << name << " := " + type;
  st_close_method(out);
}

void t_st_generator::st_getter(std::ostream& out, string cls, string name) {
  st_method(out, cls, name + "");
  out << "^ " << name;
  st_close_method(out);
}

void t_st_generator::st_accessors(std::ostream& out,
                                  string cls,
                                  string name,
                                  string type = "anObject") {
  st_setter(out, cls, name, type);
  st_getter(out, cls, name);
}

void t_st_generator::generate_class_side_definition() {
  f_ << prefix(class_name()) << " class" << endl << "\tinstanceVariableNames: 'constants enums'!"
     << endl << endl;

  st_accessors(f_, class_name() + " class", "enums");
  st_accessors(f_, class_name() + " class", "constants");

  f_ << prefix(class_name()) << " enums: Dictionary new!" << endl;
  f_ << prefix(class_name()) << " constants: Dictionary new!" << endl;

  f_ << endl;
}

/**
 * Generates code for an enumerated type. Done using a class to scope
 * the values.
 *
 * @param tenum The enumeration
 */
void t_st_generator::generate_enum(t_enum* tenum) {
  string cls_name = program_name_ + capitalize(tenum->get_name());

  f_ << prefix(class_name()) << " enums at: '" << tenum->get_name() << "' put: ["
     << "(Dictionary new " << endl;

  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 value = (*c_iter)->get_value();
    f_ << "\tat: '" << (*c_iter)->get_name() << "' put: " << value << ";" << endl;
  }

  f_ << "\tyourself)]!" << endl << endl;
}

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

  f_ << prefix(class_name()) << " constants at: '" << name << "' put: ["
     << render_const_value(type, value) << "]!" << endl << endl;
}

/**
 * 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_st_generator::render_const_value(t_type* type, t_const_value* value) {
  type = get_true_type(type);
  std::ostringstream out;
  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:
    case t_base_type::TYPE_I32:
    case t_base_type::TYPE_I64:
      out << value->get_integer();
      break;
    case t_base_type::TYPE_DOUBLE:
      if (value->get_type() == t_const_value::CV_INTEGER) {
        out << value->get_integer();
      } 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()) {
    indent(out) << value->get_integer();
  } else if (type->is_struct() || type->is_xception()) {
    out << "(" << capitalize(type->get_name()) << " new " << endl;
    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();
      }

      out << indent() << v_iter->first->get_string() << ": "
          << render_const_value(field_type, v_iter->second) << ";" << endl;
    }
    out << indent() << "yourself)";

    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();
    out << "(Dictionary new" << endl;
    indent_up();
    indent_up();
    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) {
      out << indent() << indent();
      out << "at: " << render_const_value(ktype, v_iter->first);
      out << " put: ";
      out << render_const_value(vtype, v_iter->second);
      out << ";" << endl;
    }
    out << indent() << indent() << "yourself)";
    indent_down();
    indent_down();
  } else if (type->is_list() || type->is_set()) {
    t_type* etype;
    if (type->is_list()) {
      etype = ((t_list*)type)->get_elem_type();
    } else {
      etype = ((t_set*)type)->get_elem_type();
    }
    if (type->is_set()) {
      out << "(Set new" << endl;
    } else {
      out << "(OrderedCollection new" << endl;
    }
    indent_up();
    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() << indent();
      out << "add: " << render_const_value(etype, *v_iter);
      out << ";" << endl;
    }
    out << indent() << indent() << "yourself)";
    indent_down();
    indent_down();
  } else {
    throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
  }
  return out.str();
}

/**
 * Generates a Smalltalk struct
 */
void t_st_generator::generate_struct(t_struct* tstruct) {
  generate_st_struct(f_, tstruct, false);
}

/**
 * Generates a struct definition for a thrift exception. Basically the same
 * as a struct but extends the Exception class.
 *
 * @param txception The struct definition
 */
void t_st_generator::generate_xception(t_struct* txception) {
  generate_st_struct(f_, txception, true);
}

/**
 * Generates a smalltalk class to represent a struct
 */
void t_st_generator::generate_st_struct(std::ostream& out,
                                        t_struct* tstruct,
                                        bool is_exception = false) {
  const vector<t_field*>& members = tstruct->get_members();
  vector<t_field*>::const_iterator m_iter;

  if (is_exception)
    out << "Error";
  else
    out << "Object";

  out << " subclass: #" << prefix(type_name(tstruct)) << endl << "\tinstanceVariableNames: '";

  if (members.size() > 0) {
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
      if (m_iter != members.begin())
        out << " ";
      out << camelcase((*m_iter)->get_name());
    }
  }

  out << "'\n"
      << "\tclassVariableNames: ''\n"
      << "\tpoolDictionaries: ''\n"
      << "\tcategory: '" << generated_category() << "'!\n\n";

  generate_accessors(out, tstruct);
}

bool t_st_generator::is_vowel(char c) {
  switch (tolower(c)) {
  case 'a':
  case 'e':
  case 'i':
  case 'o':
  case 'u':
    return true;
  }
  return false;
}

string t_st_generator::a_type(t_type* type) {
  string prefix;

  if (is_vowel(type_name(type)[0]))
    prefix = "an";
  else
    prefix = "a";

  return prefix + capitalize(type_name(type));
}

void t_st_generator::generate_accessors(std::ostream& out, t_struct* tstruct) {
  const vector<t_field*>& members = tstruct->get_members();
  vector<t_field*>::const_iterator m_iter;
  string type;
  string prefix;

  if (members.size() > 0) {
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
      st_accessors(out,
                   capitalize(type_name(tstruct)),
                   camelcase((*m_iter)->get_name()),
                   a_type((*m_iter)->get_type()));
    }
    out << endl;
  }
}

/**
 * Generates a thrift service.
 *
 * @param tservice The service definition
 */
void t_st_generator::generate_service(t_service* tservice) {
  generate_service_client(tservice);
  // generate_service_server(tservice);
}

string t_st_generator::temp_name() {
  std::ostringstream out;
  out << "temp" << temporary_var++;
  return out.str();
}

string t_st_generator::map_writer(t_map* tmap, string fname) {
  std::ostringstream out;
  string key = temp_name();
  string val = temp_name();

  out << "[oprot writeMapBegin: (TMap new keyType: " << type_to_enum(tmap->get_key_type())
      << "; valueType: " << type_to_enum(tmap->get_val_type()) << "; size: " << fname << " size)."
      << endl;
  indent_up();

  out << indent() << fname << " keysAndValuesDo: [:" << key << " :" << val << " |" << endl;
  indent_up();

  out << indent() << write_val(tmap->get_key_type(), key) << "." << endl << indent()
      << write_val(tmap->get_val_type(), val);
  indent_down();

  out << "]." << endl << indent() << "oprot writeMapEnd] value";
  indent_down();

  return out.str();
}

string t_st_generator::map_reader(t_map* tmap) {
  std::ostringstream out;
  string desc = temp_name();
  string val = temp_name();

  out << "[|" << desc << " " << val << "| " << endl;
  indent_up();

  out << indent() << desc << " := iprot readMapBegin." << endl << indent() << val
      << " := Dictionary new." << endl << indent() << desc << " size timesRepeat: [" << endl;

  indent_up();
  out << indent() << val << " at: " << read_val(tmap->get_key_type())
      << " put: " << read_val(tmap->get_val_type());
  indent_down();

  out << "]." << endl << indent() << "iprot readMapEnd." << endl << indent() << val << "] value";
  indent_down();

  return out.str();
}

string t_st_generator::list_writer(t_list* tlist, string fname) {
  std::ostringstream out;
  string val = temp_name();

  out << "[oprot writeListBegin: (TList new elemType: " << type_to_enum(tlist->get_elem_type())
      << "; size: " << fname << " size)." << endl;
  indent_up();

  out << indent() << fname << " do: [:" << val << "|" << endl;
  indent_up();

  out << indent() << write_val(tlist->get_elem_type(), val) << endl;
  indent_down();

  out << "]." << endl << indent() << "oprot writeListEnd] value";
  indent_down();

  return out.str();
}

string t_st_generator::list_reader(t_list* tlist) {
  std::ostringstream out;
  string desc = temp_name();
  string val = temp_name();

  out << "[|" << desc << " " << val << "| " << desc << " := iprot readListBegin." << endl;
  indent_up();

  out << indent() << val << " := OrderedCollection new." << endl << indent() << desc
      << " size timesRepeat: [" << endl;

  indent_up();
  out << indent() << val << " add: " << read_val(tlist->get_elem_type());
  indent_down();

  out << "]." << endl << indent() << "iprot readListEnd." << endl << indent() << val << "] value";
  indent_down();

  return out.str();
}

string t_st_generator::set_writer(t_set* tset, string fname) {
  std::ostringstream out;
  string val = temp_name();

  out << "[oprot writeSetBegin: (TSet new elemType: " << type_to_enum(tset->get_elem_type())
      << "; size: " << fname << " size)." << endl;
  indent_up();

  out << indent() << fname << " do: [:" << val << "|" << endl;
  indent_up();

  out << indent() << write_val(tset->get_elem_type(), val) << endl;
  indent_down();

  out << "]." << endl << indent() << "oprot writeSetEnd] value";
  indent_down();

  return out.str();
}

string t_st_generator::set_reader(t_set* tset) {
  std::ostringstream out;
  string desc = temp_name();
  string val = temp_name();

  out << "[|" << desc << " " << val << "| " << desc << " := iprot readSetBegin." << endl;
  indent_up();

  out << indent() << val << " := Set new." << endl << indent() << desc << " size timesRepeat: ["
      << endl;

  indent_up();
  out << indent() << val << " add: " << read_val(tset->get_elem_type());
  indent_down();

  out << "]." << endl << indent() << "iprot readSetEnd." << endl << indent() << val << "] value";
  indent_down();

  return out.str();
}

string t_st_generator::struct_writer(t_struct* tstruct, string sname) {
  std::ostringstream out;
  const vector<t_field*>& fields = tstruct->get_sorted_members();
  vector<t_field*>::const_iterator fld_iter;

  out << "[oprot writeStructBegin: "
      << "(TStruct new name: '" + tstruct->get_name() + "')." << endl;
  indent_up();

  for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
    bool optional = (*fld_iter)->get_req() == t_field::T_OPTIONAL;
    string fname = camelcase((*fld_iter)->get_name());
    string accessor = sname + " " + camelcase(fname);

    if (optional) {
      out << indent() << accessor << " ifNotNil: [" << endl;
      indent_up();
    }

    out << indent() << "oprot writeFieldBegin: (TField new name: '" << fname
        << "'; type: " << type_to_enum((*fld_iter)->get_type())
        << "; id: " << (*fld_iter)->get_key() << ")." << endl;

    out << indent() << write_val((*fld_iter)->get_type(), accessor) << "." << endl << indent()
        << "oprot writeFieldEnd";

    if (optional) {
      out << "]";
      indent_down();
    }

    out << "." << endl;
  }

  out << indent() << "oprot writeFieldStop; writeStructEnd] value";
  indent_down();

  return out.str();
}

string t_st_generator::struct_reader(t_struct* tstruct, string clsName = "") {
  std::ostringstream out;
  const vector<t_field*>& fields = tstruct->get_members();
  vector<t_field*>::const_iterator fld_iter;
  string val = temp_name();
  string desc = temp_name();
  string found = temp_name();

  if (clsName.size() == 0) {
    clsName = tstruct->get_name();
  }

  out << "[|" << desc << " " << val << "|" << endl;
  indent_up();

  // This is nasty, but without it we'll break things by prefixing TResult.
  string name = ((capitalize(clsName) == "TResult") ? capitalize(clsName) : prefix(clsName));
  out << indent() << val << " := " << name << " new." << endl;

  out << indent() << "iprot readStructBegin." << endl << indent() << "[" << desc
      << " := iprot readFieldBegin." << endl << indent() << desc
      << " type = TType stop] whileFalse: [|" << found << "|" << endl;
  indent_up();

  for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
    out << indent() << desc << " id = " << (*fld_iter)->get_key() << " ifTrue: [" << endl;
    indent_up();

    out << indent() << found << " := true." << endl << indent() << val << " "
        << camelcase((*fld_iter)->get_name()) << ": " << read_val((*fld_iter)->get_type());
    indent_down();

    out << "]." << endl;
  }

  out << indent() << found << " ifNil: [iprot skip: " << desc << " type]]." << endl;
  indent_down();

  out << indent() << "oprot readStructEnd." << endl << indent() << val << "] value";
  indent_down();

  return out.str();
}

string t_st_generator::write_val(t_type* t, string fname) {
  t = get_true_type(t);

  if (t->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)t)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_DOUBLE:
      return "iprot writeDouble: " + fname + " asFloat";
      break;
    case t_base_type::TYPE_I8:
    case t_base_type::TYPE_I16:
    case t_base_type::TYPE_I32:
    case t_base_type::TYPE_I64:
      return "iprot write" + capitalize(type_name(t)) + ": " + fname + " asInteger";
    default:
      return "iprot write" + capitalize(type_name(t)) + ": " + fname;
    }
  } else if (t->is_map()) {
    return map_writer((t_map*)t, fname);
  } else if (t->is_struct() || t->is_xception()) {
    return struct_writer((t_struct*)t, fname);
  } else if (t->is_list()) {
    return list_writer((t_list*)t, fname);
  } else if (t->is_set()) {
    return set_writer((t_set*)t, fname);
  } else if (t->is_enum()) {
    return "iprot writeI32: " + fname;
  } else {
    throw "Sorry, I don't know how to write this: " + type_name(t);
  }
}

string t_st_generator::read_val(t_type* t) {
  t = get_true_type(t);

  if (t->is_base_type()) {
    return "iprot read" + capitalize(type_name(t));
  } else if (t->is_map()) {
    return map_reader((t_map*)t);
  } else if (t->is_struct() || t->is_xception()) {
    return struct_reader((t_struct*)t);
  } else if (t->is_list()) {
    return list_reader((t_list*)t);
  } else if (t->is_set()) {
    return set_reader((t_set*)t);
  } else if (t->is_enum()) {
    return "iprot readI32";
  } else {
    throw "Sorry, I don't know how to read this: " + type_name(t);
  }
}

void t_st_generator::generate_send_method(t_function* function) {
  string funname = function->get_name();
  string signature = function_signature(function);
  t_struct* arg_struct = function->get_arglist();
  const vector<t_field*>& fields = arg_struct->get_members();
  vector<t_field*>::const_iterator fld_iter;

  st_method(f_, client_class_name(), "send" + capitalize(signature));
  f_ << "oprot writeMessageBegin:" << endl;
  indent_up();

  f_ << indent() << "(TCallMessage new" << endl;
  indent_up();

  f_ << indent() << "name: '" << funname << "'; " << endl << indent() << "seqid: self nextSeqid)."
     << endl;
  indent_down();
  indent_down();

  f_ << indent() << "oprot writeStructBegin: "
     << "(TStruct new name: '" + capitalize(camelcase(funname)) + "_args')." << endl;

  for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
    string fname = camelcase((*fld_iter)->get_name());

    f_ << indent() << "oprot writeFieldBegin: (TField new name: '" << fname
       << "'; type: " << type_to_enum((*fld_iter)->get_type()) << "; id: " << (*fld_iter)->get_key()
       << ")." << endl;

    f_ << indent() << write_val((*fld_iter)->get_type(), fname) << "." << endl << indent()
       << "oprot writeFieldEnd." << endl;
  }

  f_ << indent() << "oprot writeFieldStop; writeStructEnd; writeMessageEnd." << endl;
  f_ << indent() << "oprot transport flush";

  st_close_method(f_);
}

// We only support receiving TResult structures (so this won't work on the server side)
void t_st_generator::generate_recv_method(t_function* function) {
  string funname = camelcase(function->get_name());
  string signature = function_signature(function);

  t_struct result(program_, "TResult");
  t_field success(function->get_returntype(), "success", 0);
  result.append(&success);

  t_struct* xs = function->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) {
    // duplicate the field, but call it "exception"... we don't need a dynamic name
    t_field* exception = new t_field((*f_iter)->get_type(), "exception", (*f_iter)->get_key());
    result.append(exception);
  }

  st_method(f_, client_class_name(), "recv" + capitalize(funname));
  f_ << "| f msg res | " << endl << indent() << "msg := oprot readMessageBegin." << endl << indent()
     << "self validateRemoteMessage: msg." << endl << indent()
     << "res := " << struct_reader(&result) << "." << endl << indent() << "oprot readMessageEnd."
     << endl << indent() << "oprot transport flush." << endl << indent()
     << "res exception ifNotNil: [res exception signal]." << endl << indent() << "^ res";
  st_close_method(f_);
}

string t_st_generator::function_types_comment(t_function* fn) {
  std::ostringstream out;
  const vector<t_field*>& fields = fn->get_arglist()->get_members();
  vector<t_field*>::const_iterator f_iter;

  out << "\"";

  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    out << camelcase((*f_iter)->get_name()) << ": " << type_name((*f_iter)->get_type());
    if ((f_iter + 1) != fields.end()) {
      out << ", ";
    }
  }

  out << "\"";

  return out.str();
}

/**
 * Generates a service client definition.
 *
 * @param tservice The service to generate a server for.
 */
void t_st_generator::generate_service_client(t_service* tservice) {
  string extends = "";
  string extends_client = "TClient";
  vector<t_function*> functions = tservice->get_functions();
  vector<t_function*>::iterator f_iter;

  if (tservice->get_extends() != nullptr) {
    extends = type_name(tservice->get_extends());
    extends_client = extends + "Client";
  }

  f_ << extends_client << " subclass: #" << prefix(client_class_name()) << endl
     << "\tinstanceVariableNames: ''\n"
     << "\tclassVariableNames: ''\n"
     << "\tpoolDictionaries: ''\n"
     << "\tcategory: '" << generated_category() << "'!\n\n";

  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
    string funname = camelcase((*f_iter)->get_name());
    string signature = function_signature(*f_iter);

    st_method(f_, client_class_name(), signature);
    f_ << function_types_comment(*f_iter) << endl << indent() << "self send"
       << capitalize(signature) << "." << endl;

    if (!(*f_iter)->is_oneway()) {
      f_ << indent() << "^ self recv" << capitalize(funname) << " success " << endl;
    }

    st_close_method(f_);

    generate_send_method(*f_iter);
    if (!(*f_iter)->is_oneway()) {
      generate_recv_method(*f_iter);
    }
  }
}

/**
 * Renders a function signature of the form 'type name(args)'
 *
 * @param tfunction Function definition
 * @return String of rendered function definition
 */
string t_st_generator::function_signature(t_function* tfunction) {
  return camelcase(tfunction->get_name()) + capitalize(argument_list(tfunction->get_arglist()));
}

/**
 * Renders a field list
 */
string t_st_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 += " ";
    }
    string name = camelcase((*f_iter)->get_name());
    result += name + ": " + name;
  }
  return result;
}

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

  string name = ttype->get_name();
  if (ttype->is_struct() || ttype->is_xception()) {
    name = capitalize(ttype->get_name());
  }

  return prefix + name;
}

/* Convert t_type to Smalltalk type code */
string t_st_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:
      throw "NO T_VOID CONSTRUCT";
    case t_base_type::TYPE_STRING:
      return "TType string";
    case t_base_type::TYPE_BOOL:
      return "TType bool";
    case t_base_type::TYPE_I8:
      return "TType byte";
    case t_base_type::TYPE_I16:
      return "TType i16";
    case t_base_type::TYPE_I32:
      return "TType i32";
    case t_base_type::TYPE_I64:
      return "TType i64";
    case t_base_type::TYPE_DOUBLE:
      return "TType double";
    default:
      throw "compiler error: unhandled type";
    }
  } else if (type->is_enum()) {
    return "TType i32";
  } else if (type->is_struct() || type->is_xception()) {
    return "TType struct";
  } else if (type->is_map()) {
    return "TType map";
  } else if (type->is_set()) {
    return "TType set";
  } else if (type->is_list()) {
    return "TType list";
  }

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

THRIFT_REGISTER_GENERATOR(st, "Smalltalk", "")