1 #include <cassert>
2 
3 #include <string>
4 #include <fstream>
5 #include <iostream>
6 #include <vector>
7 #include <cctype>
8 
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <sstream>
12 
13 #include "thrift/platform.h"
14 #include "thrift/generate/t_oop_generator.h"
15 
16 using std::map;
17 using std::ostream;
18 using std::ostringstream;
19 using std::string;
20 using std::stringstream;
21 using std::vector;
22 
23 static const string endl = "\n"; // avoid ostream << std::endl flushes
24 
25 class t_netcore_generator : public t_oop_generator
26 {
27 
28     struct member_mapping_scope
29     {
30     public:
member_mapping_scopemember_mapping_scope31         member_mapping_scope() : scope_member(0) { }
32         void* scope_member;
33         map<string, string> mapping_table;
34     };
35 
36 public:
37     t_netcore_generator(t_program* program, const map<string, string>& parsed_options, const string& option_string);
38 
39     bool is_wcf_enabled() const;
40     bool is_nullable_enabled() const;
41     bool is_hashcode_enabled() const;
42     bool is_serialize_enabled() const;
43     bool is_union_enabled() const;
44     map<string, int> get_keywords_list() const;
45 
46     // overrides
47     void init_generator();
48     void close_generator();
49     void generate_consts(vector<t_const*> consts);
50     void generate_consts(ostream& out, vector<t_const*> consts);
51     void generate_typedef(t_typedef* ttypedef);
52     void generate_enum(t_enum* tenum);
53     void generate_enum(ostream& out, t_enum* tenum);
54     void generate_struct(t_struct* tstruct);
55     void generate_xception(t_struct* txception);
56     void generate_service(t_service* tservice);
57 
58     void generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset);
59     void generate_netcore_property(ostream& out, t_field* tfield, bool isPublic, bool includeIsset = true, string fieldPrefix = "");
60     bool print_const_value(ostream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval = false, bool needtype = false);
61     string render_const_value(ostream& out, string name, t_type* type, t_const_value* value);
62     void print_const_constructor(ostream& out, vector<t_const*> consts);
63     void print_const_def_value(ostream& out, string name, t_type* type, t_const_value* value);
64     void generate_netcore_struct(t_struct* tstruct, bool is_exception);
65     void generate_netcore_union(t_struct* tunion);
66     void generate_netcore_struct_definition(ostream& out, t_struct* tstruct, bool is_xception = false, bool in_class = false, bool is_result = false);
67     void generate_netcore_union_definition(ostream& out, t_struct* tunion);
68     void generate_netcore_union_class(ostream& out, t_struct* tunion, t_field* tfield);
69     void generate_netcore_wcffault(ostream& out, t_struct* tstruct);
70     void generate_netcore_struct_reader(ostream& out, t_struct* tstruct);
71     void generate_netcore_struct_result_writer(ostream& out, t_struct* tstruct);
72     void generate_netcore_struct_writer(ostream& out, t_struct* tstruct);
73     void generate_netcore_struct_tostring(ostream& out, t_struct* tstruct);
74     void generate_netcore_struct_equals(ostream& out, t_struct* tstruct);
75     void generate_netcore_struct_hashcode(ostream& out, t_struct* tstruct);
76     void generate_netcore_union_reader(ostream& out, t_struct* tunion);
77     void generate_function_helpers(ostream& out, t_function* tfunction);
78     void generate_service_interface(ostream& out, t_service* tservice);
79     void generate_service_helpers(ostream& out, t_service* tservice);
80     void generate_service_client(ostream& out, t_service* tservice);
81     void generate_service_server(ostream& out, t_service* tservice);
82     void generate_process_function_async(ostream& out, t_service* tservice, t_function* function);
83     void generate_deserialize_field(ostream& out, t_field* tfield, string prefix = "", bool is_propertyless = false);
84     void generate_deserialize_struct(ostream& out, t_struct* tstruct, string prefix = "");
85     void generate_deserialize_container(ostream& out, t_type* ttype, string prefix = "");
86     void generate_deserialize_set_element(ostream& out, t_set* tset, string prefix = "");
87     void generate_deserialize_map_element(ostream& out, t_map* tmap, string prefix = "");
88     void generate_deserialize_list_element(ostream& out, t_list* list, string prefix = "");
89     void generate_serialize_field(ostream& out, t_field* tfield, string prefix = "", bool is_element = false, bool is_propertyless = false);
90     void generate_serialize_struct(ostream& out, t_struct* tstruct, string prefix = "");
91     void generate_serialize_container(ostream& out, t_type* ttype, string prefix = "");
92     void generate_serialize_map_element(ostream& out, t_map* tmap, string iter, string map);
93     void generate_serialize_set_element(ostream& out, t_set* tmap, string iter);
94     void generate_serialize_list_element(ostream& out, t_list* tlist, string iter);
95     void generate_netcore_doc(ostream& out, t_field* field);
96     void generate_netcore_doc(ostream& out, t_doc* tdoc);
97     void generate_netcore_doc(ostream& out, t_function* tdoc);
98     void generate_netcore_docstring_comment(ostream& out, string contents);
99     void docstring_comment(ostream& out, const string& comment_start, const string& line_prefix, const string& contents, const string& comment_end);
100     void start_netcore_namespace(ostream& out);
101     void end_netcore_namespace(ostream& out);
102 
103     string netcore_type_usings() const;
104     string netcore_thrift_usings() const;
105 
106     string type_name(t_type* ttype, bool in_countainer = false, bool in_init = false, bool in_param = false, bool is_required = false);
107     string base_type_name(t_base_type* tbase, bool in_container = false, bool in_param = false, bool is_required = false);
108     string declare_field(t_field* tfield, bool init = false, string prefix = "");
109     string function_signature_async(t_function* tfunction, string prefix = "");
110     string function_signature(t_function* tfunction, string prefix = "");
111     string argument_list(t_struct* tstruct);
112     string type_to_enum(t_type* ttype);
113     string prop_name(t_field* tfield, bool suppress_mapping = false);
114     string get_enum_class_name(t_type* type);
115 
116 private:
117     string namespace_name_;
118     string namespace_dir_;
119 
120     bool nullable_;
121     bool union_;
122     bool hashcode_;
123     bool serialize_;
124     bool wcf_;
125 
126     string wcf_namespace_;
127     map<string, int> netcore_keywords;
128     vector<member_mapping_scope> member_mapping_scopes;
129 
130     void init_keywords();
131     string normalize_name(string name);
132     string make_valid_csharp_identifier(string const& fromName);
133     void prepare_member_name_mapping(t_struct* tstruct);
134     void prepare_member_name_mapping(void* scope, const vector<t_field*>& members, const string& structname);
135     void cleanup_member_name_mapping(void* scope);
136     string get_mapped_member_name(string oldname);
137 };
138