forked from apache/thrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht_delphi_generator.cc
3887 lines (3392 loc) · 142 KB
/
t_delphi_generator.cc
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
/*
* 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 <cassert>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <list>
#include <stdlib.h>
#include <sys/stat.h>
#include <sstream>
#include <cctype>
#include "platform.h"
#include "t_oop_generator.h"
using std::map;
using std::ofstream;
using std::ostream;
using std::ostringstream;
using std::string;
using std::stringstream;
using std::vector;
static const string endl = "\n"; // avoid ostream << std::endl flushes
class t_delphi_generator : public t_oop_generator {
public:
t_delphi_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;
indent_impl_ = 0;
has_forward = false;
has_enum = false;
has_const = false;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("ansistr_binary");
ansistr_binary_ = (iter != parsed_options.end());
iter = parsed_options.find("register_types");
register_types_ = (iter != parsed_options.end());
iter = parsed_options.find("constprefix");
constprefix_ = (iter != parsed_options.end());
iter = parsed_options.find("events");
events_ = (iter != parsed_options.end());
iter = parsed_options.find("xmldoc");
xmldoc_ = (iter != parsed_options.end());
out_dir_base_ = "gen-delphi";
escape_.clear();
escape_['\''] = "''";
}
void init_generator();
void close_generator();
void generate_consts(std::vector<t_const*> consts);
void generate_typedef(t_typedef* ttypedef);
void generate_enum(t_enum* tenum);
void generate_forward_declaration(t_struct* tstruct);
void generate_struct(t_struct* tstruct);
void generate_xception(t_struct* txception);
void generate_service(t_service* tservice);
void generate_property(ostream& out, t_field* tfield, bool isPublic, bool is_xception);
void generate_property_writer_(ostream& out, t_field* tfield, bool isPublic);
void generate_delphi_property(ostream& out,
bool struct_is_exception,
t_field* tfield,
bool isPublic,
std::string fieldPrefix = "");
void generate_delphi_isset_reader_definition(ostream& out, t_field* tfield, bool is_xception);
void generate_delphi_property_reader_definition(ostream& out,
t_field* tfield,
bool is_xception_class);
void generate_delphi_property_writer_definition(ostream& out,
t_field* tfield,
bool is_xception_class);
void generate_delphi_property_reader_impl(ostream& out,
std::string cls_prefix,
std::string name,
t_type* type,
t_field* tfield,
std::string fieldPrefix,
bool is_xception_class);
void generate_delphi_property_writer_impl(ostream& out,
std::string cls_prefix,
std::string name,
t_type* type,
t_field* tfield,
std::string fieldPrefix,
bool is_xception_class,
bool is_union,
bool is_xception_factory,
std::string xception_factroy_name);
void generate_delphi_clear_union_value(ostream& out,
std::string cls_prefix,
std::string name,
t_type* type,
t_field* tfield,
std::string fieldPrefix,
bool is_xception_class,
bool is_union,
bool is_xception_factory,
std::string xception_factroy_name);
void generate_delphi_isset_reader_impl(ostream& out,
std::string cls_prefix,
std::string name,
t_type* type,
t_field* tfield,
std::string fieldPrefix,
bool is_xception);
void generate_delphi_struct_writer_impl(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception);
void generate_delphi_struct_result_writer_impl(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception);
void generate_delphi_struct_tostring_impl(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception,
bool is_x_factory);
void add_delphi_uses_list(string unitname);
void generate_delphi_struct_reader_impl(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception);
void generate_delphi_create_exception_impl(ostream& out,
string cls_prefix,
t_struct* tstruct,
bool is_exception);
bool const_needs_var(t_type* type);
void print_const_prop(std::ostream& out, string name, t_type* type, t_const_value* value);
void print_private_field(std::ostream& out, string name, t_type* type, t_const_value* value);
void print_const_value(std::ostream& vars,
std::ostream& out,
std::string name,
t_type* type,
t_const_value* value);
void initialize_field(std::ostream& vars,
std::ostream& out,
std::string name,
t_type* type,
t_const_value* value);
void finalize_field(std::ostream& out,
std::string name,
t_type* type,
t_const_value* value,
std::string cls_nm = "");
std::string render_const_value(std::ostream& local_vars,
std::ostream& out,
std::string name,
t_type* type,
t_const_value* value);
void print_const_def_value(std::ostream& vars,
std::ostream& out,
std::string name,
t_type* type,
t_const_value* value,
std::string cls_nm = "");
std::string make_constants_classname();
void generate_delphi_struct(t_struct* tstruct, bool is_exception);
void generate_delphi_struct_impl(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception,
bool is_result = false,
bool is_x_factory = false);
void print_delphi_struct_type_factory_func(ostream& out, t_struct* tstruct);
void generate_delphi_struct_type_factory(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception,
bool is_result = false,
bool is_x_factory = false);
void generate_delphi_struct_type_factory_registration(ostream& out,
std::string cls_prefix,
t_struct* tstruct,
bool is_exception,
bool is_result = false,
bool is_x_factory = false);
void generate_delphi_struct_definition(std::ostream& out,
t_struct* tstruct,
bool is_xception = false,
bool in_class = false,
bool is_result = false,
bool is_x_factory = false);
void generate_delphi_struct_reader(std::ostream& out, t_struct* tstruct);
void generate_delphi_struct_result_writer(std::ostream& out, t_struct* tstruct);
void generate_delphi_struct_writer(std::ostream& out, t_struct* tstruct);
void generate_delphi_struct_tostring(std::ostream& out, t_struct* tstruct);
void generate_function_helpers(t_function* tfunction);
void generate_service_interface(t_service* tservice);
void generate_service_helpers(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* function);
void generate_deserialize_field(std::ostream& out,
bool is_xception,
t_field* tfield,
std::string prefix,
std::ostream& local_vars);
void generate_deserialize_struct(std::ostream& out,
t_struct* tstruct,
std::string name,
std::string prefix);
void generate_deserialize_container(ostream& out,
bool is_xception,
t_type* ttype,
string name,
std::ostream& local_vars);
void generate_deserialize_set_element(std::ostream& out,
bool is_xception,
t_set* tset,
std::string prefix,
std::ostream& local_vars);
void generate_deserialize_map_element(std::ostream& out,
bool is_xception,
t_map* tmap,
std::string prefix,
std::ostream& local_vars);
void generate_deserialize_list_element(std::ostream& out,
bool is_xception,
t_list* list,
std::string prefix,
std::ostream& local_vars);
void generate_serialize_field(std::ostream& out,
bool is_xception,
t_field* tfield,
std::string prefix,
std::ostream& local_vars);
void generate_serialize_struct(std::ostream& out,
t_struct* tstruct,
std::string prefix,
std::ostream& local_vars);
void generate_serialize_container(std::ostream& out,
bool is_xception,
t_type* ttype,
std::string prefix,
std::ostream& local_vars);
void generate_serialize_map_element(std::ostream& out,
bool is_xception,
t_map* tmap,
std::string iter,
std::string map,
std::ostream& local_vars);
void generate_serialize_set_element(std::ostream& out,
bool is_xception,
t_set* tmap,
std::string iter,
std::ostream& local_vars);
void generate_serialize_list_element(std::ostream& out,
bool is_xception,
t_list* tlist,
std::string iter,
std::ostream& local_vars);
void delphi_type_usings(std::ostream& out);
std::string delphi_thrift_usings();
std::string type_name(t_type* ttype,
bool b_cls = false,
bool b_no_postfix = false,
bool b_exception_factory = false,
bool b_full_exception_factory = false);
std::string normalize_clsnm(std::string name,
std::string prefix,
bool b_no_check_keyword = false);
std::string make_valid_delphi_identifier(std::string const& fromName);
std::string input_arg_prefix(t_type* ttype);
std::string base_type_name(t_base_type* tbase);
std::string declare_field(t_field* tfield,
bool init = false,
std::string prefix = "",
bool is_xception_class = false);
std::string function_signature(t_function* tfunction,
std::string full_cls = "",
bool is_xception = false);
std::string argument_list(t_struct* tstruct);
std::string constructor_argument_list(t_struct* tstruct, std::string current_indent);
std::string type_to_enum(t_type* ttype);
std::string prop_name(t_field* tfield, bool is_xception = false);
std::string prop_name(std::string name, bool is_xception = false);
std::string constructor_param_name(string name);
void write_enum(std::string line);
void write_forward_decr(std::string line);
void write_const(std::string line);
void write_struct(std::string line);
void write_service(std::string line);
virtual std::string autogen_comment() {
return std::string("(**\n") + " * Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
+ " *\n" + " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
+ " *)\n";
}
string replace_all(string contents, string search, string replace);
string xml_encode(string contents);
string xmldoc_encode(string contents);
string xmlattrib_encode(string contents);
void generate_delphi_doc(std::ostream& out, t_field* field);
void generate_delphi_doc(std::ostream& out, t_doc* tdoc);
void generate_delphi_doc(std::ostream& out, t_function* tdoc);
void generate_delphi_docstring_comment(std::ostream& out, string contents);
bool type_can_be_null(t_type* ttype) {
while (ttype->is_typedef()) {
ttype = ((t_typedef*)ttype)->get_type();
}
return ttype->is_container() || ttype->is_struct() || ttype->is_xception();
}
private:
std::string namespace_name_;
std::ostringstream s_forward_decr;
std::ostringstream s_enum;
std::ostringstream s_const;
std::ostringstream s_struct;
std::ostringstream s_service;
std::ostringstream s_const_impl;
std::ostringstream s_struct_impl;
std::ostringstream s_service_impl;
std::ostringstream s_type_factory_registration;
std::ostringstream s_type_factory_funcs;
bool has_forward;
bool has_enum;
bool has_const;
std::string namespace_dir_;
std::map<std::string, int> delphi_keywords;
std::map<std::string, int> delphi_reserved_method;
std::map<std::string, int> delphi_reserved_method_exception;
std::map<std::string, int> types_known;
std::list<t_typedef*> typedefs_pending;
std::vector<std::string> uses_list;
void create_keywords();
bool find_keyword(std::map<std::string, int>& keyword_map, std::string name);
std::string normalize_name(std::string name,
bool b_method = false,
bool b_exception_method = false);
std::string empty_value(t_type* type);
bool is_fully_defined_type(t_type* ttype);
void add_defined_type(t_type* ttype);
void init_known_types_list();
bool is_void(t_type* type);
int indent_impl_;
bool ansistr_binary_;
bool register_types_;
bool constprefix_;
bool events_;
bool xmldoc_;
void indent_up_impl() { ++indent_impl_; };
void indent_down_impl() { --indent_impl_; };
std::string indent_impl() {
std::string ind = "";
int i;
for (i = 0; i < indent_impl_; ++i) {
ind += " ";
}
return ind;
};
std::ostream& indent_impl(std::ostream& os) { return os << indent_impl(); };
};
string t_delphi_generator::replace_all(string contents, string search, string repl) {
string str(contents);
size_t slen = search.length();
size_t rlen = repl.length();
size_t incr = (rlen > 0) ? rlen : 1;
if (slen > 0) {
size_t found = str.find(search);
while ((found != string::npos) && (found < str.length())) {
str.replace(found, slen, repl);
found = str.find(search, found + incr);
}
}
return str;
}
// XML encoding
string t_delphi_generator::xml_encode(string contents) {
string str(contents);
// escape the escape
str = replace_all(str, "&", "&");
// other standard XML entities
str = replace_all(str, "<", "<");
str = replace_all(str, ">", ">");
return str;
}
// XML attribute encoding
string t_delphi_generator::xmlattrib_encode(string contents) {
string str(xml_encode(contents));
// our attribs are enclosed in "
str = replace_all(str, "\"", "\\\"");
return str;
}
// XML encoding for doc comments
string t_delphi_generator::xmldoc_encode(string contents) {
string str(xml_encode(contents));
// XMLDoc specific: convert linebreaks into <para>graphs</para>
str = replace_all(str, "\r\n", "\r");
str = replace_all(str, "\n", "\r");
str = replace_all(str, "\r", "</para>\n<para>");
return str;
}
void t_delphi_generator::generate_delphi_docstring_comment(ostream& out, string contents) {
if (xmldoc_) {
generate_docstring_comment(out,
"{$REGION 'XMLDoc'}/// <summary>\n",
"/// ",
"<para>" + contents + "</para>",
"/// </summary>\n{$ENDREGION}\n");
}
}
void t_delphi_generator::generate_delphi_doc(ostream& out, t_field* field) {
if (xmldoc_) {
if (field->get_type()->is_enum()) {
string combined_message = xmldoc_encode(field->get_doc()) + "\n<seealso cref=\""
+ xmldoc_encode(type_name(field->get_type())) + "\"/>";
generate_delphi_docstring_comment(out, combined_message);
} else {
generate_delphi_doc(out, (t_doc*)field);
}
}
}
void t_delphi_generator::generate_delphi_doc(ostream& out, t_doc* tdoc) {
if (tdoc->has_doc() && xmldoc_) {
generate_delphi_docstring_comment(out, xmldoc_encode(tdoc->get_doc()));
}
}
void t_delphi_generator::generate_delphi_doc(ostream& out, t_function* tfunction) {
if (tfunction->has_doc() && xmldoc_) {
stringstream ps;
const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
vector<t_field*>::const_iterator p_iter;
for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
t_field* p = *p_iter;
ps << "\n<param name=\"" << xmlattrib_encode(p->get_name()) << "\">";
if (p->has_doc()) {
std::string str = p->get_doc();
str.erase(std::remove(str.begin(), str.end(), '\n'),
str.end()); // remove the newlines that appear from the parser
ps << xmldoc_encode(str);
}
ps << "</param>";
}
generate_docstring_comment(out,
"{$REGION 'XMLDoc'}",
"/// ",
"<summary><para>" + xmldoc_encode(tfunction->get_doc())
+ "</para></summary>" + ps.str(),
"{$ENDREGION}\n");
}
}
bool t_delphi_generator::find_keyword(std::map<std::string, int>& keyword_map, std::string name) {
std::string::size_type len = name.length();
if (len <= 0) {
return false;
}
std::string::size_type nlast = name.find_last_of('_');
if (nlast >= 1) {
if (nlast == (len - 1)) {
string new_name(name, 0, nlast);
return find_keyword(keyword_map, new_name);
}
}
return (keyword_map[name] == 1);
}
std::string t_delphi_generator::normalize_name(std::string name,
bool b_method,
bool b_exception_method) {
string tmp(name);
std::transform(tmp.begin(), tmp.end(), tmp.begin(), static_cast<int (*)(int)>(std::tolower));
bool b_found = false;
if (find_keyword(delphi_keywords, tmp)) {
b_found = true;
} else if (b_method && find_keyword(delphi_reserved_method, tmp)) {
b_found = true;
} else if (b_exception_method && find_keyword(delphi_reserved_method_exception, tmp)) {
b_found = true;
}
if (b_found) {
return name + "_";
} else {
return name;
}
}
void t_delphi_generator::create_keywords() {
delphi_keywords["and"] = 1;
delphi_keywords["end"] = 1;
delphi_keywords["interface"] = 1;
delphi_keywords["raise"] = 1;
delphi_keywords["uses"] = 1;
delphi_keywords["array"] = 1;
delphi_keywords["except"] = 1;
delphi_keywords["is"] = 1;
delphi_keywords["record"] = 1;
delphi_keywords["var"] = 1;
delphi_keywords["as"] = 1;
delphi_keywords["exports"] = 1;
delphi_keywords["label"] = 1;
delphi_keywords["repeat"] = 1;
delphi_keywords["while"] = 1;
delphi_keywords["asm"] = 1;
delphi_keywords["file"] = 1;
delphi_keywords["library"] = 1;
delphi_keywords["resourcestring"] = 1;
delphi_keywords["with"] = 1;
delphi_keywords["begin"] = 1;
delphi_keywords["finalization"] = 1;
delphi_keywords["mod"] = 1;
delphi_keywords["set"] = 1;
delphi_keywords["xor"] = 1;
delphi_keywords["case"] = 1;
delphi_keywords["finally"] = 1;
delphi_keywords["nil"] = 1;
delphi_keywords["shl"] = 1;
delphi_keywords["class"] = 1;
delphi_keywords["for"] = 1;
delphi_keywords["not"] = 1;
delphi_keywords["shr"] = 1;
delphi_keywords["const"] = 1;
delphi_keywords["function"] = 1;
delphi_keywords["object"] = 1;
delphi_keywords["string"] = 1;
delphi_keywords["constructor"] = 1;
delphi_keywords["goto"] = 1;
delphi_keywords["of"] = 1;
delphi_keywords["then"] = 1;
delphi_keywords["destructor"] = 1;
delphi_keywords["if"] = 1;
delphi_keywords["or"] = 1;
delphi_keywords["threadvar"] = 1;
delphi_keywords["dispinterface"] = 1;
delphi_keywords["implementation"] = 1;
delphi_keywords["out"] = 1;
delphi_keywords["to"] = 1;
delphi_keywords["div"] = 1;
delphi_keywords["in"] = 1;
delphi_keywords["packed"] = 1;
delphi_keywords["try"] = 1;
delphi_keywords["do"] = 1;
delphi_keywords["inherited"] = 1;
delphi_keywords["procedure"] = 1;
delphi_keywords["type"] = 1;
delphi_keywords["downto"] = 1;
delphi_keywords["initialization"] = 1;
delphi_keywords["program"] = 1;
delphi_keywords["unit"] = 1;
delphi_keywords["else"] = 1;
delphi_keywords["inline"] = 1;
delphi_keywords["property"] = 1;
delphi_keywords["until"] = 1;
delphi_keywords["private"] = 1;
delphi_keywords["protected"] = 1;
delphi_keywords["public"] = 1;
delphi_keywords["published"] = 1;
delphi_keywords["automated"] = 1;
delphi_keywords["at"] = 1;
delphi_keywords["on"] = 1;
// reserved/predefined variables and types (lowercase!)
delphi_keywords["result"] = 1;
delphi_keywords["tbytes"] = 1;
delphi_keywords["tobject"] = 1;
delphi_keywords["tclass"] = 1;
delphi_keywords["tinterfacedobject"] = 1;
delphi_reserved_method["create"] = 1;
delphi_reserved_method["free"] = 1;
delphi_reserved_method["initinstance"] = 1;
delphi_reserved_method["cleanupinstance"] = 1;
delphi_reserved_method["classtype"] = 1;
delphi_reserved_method["classname"] = 1;
delphi_reserved_method["classnameis"] = 1;
delphi_reserved_method["classparent"] = 1;
delphi_reserved_method["classinfo"] = 1;
delphi_reserved_method["instancesize"] = 1;
delphi_reserved_method["inheritsfrom"] = 1;
delphi_reserved_method["methodaddress"] = 1;
delphi_reserved_method["methodaddress"] = 1;
delphi_reserved_method["methodname"] = 1;
delphi_reserved_method["fieldaddress"] = 1;
delphi_reserved_method["fieldaddress"] = 1;
delphi_reserved_method["getinterface"] = 1;
delphi_reserved_method["getinterfaceentry"] = 1;
delphi_reserved_method["getinterfacetable"] = 1;
delphi_reserved_method["unitname"] = 1;
delphi_reserved_method["equals"] = 1;
delphi_reserved_method["gethashcode"] = 1;
delphi_reserved_method["tostring"] = 1;
delphi_reserved_method["safecallexception"] = 1;
delphi_reserved_method["afterconstruction"] = 1;
delphi_reserved_method["beforedestruction"] = 1;
delphi_reserved_method["dispatch"] = 1;
delphi_reserved_method["defaulthandler"] = 1;
delphi_reserved_method["newinstance"] = 1;
delphi_reserved_method["freeinstance"] = 1;
delphi_reserved_method["destroy"] = 1;
delphi_reserved_method["read"] = 1;
delphi_reserved_method["write"] = 1;
delphi_reserved_method_exception["setinnerexception"] = 1;
delphi_reserved_method_exception["setstackinfo"] = 1;
delphi_reserved_method_exception["getstacktrace"] = 1;
delphi_reserved_method_exception["raisingexception"] = 1;
delphi_reserved_method_exception["createfmt"] = 1;
delphi_reserved_method_exception["createres"] = 1;
delphi_reserved_method_exception["createresfmt"] = 1;
delphi_reserved_method_exception["createhelp"] = 1;
delphi_reserved_method_exception["createfmthelp"] = 1;
delphi_reserved_method_exception["createreshelp"] = 1;
delphi_reserved_method_exception["createresfmthelp"] = 1;
delphi_reserved_method_exception["getbaseexception"] = 1;
delphi_reserved_method_exception["baseexception"] = 1;
delphi_reserved_method_exception["helpcontext"] = 1;
delphi_reserved_method_exception["innerexception"] = 1;
delphi_reserved_method_exception["message"] = 1;
delphi_reserved_method_exception["stacktrace"] = 1;
delphi_reserved_method_exception["stackinfo"] = 1;
delphi_reserved_method_exception["getexceptionstackinfoproc"] = 1;
delphi_reserved_method_exception["getstackinfostringproc"] = 1;
delphi_reserved_method_exception["cleanupstackinfoproc"] = 1;
delphi_reserved_method_exception["raiseouterexception"] = 1;
delphi_reserved_method_exception["throwouterexception"] = 1;
}
void t_delphi_generator::add_delphi_uses_list(string unitname) {
vector<std::string>::const_iterator s_iter;
bool found = false;
for (s_iter = uses_list.begin(); s_iter != uses_list.end(); ++s_iter) {
if ((*s_iter) == unitname) {
found = true;
break;
}
}
if (!found) {
uses_list.push_back(unitname);
}
}
void t_delphi_generator::init_generator() {
indent_impl_ = 0;
namespace_name_ = program_->get_namespace("delphi");
has_forward = false;
has_enum = false;
has_const = false;
create_keywords();
add_delphi_uses_list("Classes");
add_delphi_uses_list("SysUtils");
add_delphi_uses_list("Generics.Collections");
add_delphi_uses_list("Thrift");
add_delphi_uses_list("Thrift.Utils");
add_delphi_uses_list("Thrift.Collections");
add_delphi_uses_list("Thrift.Protocol");
add_delphi_uses_list("Thrift.Transport");
if (register_types_) {
add_delphi_uses_list("Thrift.TypeRegistry");
}
init_known_types_list();
string unitname, nsname;
const vector<t_program*>& includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
unitname = includes[i]->get_name();
nsname = includes[i]->get_namespace("delphi");
if ("" != nsname) {
unitname = nsname;
}
add_delphi_uses_list(unitname);
}
MKDIR(get_out_dir().c_str());
}
void t_delphi_generator::close_generator() {
std::string unitname = program_name_;
if ("" != namespace_name_) {
unitname = namespace_name_;
}
for (int i = 0; i < (int)unitname.size(); i++) {
if (unitname[i] == ' ') {
unitname.replace(i, 1, "_");
}
}
std::string f_name = get_out_dir() + "/" + unitname + ".pas";
std::ofstream f_all;
f_all.open(f_name.c_str());
f_all << autogen_comment() << endl;
generate_delphi_doc(f_all, program_);
f_all << "unit " << unitname << ";" << endl << endl;
f_all << "interface" << endl << endl;
f_all << "uses" << endl;
indent_up();
vector<std::string>::const_iterator s_iter;
for (s_iter = uses_list.begin(); s_iter != uses_list.end(); ++s_iter) {
if (s_iter != uses_list.begin()) {
f_all << ",";
f_all << endl;
}
indent(f_all) << *s_iter;
}
f_all << ";" << endl << endl;
indent_down();
string tmp_unit(unitname);
for (int i = 0; i < (int)tmp_unit.size(); i++) {
if (tmp_unit[i] == '.') {
tmp_unit.replace(i, 1, "_");
}
}
f_all << "const" << endl;
indent_up();
indent(f_all) << "c" << tmp_unit
<< "_Option_AnsiStr_Binary = " << (ansistr_binary_ ? "True" : "False") << ";"
<< endl;
indent(f_all) << "c" << tmp_unit
<< "_Option_Register_Types = " << (register_types_ ? "True" : "False") << ";"
<< endl;
indent(f_all) << "c" << tmp_unit
<< "_Option_ConstPrefix = " << (constprefix_ ? "True" : "False") << ";" << endl;
indent(f_all) << "c" << tmp_unit << "_Option_Events = " << (events_ ? "True" : "False")
<< ";" << endl;
indent(f_all) << "c" << tmp_unit << "_Option_XmlDoc = " << (xmldoc_ ? "True" : "False")
<< ";" << endl;
indent_down();
f_all << endl;
f_all << "type" << endl;
if (has_forward) {
f_all << s_forward_decr.str() << endl;
}
if (has_enum) {
indent(f_all) << endl;
indent(f_all) << "{$SCOPEDENUMS ON}" << endl << endl;
f_all << s_enum.str();
indent(f_all) << "{$SCOPEDENUMS OFF}" << endl << endl;
}
f_all << s_struct.str();
f_all << s_service.str();
f_all << s_const.str();
f_all << "implementation" << endl << endl;
f_all << s_struct_impl.str();
f_all << s_service_impl.str();
f_all << s_const_impl.str();
if (register_types_) {
f_all << endl;
f_all << "// Type factory methods and registration" << endl;
f_all << s_type_factory_funcs.str();
f_all << "procedure RegisterTypeFactories;" << endl;
f_all << "begin" << endl;
f_all << s_type_factory_registration.str();
f_all << "end;" << endl;
}
f_all << endl;
string constants_class = make_constants_classname();
f_all << "initialization" << endl;
if (has_const) {
f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
f_all << " " << constants_class.c_str() << "_Initialize;" << endl;
f_all << "{$IFEND}" << endl;
}
if (register_types_) {
f_all << " RegisterTypeFactories;" << endl;
}
f_all << endl;
f_all << "finalization" << endl;
if (has_const) {
f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
f_all << " " << constants_class.c_str() << "_Finalize;" << endl;
f_all << "{$IFEND}" << endl;
}
f_all << endl << endl;
f_all << "end." << endl;
f_all.close();
if (!typedefs_pending.empty()) {
pwarning(0, "%d typedefs with unresolved type references left:\n", typedefs_pending.size());
for (std::list<t_typedef*>::iterator iter = typedefs_pending.begin();
typedefs_pending.end() != iter;
++iter) {
pwarning(0, "- %s\n", (*iter)->get_symbolic().c_str());
}
}
}
void t_delphi_generator::delphi_type_usings(ostream& out) {
indent_up();
indent(out) << "Classes, SysUtils, Generics.Collections, Thrift.Collections, Thrift.Protocol,"
<< endl;
indent(out) << "Thrift.Transport;" << endl << endl;
indent_down();
}
void t_delphi_generator::generate_forward_declaration(t_struct* tstruct) {
// Forward declare struct def
has_forward = true;
pverbose("forward declaration of %s\n", type_name(tstruct).c_str());
string what = tstruct->is_xception() ? "class" : "interface";
indent_up();
indent(s_forward_decr) << type_name(tstruct, tstruct->is_xception(), true) << " = " << what << ";"
<< endl;
indent_down();
add_defined_type(tstruct);
}
void t_delphi_generator::generate_typedef(t_typedef* ttypedef) {
t_type* type = ttypedef->get_type();
// write now or save for later?
if (!is_fully_defined_type(type)) {
pverbose("typedef %s: unresolved dependencies found\n", type_name(ttypedef).c_str());
typedefs_pending.push_back(ttypedef);
return;
}
indent_up();
generate_delphi_doc(s_struct, ttypedef);
indent(s_struct) << type_name(ttypedef) << " = ";
// commented out: the benefit is not big enough to risk breaking existing code
// bool container = type->is_list() || type->is_map() || type->is_set();
// if( ! container)
// s_struct << "type "; //the "type A = type B" syntax leads to E2574 with generics
s_struct << type_name(ttypedef->get_type()) << ";" << endl << endl;
indent_down();
add_defined_type(ttypedef);
}
bool t_delphi_generator::is_fully_defined_type(t_type* ttype) {
if ((NULL != ttype->get_program()) && (ttype->get_program() != program_)) {
t_scope* scope = ttype->get_program()->scope();
if (NULL != scope->get_type(ttype->get_name())) {
// printf("type %s found in included scope %s\n", ttype->get_name().c_str(),
// ttype->get_program()->get_name().c_str());
return true;
}
}
if (ttype->is_typedef()) {
return (1 == types_known[type_name(ttype)]);
}
if (ttype->is_base_type()) {
return (1 == types_known[base_type_name((t_base_type*)ttype)]);
} else if (ttype->is_enum()) {
return true; // enums are written first, before all other types
} else if (ttype->is_map()) {
t_map* tmap = (t_map*)ttype;
return is_fully_defined_type(tmap->get_key_type())
&& is_fully_defined_type(tmap->get_val_type());
} else if (ttype->is_set()) {
t_set* tset = (t_set*)ttype;
return is_fully_defined_type(tset->get_elem_type());
} else if (ttype->is_list()) {
t_list* tlist = (t_list*)ttype;
return is_fully_defined_type(tlist->get_elem_type());
}
return (1 == types_known[type_name(ttype)]);
}
void t_delphi_generator::add_defined_type(t_type* ttype) {
// mark as known type
types_known[type_name(ttype)] = 1;
// check all pending typedefs
std::list<t_typedef*>::iterator iter;
bool more = true;
while (more && (!typedefs_pending.empty())) {
more = false;
for (iter = typedefs_pending.begin(); typedefs_pending.end() != iter; ++iter) {
t_typedef* ttypedef = (*iter);
if (is_fully_defined_type(ttypedef->get_type())) {
pverbose("typedef %s: all pending references are now resolved\n",
type_name(ttypedef).c_str());
typedefs_pending.erase(iter);
generate_typedef(ttypedef);
more = true;
break;
}
}
}
}
void t_delphi_generator::init_known_types_list() {
// known base types
types_known[type_name(g_type_string)] = 1;
types_known[type_name(g_type_binary)] = 1;
types_known[type_name(g_type_bool)] = 1;
types_known[type_name(g_type_byte)] = 1;
types_known[type_name(g_type_i16)] = 1;
types_known[type_name(g_type_i32)] = 1;
types_known[type_name(g_type_i64)] = 1;
types_known[type_name(g_type_double)] = 1;
}
void t_delphi_generator::generate_enum(t_enum* tenum) {
has_enum = true;
indent_up();
generate_delphi_doc(s_enum, tenum);
indent(s_enum) << type_name(tenum, true, true) << " = "
<< "(" << endl;
indent_up();
vector<t_enum_value*> constants = tenum->get_constants();
if (constants.empty()) {
indent(s_enum) << "dummy = 0 // empty enums are not allowed";
} else {
vector<t_enum_value*>::iterator c_iter;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
if (c_iter != constants.begin()) {
s_enum << ",";