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