OpenSDN source code
t_c_generator.cc
Go to the documentation of this file.
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  * Modifications Copyright (c) Juniper Networks, Inc.
24  * Copyright Contributors to the OpenSDN project
25  */
26 
27 #include <fstream>
28 #include <iostream>
29 #include <string>
30 #include <vector>
31 
32 #include <ctype.h>
33 
34 #include "platform.h"
35 #include "t_oop_generator.h"
36 
37 using namespace std;
38 
39 /* forward declarations */
40 string initial_caps_to_underscores(string name);
41 string to_upper_case(string name);
42 string to_lower_case(string name);
43 
48  public:
49 
50  /* constructor */
52  const map<string, string> &parsed_options,
53  const string &option_string) : t_oop_generator(program)
54  {
55  (void) parsed_options;
56  (void) option_string;
57  /* set the output directory */
58  this->out_dir_base_ = "gen-c";
59 
60  /* set the namespace */
61  this->nspace = program_->get_namespace("c");
62 
63  if (this->nspace.empty()) {
64  this->nspace = "";
65  this->nspace_u = "";
66  this->nspace_uc = "";
67  this->nspace_lc = "";
68  } else {
69  /* replace dots with underscores */
70  char *tmp = strdup(this->nspace.c_str());
71  for (unsigned int i = 0; i < strlen(tmp); i++) {
72  if (tmp[i] == '.') {
73  tmp[i] = '_';
74  }
75  }
76  this->nspace = string(tmp, strlen(tmp));
77  free(tmp);
78 
79  /* clean up the namespace for C.
80  * An input of 'namespace foo' should result in:
81  * - nspace = foo - for thrift objects and typedefs
82  * - nspace_u = Foo - for internal GObject prefixes
83  * - nspace_uc = FOO_ - for macro prefixes
84  * - nspace_lc = foo_ - for filename and method prefixes
85  * The underscores are there since uc and lc strings are used as file and
86  * variable prefixes.
87  */
88  this->nspace_u = initial_caps_to_underscores(this->nspace);
89  this->nspace_uc = to_upper_case(this->nspace_u) + "_";
90  this->nspace_lc = to_lower_case(this->nspace_u) + "_";
91  }
92  }
93 
94  /* initialization and destruction */
95  void init_generator();
96  void close_generator();
97 
98  /* generation functions */
99  void generate_typedef(t_typedef *ttypedef);
100  void generate_enum(t_enum *tenum);
101  void generate_consts(vector<t_const *> consts);
102  void generate_struct(t_struct *tstruct);
103  void generate_service(t_service *tservice);
104  void generate_xception(t_struct *tstruct);
105  void generate_sandesh(t_sandesh *tsandesh);
106  void generate_sandesh_info();
107 
108  private:
109 
110  /* file streams */
111  ofstream f_types_;
112  ofstream f_types_impl_;
113  ofstream f_header_;
114  ofstream f_service_;
115 
116  /* namespace variables */
117  string nspace;
118  string nspace_u;
119  string nspace_uc;
120  string nspace_lc;
121 
122  /* helper functions */
123  bool is_complex_type(t_type *ttype);
124  string type_name(t_type* ttype, bool in_typedef=false, bool is_const=false);
125  string base_type_name(t_base_type *type);
126  string type_to_enum(t_type *type);
127  string constant_value(string name, t_type *type, t_const_value *value);
128  string argument_list(t_struct *tstruct);
129  string declare_field(t_field *tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false);
130 
131  /* generation functions */
132  void generate_const_initializer(string name, t_type *type, t_const_value *value);
133  void generate_service_client(t_service *tservice);
134  void generate_service_server(t_service *tservice);
135  void generate_object(t_struct *tstruct);
136  void generate_object(t_sandesh *tsandesh);
137  void generate_object_internal(string name, const vector<t_field *> &members, bool is_sandesh);
138  void generate_struct_writer(ofstream &out, string name, const vector<t_field *> &fields, bool is_sandesh, bool is_function=true);
139  void generate_struct_writer_to_buffer(ofstream &out, string name, const vector<t_field *> &fields, bool is_sandesh, bool is_function=true);
140  void generate_struct_reader(ofstream &out, string name, const vector<t_field *> &fields, bool is_sandesh, bool is_function=true);
141  void generate_struct_reader_from_buffer(ofstream &out, string name, const vector<t_field *> &fields, bool is_sandesh, bool is_function=true);
142  void generate_struct_deleter(ofstream &out, string name, const vector<t_field *> &fields, bool is_sandesh, bool is_function=true);
143 
144  void generate_buffer_bounds_chk(ofstream &out, string length, int error_ret);
145  void generate_buffer_incr_offset(ofstream &out, string length);
146 
147  void generate_write_buffer_memcpy(ofstream &out, string source, string length, bool ref);
148  void generate_write_buffer_memcpy_incr_offset(ofstream &out, string source, string length, bool ref);
149  void generate_write_buffer_chk_memcpy_incr_offset(ofstream &out, string source, string length, int error_ret, bool ref);
150  void generate_write_buffer_binary(ofstream &out, string buf, string buf_len, int error_ret);
151 
152  void generate_struct_begin_writer_to_buffer(ofstream &out, string name, bool is_sandesh, int error_ret);
153  void generate_struct_end_writer_to_buffer(ofstream &out, bool is_sandesh);
154  void generate_field_begin_writer_to_buffer(ofstream &out, string key, string field_type, int error_ret);
155  void generate_field_end_writer_to_buffer(ofstream &out);
156  void generate_field_stop_writer_to_buffer(ofstream &out, int error_ret);
157  void generate_list_begin_writer_to_buffer(ofstream &out, string element_type, string length, int error_ret);
158  void generate_list_end_writer_to_buffer(ofstream &out);
159 
160  void generate_serialize_bool_to_buffer(ofstream &out, string name, int error_ret);
161  void generate_serialize_byte_to_buffer(ofstream &out, string name, int error_ret);
162  void generate_serialize_i16_to_buffer(ofstream &out, string name, int error_ret);
163  void generate_serialize_i32_to_buffer(ofstream &out, string name, int error_ret);
164  void generate_serialize_i64_to_buffer(ofstream &out, string name, int error_ret);
165  void generate_serialize_u16_to_buffer(ofstream &out, string name, int error_ret);
166  void generate_serialize_u32_to_buffer(ofstream &out, string name, int error_ret);
167  void generate_serialize_u64_to_buffer(ofstream &out, string name, int error_ret);
168  void generate_serialize_string_to_buffer(ofstream &out, string name, int error_ret);
169  void generate_serialize_xml_to_buffer(ofstream &out, string name, int error_ret);
170  void generate_serialize_ipaddr_to_buffer(ofstream &out, string name, int error_ret);
171  void generate_serialize_uuid_t_to_buffer(ofstream &out, string name, int error_ret);
172  void generate_serialize_double_to_buffer(ofstream &out, string name, int error_ret);
173 
174  void generate_read_buffer_memcpy(ofstream &out, string dest, string length, bool ref);
175  void generate_read_buffer_memcpy_incr_offset(ofstream &out, string dest, string length, bool ref);
176  void generate_read_buffer_chk_memcpy_incr_offset(ofstream &out, string dest, string length, int error_ret, bool ref);
177 
178  void generate_struct_begin_reader_from_buffer(ofstream &out, string name, bool is_sandesh, int error_ret);
179  void generate_struct_end_reader_from_buffer(ofstream &out, bool is_sandesh);
180  void generate_field_begin_reader_from_buffer(ofstream &out, string field_type, string field_id, int error_ret);
181  void generate_field_end_reader_from_buffer(ofstream &out);
182  void generate_list_begin_reader_from_buffer(ofstream &out, string element_size, int error_ret);
183  void generate_list_end_reader_from_buffer(ofstream &out);
184 
185  void generate_deserialize_bool_from_buffer(ofstream &out, string name, int error_ret);
186  void generate_deserialize_byte_from_buffer(ofstream &out, string name, int error_ret);
187  void generate_deserialize_i16_from_buffer(ofstream &out, string name, int error_ret);
188  void generate_deserialize_i32_from_buffer(ofstream &out, string name, int error_ret);
189  void generate_deserialize_i64_from_buffer(ofstream &out, string name, int error_ret);
190  void generate_deserialize_u16_from_buffer(ofstream &out, string name, int error_ret);
191  void generate_deserialize_u32_from_buffer(ofstream &out, string name, int error_ret);
192  void generate_deserialize_u64_from_buffer(ofstream &out, string name, int error_ret);
193  void generate_deserialize_string_from_buffer(ofstream &out, string name, int error_ret);
194  void generate_deserialize_xml_from_buffer(ofstream &out, string name, int error_ret);
195  void generate_deserialize_ipaddr_from_buffer(ofstream &out, string name, int error_ret);
196  void generate_deserialize_uuid_t_from_buffer(ofstream &out, string name, int error_ret);
197  void generate_deserialize_double_from_buffer(ofstream &out, string name, int error_ret);
198 
199  void generate_serialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret);
200  void generate_serialize_field_to_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret);
201  void generate_serialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret);
202  void generate_serialize_struct_to_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret);
203  void generate_serialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret);
204  void generate_serialize_container_to_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret);
205  void generate_serialize_list_element(ofstream &out, t_list *tlist, string list, string index, int error_ret);
206  void generate_serialize_list_element_to_buffer(ofstream &out, t_list *tlist, string list, string index, int error_ret);
207 
208  void generate_deserialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret);
209  void generate_deserialize_field_from_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret);
210  void generate_deserialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret);
211  void generate_deserialize_struct_from_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret);
212  void generate_deserialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret);
213  void generate_deserialize_container_from_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret);
214  void generate_deserialize_list_element(ofstream &out, t_list *tlist, string prefix, string index, int error_ret);
215  void generate_deserialize_list_element_from_buffer(ofstream &out, t_list *tlist, string prefix, string index, int error_ret);
216 };
217 
223  /* create output directory */
224  MKDIR(get_out_dir().c_str());
225 
226  string program_name_u = initial_caps_to_underscores(program_name_);
227  string program_name_uc = to_upper_case(program_name_u);
228  string program_name_lc = to_lower_case(program_name_u);
229 
230  /* create output files */
231  string f_types_name = get_out_dir() + this->nspace_lc
232  + program_name_lc + "_types.h";
233  f_types_.open(f_types_name.c_str());
234  string f_types_impl_name = get_out_dir() + this->nspace_lc
235  + program_name_lc + "_types.c";
236  f_types_impl_.open(f_types_impl_name.c_str());
237 
238  /* add thrift boilerplate headers */
239  f_types_ << autogen_comment();
240  f_types_impl_ << autogen_comment();
241 
242  /* include inclusion guard */
243  f_types_ <<
244  "#ifndef " << this->nspace_uc << program_name_uc << "_TYPES_H" << endl <<
245  "#define " << this->nspace_uc << program_name_uc << "_TYPES_H" << endl <<
246  endl;
247 
248  /* include C++ compatibility */
249  f_types_ <<
250  "#ifdef __cplusplus" << endl <<
251  "extern \"C\" {" << endl <<
252  "#endif" << endl <<
253  endl;
254 
255  /* include base types */
256  f_types_ <<
257  "/* base includes */" << endl <<
258  "#include \"sandesh/library/c/sandesh.h\"" << endl;
259 
260  /* include other thrift includes */
261  const vector<t_program *> &includes = program_->get_includes();
262  for (size_t i = 0; i < includes.size(); ++i) {
263  f_types_ <<
264  "/* other thrift includes */" << endl <<
265  "#include \"" << this->nspace_lc << includes[i]->get_name() <<
266  "_types.h\"" << endl;
267  }
268  f_types_ << endl;
269 
270  /* include custom headers */
271  const vector<string> &c_includes = program_->get_c_includes();
272  f_types_ << "/* custom thrift includes */" << endl;
273  for (size_t i = 0; i < c_includes.size(); ++i) {
274  if (c_includes[i][0] == '<') {
275  f_types_ <<
276  "#include " << c_includes[i] << endl;
277  } else {
278  f_types_ <<
279  "#include \"" << c_includes[i] << "\"" << endl;
280  }
281  }
282  f_types_ << endl;
283 
284  // include the types file
285  f_types_impl_ <<
286  endl <<
287  "#include \"" << this->nspace_lc << program_name_u <<
288  "_types.h\"" << endl <<
289  endl;
290 
291  f_types_ <<
292  "/* begin types */" << endl << endl;
293 }
294 
299  string program_name_uc = to_upper_case
300  (initial_caps_to_underscores(program_name_));
301 
302  /* end the C++ compatibility */
303  f_types_ <<
304  "#ifdef __cplusplus" << endl <<
305  "}" << endl <<
306  "#endif" << endl <<
307  endl;
308 
309  /* end the header inclusion guard */
310  f_types_ <<
311  "#endif /* " << this->nspace_uc << program_name_uc << "_TYPES_H */" << endl;
312 
313  /* close output file */
314  f_types_.close();
315  f_types_impl_.close();
316 }
317 
328  f_types_ <<
329  indent() << "typedef " << type_name(ttypedef->get_type(), true) <<
330  " " << this->nspace << ttypedef->get_symbolic() << ";" << endl <<
331  endl;
332 }
333 
351  string name = tenum->get_name();
352  string name_uc = to_upper_case(initial_caps_to_underscores(name));
353 
354  f_types_ <<
355  indent() << "enum _" << this->nspace << name << " {" << endl;
356 
357  indent_up();
358 
359  vector<t_enum_value *> constants = tenum->get_constants();
360  vector<t_enum_value *>::iterator c_iter;
361  bool first = true;
362 
363  /* output each of the enumeration elements */
364  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
365  if (first) {
366  first = false;
367  } else {
368  f_types_ << "," << endl;
369  }
370 
371  f_types_ <<
372  indent() << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name();
373  if ((*c_iter)->has_value()) {
374  f_types_ <<
375  " = " << (*c_iter)->get_value();
376  }
377  }
378 
379  indent_down();
380  f_types_ <<
381  endl <<
382  "};" << endl <<
383  "typedef enum _" << this->nspace << name << " " << this->nspace << name << ";" << endl <<
384  endl;
385 }
386 
390 void t_c_generator::generate_consts (vector<t_const *> consts) {
391  f_types_ << "/* constants */" << endl;
392  f_types_impl_ << "/* constants */" << endl;
393 
394  vector<t_const *>::iterator c_iter;
395  for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
396  string name = (*c_iter)->get_name();
397  string name_uc = to_upper_case(name);
398  string name_lc = to_lower_case(name);
399  t_type *type = (*c_iter)->get_type();
400  t_const_value *value = (*c_iter)->get_value();
401 
402  f_types_ <<
403  indent() << "#define " << this->nspace_uc << name_uc << " " <<
404  constant_value (name_lc, type, value) << endl;
405 
406  generate_const_initializer (name_lc, type, value);
407  }
408 
409  f_types_ << endl;
410  f_types_impl_ << endl;
411 }
412 
433  f_types_ << "/* struct " << tstruct->get_name() << " */" << endl;
434  generate_object(tstruct);
435 }
436 
441  f_types_ << "/* sandesh " << tsandesh->get_name() << " */" << endl;
442  generate_object(tsandesh);
443 }
444 
449  string program_name_u = initial_caps_to_underscores(program_name_);
450  string program_name_lc = to_lower_case(program_name_u);
451  ofstream &out = f_types_impl_;
452  out << endl <<
453  "static sandesh_info_t " << program_name_lc << "_infos[] = {" << endl;
454  vector<t_sandesh*> sandeshs = program_->get_sandeshs();
455  vector<t_sandesh*>::iterator s_iter;
456  indent_up();
457  for (s_iter = sandeshs.begin(); s_iter != sandeshs.end(); ++s_iter) {
458  string sandesh_name = get_sandesh_name(*s_iter);
459  string sandesh_name_u = initial_caps_to_underscores(sandesh_name);
460  out << indent() << "{" << endl;
461  indent_up();
462  out <<
463  indent() << ".name = \"" << sandesh_name << "\"," << endl <<
464  indent() << ".size = sizeof(" << sandesh_name << ")," << endl <<
465  indent() << ".read = " << sandesh_name_u << "_read," << endl <<
466  indent() << ".read_binary_from_buffer = " << sandesh_name_u << "_read_binary_from_buffer," << endl <<
467  indent() << ".write = " << sandesh_name_u << "_write," << endl <<
468  indent() << ".write_binary_to_buffer = " << sandesh_name_u << "_write_binary_to_buffer," << endl <<
469  indent() << ".process = " << sandesh_name_u << "_process," << endl <<
470  indent() << ".free = " << sandesh_name_u << "_free," << endl;
471  indent_down();
472  out << indent() << "}," << endl;
473  }
474  // Add end of array marker
475  out << indent() << "{" << endl;
476  indent_up();
477  out <<
478  indent() << ".name = NULL," << endl <<
479  indent() << ".size = 0," << endl <<
480  indent() << ".read = NULL," << endl <<
481  indent() << ".read_binary_from_buffer = NULL," << endl <<
482  indent() << ".write = NULL," << endl <<
483  indent() << ".write_binary_to_buffer = NULL," << endl <<
484  indent() << ".process = NULL," << endl <<
485  indent() << ".free = NULL," << endl;
486  indent_down();
487  out << indent() << "}, " << endl;
488  indent_down();
489  out << "};" << endl << endl;
490 
491  // Generate the extract function
492  out << "sandesh_info_t *" << endl
493  << program_name_lc <<
494  "_find_sandesh_info (const char *sname)" << endl;
495  out << "{" << endl;
496  indent_up();
497  out << indent() << "return sandesh_find_info(" << program_name_lc <<
498  "_infos, sname);" << endl;
499  indent_down();
500  out << "}" << endl << endl;
501 
502  // Generate the extract function declaration
503  f_types_ << endl <<
504  "sandesh_info_t * " << program_name_lc <<
505  "_find_sandesh_info(const char *sname);" << endl;
506 }
507 
513  // NOT IMPLEMENTED
514 }
515 
520  // NOT IMPLEMENTED
521 }
522 
523 /********************
524  * HELPER FUNCTIONS *
525  ********************/
526 
531  ttype = get_true_type (ttype);
532 
533  return ttype->is_container()
534  || ttype->is_struct()
535  || ttype->is_xception()
536  || (ttype->is_base_type()
537  && (((t_base_type *) ttype)->get_base()
539  ((t_base_type *) ttype)->get_base()
540  == t_base_type::TYPE_XML));
541 }
542 
543 
547 string t_c_generator::type_name (t_type* ttype, bool in_typedef, bool is_const) {
548  (void) in_typedef;
549  if (ttype->is_base_type()) {
550  string bname = base_type_name ((t_base_type *) ttype);
551 
552  if (is_const) {
553  return "const " + bname;
554  } else {
555  return bname;
556  }
557  }
558 
559  if (ttype->is_container()) {
560  string cname;
561 
562  t_container *tcontainer = (t_container *) ttype;
563  if (tcontainer->has_cpp_name()) {
564  cname = tcontainer->get_cpp_name();
565  } else if (ttype->is_map()) {
566  throw "compiler error: no C support for map";
567  } else if (ttype->is_set()) {
568  throw "compiler error: no C support for set";
569  } else if (ttype->is_list()) {
570  t_type *etype = ((t_list *) ttype)->get_elem_type();
571  if (etype->is_base_type()) {
572  cname = base_type_name((t_base_type *) etype) + " *";
573  } else {
574  cname = type_name(etype, in_typedef, is_const);
575  }
576  }
577 
578  if (is_const) {
579  return "const " + cname;
580  } else {
581  return cname;
582  }
583  }
584 
585  // check for a namespace
586  string pname = this->nspace + ttype->get_name();
587 
588  if (is_complex_type (ttype)) {
589  pname += " *";
590  }
591 
592  if (is_const) {
593  return "const " + pname;
594  } else {
595  return pname;
596  }
597 }
598 
603  t_base_type::t_base tbase = type->get_base();
604 
605  switch (tbase) {
607  return "void";
609  if (type->is_binary()) {
610  return "u_int8_t *";
611  } else {
612  return "char *";
613  }
614  case t_base_type::TYPE_XML:
615  return "char *";
617  return "u_int8_t";
619  return "int8_t";
621  return "int16_t";
623  return "int32_t";
625  return "int64_t";
626  case t_base_type::TYPE_U16:
627  return "uint16_t";
628  case t_base_type::TYPE_U32:
629  return "uint32_t";
630  case t_base_type::TYPE_U64:
631  return "uint64_t";
633  return "double";
634  case t_base_type::TYPE_IPV4:
635  return "uint32_t";
636  case t_base_type::TYPE_IPADDR:
637  return "ipaddr_t";
638  case t_base_type::TYPE_UUID:
639  return "ct_uuid_t";
640  default:
641  throw "compiler error: no C base type name for base type "
642  + t_base_type::t_base_name (tbase);
643  }
644 }
645 
651  type = get_true_type (type);
652 
653  if (type->is_base_type()) {
654  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
655 
656  switch (tbase) {
658  throw "NO T_VOID CONSTRUCT";
660  case t_base_type::TYPE_STATIC_CONST_STRING:
661  case t_base_type::TYPE_SANDESH_SYSTEM:
662  case t_base_type::TYPE_SANDESH_REQUEST:
663  case t_base_type::TYPE_SANDESH_RESPONSE:
664  case t_base_type::TYPE_SANDESH_TRACE:
665  case t_base_type::TYPE_SANDESH_TRACE_OBJECT:
666  case t_base_type::TYPE_SANDESH_BUFFER:
667  case t_base_type::TYPE_SANDESH_UVE:
668  case t_base_type::TYPE_SANDESH_OBJECT:
669  case t_base_type::TYPE_SANDESH_FLOW:
670  return "T_STRING";
672  return "T_BOOL";
674  return "T_BYTE";
676  return "T_I16";
678  return "T_I32";
680  return "T_I64";
681  case t_base_type::TYPE_U16:
682  return "T_U16";
683  case t_base_type::TYPE_U32:
684  return "T_U32";
685  case t_base_type::TYPE_U64:
686  return "T_U64";
687  case t_base_type::TYPE_XML:
688  return "T_XML";
690  return "T_DOUBLE";
691  case t_base_type::TYPE_IPV4:
692  return "T_IPV4";
693  case t_base_type::TYPE_IPADDR:
694  return "T_IPADDR";
695  case t_base_type::TYPE_UUID:
696  return "T_UUID";
697  }
698  } else if (type->is_enum()) {
699  return "T_I32";
700  } else if (type->is_struct()) {
701  return "T_STRUCT";
702  } else if (type->is_xception()) {
703  return "T_STRUCT";
704  } else if (type->is_map()) {
705  return "T_MAP";
706  } else if (type->is_set()) {
707  return "T_SET";
708  } else if (type->is_list()) {
709  return "T_LIST";
710  }
711 
712  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
713 }
714 
715 
720  ostringstream render;
721 
722  if (type->is_base_type()) {
723  /* primitives */
724  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
725  switch (tbase) {
727  case t_base_type::TYPE_XML:
728  render << "\"" + value->get_string() + "\"";
729  break;
731  render << ((value->get_integer() != 0) ? 1 : 0);
732  break;
737  case t_base_type::TYPE_U16:
738  case t_base_type::TYPE_U32:
739  case t_base_type::TYPE_U64:
740  case t_base_type::TYPE_IPV4:
741  render << value->get_integer();
742  break;
743  case t_base_type::TYPE_UUID:
744  render << value->get_uuid(); // Need to fix later, c structure is not getting initialize.
745  break;
747  if (value->get_type() == t_const_value::CV_INTEGER) {
748  render << value->get_integer();
749  } else {
750  render << value->get_double();
751  }
752  break;
753  default:
754  throw "compiler error: no const of base type "
755  + t_base_type::t_base_name (tbase);
756  }
757  } else if (type->is_enum()) {
758  render << "(" << type_name (type) << ")" << value->get_integer();
759  } else if (type->is_struct() || type->is_xception() || type->is_list()
760  || type->is_set() || type->is_map()) {
761  render << "(" << this->nspace_lc <<
762  to_lower_case(name) << "_constant())";
763  } else {
764  render << "NULL /* not supported */";
765  }
766 
767  return render.str();
768 }
769 
777  string result = "";
778 
779  const vector<t_field*>& fields = tstruct->get_members();
780  vector<t_field*>::const_iterator f_iter;
781  bool first = true;
782  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
783  if (first) {
784  first = false;
785  } else {
786  result += ", ";
787  }
788  result += type_name((*f_iter)->get_type(), false, true) + " " +
789  (*f_iter)->get_name();
790  }
791  return result;
792 }
793 
798  bool init,
799  bool pointer,
800  bool constant,
801  bool reference) {
802  string result = "";
803  if (constant) {
804  result += "const ";
805  }
806  result += type_name(tfield->get_type());
807  if (pointer) {
808  result += "*";
809  }
810  if (reference) {
811  result += "*";
812  }
813  result += " " + tfield->get_name();
814  if (init) {
815  t_type* type = get_true_type(tfield->get_type());
816 
817  if (type->is_base_type()) {
818  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
819  switch (tbase) {
821  break;
827  case t_base_type::TYPE_U16:
828  case t_base_type::TYPE_U32:
829  case t_base_type::TYPE_U64:
830  case t_base_type::TYPE_IPV4:
831  result += " = 0";
832  break;
834  result += " = (gdouble) 0";
835  break;
837  case t_base_type::TYPE_XML:
838  result += " = NULL";
839  break;
840  case t_base_type::TYPE_UUID:
841  result += "{0}";
842  break;
843  default:
844  throw "compiler error: no C intializer for base type "
845  + t_base_type::t_base_name (tbase);
846  }
847  } else if (type->is_enum()) {
848  result += " = (" + type_name (type) + ") 0";
849  }
850  }
851 
852  if (!reference) {
853  result += ";";
854  }
855 
856  return result;
857 }
858 
863  string name_u = initial_caps_to_underscores(name);
864  string name_lc = to_lower_case(name_u);
865  string type_u = initial_caps_to_underscores(type->get_name());
866  string type_uc = to_upper_case(type_u);
867 
868  if (type->is_struct() || type->is_xception()) {
869  throw "compiler error: no support for const struct or exception";
870  } else if (type->is_list()) {
871  throw "compiler error: no support for const array";
872  } else if (type->is_set()) {
873  throw "compiler error: no support for const set";
874  } else if (type->is_map()) {
875  throw "compiler error: no support for const map";
876  }
877 }
878 
883  // NOT IMPLEMENTED
884 }
885 
890  // NOT IMPLEMENTED
891 }
892 
897  string name = tsandesh->get_name();
898  generate_object_internal(name, tsandesh->get_members(), true);
899 }
900 
905  string name = tstruct->get_name();
906  generate_object_internal(name, tstruct->get_members(), false);
907 }
908 
913  const vector<t_field *> &members,
914  bool is_sandesh) {
915  string name_u = initial_caps_to_underscores(name);
916  string name_uc = to_upper_case(name_u);
917  string dtname = is_sandesh ? "void" : name;
918  string dvname = is_sandesh ? "sandesh" : name_u;
919 
920  // write the instance definition
921  f_types_ <<
922  "struct _" << this->nspace << name << endl <<
923  "{ " << endl <<
924  " /* public */" << endl;
925 
926  // for each field, add a member variable
927  vector<t_field *>::const_iterator m_iter;
928  for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
929  // Ignore auto generated fields
930  if ((*m_iter)->get_auto_generated()) {
931  continue;
932  }
933  t_type *t = get_true_type ((*m_iter)->get_type());
934  f_types_ <<
935  " " << type_name (t) << " " << (*m_iter)->get_name() << ";" << endl;
936  if (t->is_list()) {
937  // Auto-generate size for the list
938  f_types_ << " " << "u_int32_t " << (*m_iter)->get_name() << "_size;"
939  << endl;
940  }
941  if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
942  f_types_ <<
943  " u_int8_t __isset_" << (*m_iter)->get_name() << ";" << endl;
944  }
945  }
946 
947  // close the structure definition and create a typedef
948  f_types_ <<
949  "};" << endl <<
950  "typedef struct _" << this->nspace << name << " " <<
951  this->nspace << name << ";" << endl <<
952  endl;
953 
954  // prototypes for struct I/O methods
955  f_types_ <<
956  "int32_t " <<
957  this->nspace_lc << name_u <<
958  "_write (" << dtname <<
959  "* w" << dvname << ", ThriftProtocol *protocol, int *error);" << endl;
960  f_types_ <<
961  "int32_t " <<
962  this->nspace_lc << name_u <<
963  "_write_binary_to_buffer (" << dtname <<
964  "* w" << dvname << ", uint8_t *buf, const size_t buf_len, int *error);" << endl;
965  f_types_ <<
966  "int32_t " <<
967  this->nspace_lc << name_u <<
968  "_read (" << dtname <<
969  "* r" << dvname << ", ThriftProtocol *protocol, int *error);" << endl;
970  f_types_ <<
971  "int32_t " <<
972  this->nspace_lc << name_u <<
973  "_read_binary_from_buffer (" << dtname <<
974  "* r" << dvname << ", uint8_t *buf, const size_t buf_len, int *error);" << endl;
975  f_types_ <<
976  "void " <<
977  this->nspace_lc << name_u <<
978  "_free(" << dtname <<
979  "* f" << dvname << ");" << endl;
980  if (is_sandesh) {
981  f_types_ <<
982  "void " <<
983  this->nspace_lc << name_u <<
984  "_process (void *p" << name_u << ");" << endl;
985  }
986 
987  // start writing the object implementation .c file
988  // generate struct I/O methods
989  generate_struct_reader (f_types_impl_, name, members, is_sandesh);
990  generate_struct_reader_from_buffer (f_types_impl_, name, members, is_sandesh);
991  generate_struct_writer (f_types_impl_, name, members, is_sandesh);
992  generate_struct_writer_to_buffer (f_types_impl_, name, members, is_sandesh);
993  generate_struct_deleter (f_types_impl_, name, members, is_sandesh);
994 }
995 
1000  string name,
1001  const vector<t_field *> &fields,
1002  bool is_sandesh,
1003  bool is_function) {
1004  string name_u = initial_caps_to_underscores(name);
1005  string fname = "f" + name_u;
1006  string sname = is_sandesh ? "sandesh" : "struct";
1007  string dname = is_sandesh ? "void" : name;
1008  string dfname = is_sandesh ? "fsandesh" : fname;
1009 
1010  vector <t_field *>::const_iterator f_iter;
1011 
1012  if (is_function) {
1013  indent(out) <<
1014  "void" << endl <<
1015  this->nspace_lc << name_u <<
1016  "_free (" << dname <<
1017  "* " << dfname << ")" << endl;
1018  }
1019  indent(out) << "{" << endl;
1020  indent_up();
1021 
1022  if (is_sandesh) {
1023  out << indent() << name << "* " << fname << " = " <<
1024  "(" << name << "* ) " << dfname << ";" <<
1025  endl << endl;
1026  }
1027 
1028  // satisfy -Wall in case we don't use some variables
1029  out <<
1030  indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
1031  indent() << "THRIFT_UNUSED_VAR (" << fname << ");" << endl;
1032 
1033  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1034  // Ignore auto generated fields
1035  if ((*f_iter)->get_auto_generated()) {
1036  continue;
1037  }
1038  t_type *type = get_true_type ((*f_iter)->get_type());
1039  string fvname = fname + "->" + (*f_iter)->get_name();
1040  if (type->is_struct() || type->is_xception()) {
1041  indent(out) << "if (" << fvname << ") {" << endl;
1042  indent_up();
1043  indent(out) << ((t_struct *)type)->get_name() << "_free(" <<
1044  fvname << ");" << endl;
1045  indent(out) << "os_free(" << fvname << ");" << endl;
1046  indent(out) << fvname << " = NULL;" << endl;
1047  scope_down(out);
1048  } else if (type->is_container()) {
1049  indent(out) << "if (" << fvname << ") {" << endl;
1050  indent_up();
1051  indent(out) << "os_free(" << fvname << ");" << endl;
1052  indent(out) << fvname << "_size = 0;" << endl;
1053  indent(out) << fvname << " = NULL;" << endl;
1054  scope_down(out);
1055  } else if (type->is_base_type()) {
1056  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
1057  switch (tbase) {
1059  case t_base_type::TYPE_XML:
1060  indent(out) << "if (" << fvname << ") {" << endl;
1061  indent_up();
1062  indent(out) << "os_free(" << fvname << ");" << endl;
1063  indent(out) << fvname << " = NULL;" << endl;
1064  scope_down(out);
1065  break;
1066 
1067  default:
1068  break;
1069  }
1070  }
1071  }
1072  indent_down();
1073  indent(out) <<
1074  "}" << endl <<
1075  endl;
1076 }
1077 
1079  string length,
1080  int error_ret) {
1081  out <<
1082  indent() << "if (Xbuflen < Xoffset + " << length << ") {" << endl <<
1083  indent() << " return " << error_ret << ";" << endl <<
1084  indent() << "}" << endl;
1085 }
1086 
1088  string length) {
1089  out <<
1090  indent() << "Xoffset += " << length << ";" << endl;
1091 }
1092 
1094  string source,
1095  string length,
1096  bool ref) {
1097  if (ref) {
1098  out <<
1099  indent() << "memcpy (Xbuf + Xoffset, &" << source << ", " <<
1100  length << ");" << endl;
1101  } else {
1102  out <<
1103  indent() << "memcpy (Xbuf + Xoffset, " << source << ", " <<
1104  length << ");" << endl;
1105  }
1106 }
1107 
1109  string dest,
1110  string length,
1111  bool ref) {
1112  if (ref) {
1113  out <<
1114  indent() << "memcpy (&" << dest << ", Xbuf + Xoffset, " <<
1115  length << ");" << endl;
1116  } else {
1117  out <<
1118  indent() << "memcpy (" << dest << ", Xbuf + Xoffset, " <<
1119  length << ");" << endl;
1120  }
1121 }
1122 
1124  string source,
1125  string length,
1126  bool ref) {
1127  generate_write_buffer_memcpy (out, source, length, ref);
1128  generate_buffer_incr_offset (out, length);
1129 }
1130 
1132  string dest,
1133  string length,
1134  bool ref) {
1135  generate_read_buffer_memcpy (out, dest, length, ref);
1136  generate_buffer_incr_offset (out, length);
1137 }
1138 
1140  string source,
1141  string length,
1142  int error_ret,
1143  bool ref) {
1144  generate_buffer_bounds_chk (out, length, error_ret);
1145  generate_write_buffer_memcpy_incr_offset (out, source, length, ref);
1146 }
1147 
1149  string dest,
1150  string length,
1151  int error_ret,
1152  bool ref) {
1153  generate_buffer_bounds_chk (out, length, error_ret);
1154  generate_read_buffer_memcpy_incr_offset (out, dest, length, ref);
1155 }
1156 
1157 void t_c_generator::generate_write_buffer_binary(ofstream &out, string buf,
1158  string buf_len, int error_ret) {
1159  string nbuf_len(tmp("Xnbuflen"));
1160  generate_buffer_bounds_chk (out, "4", error_ret);
1161  scope_up(out);
1162  out <<
1163  indent() << "int32_t " << nbuf_len << " = htonl (" <<
1164  buf_len << ");" << endl;
1165  generate_write_buffer_memcpy_incr_offset (out, nbuf_len, "4", true);
1166  scope_down(out);
1167  generate_write_buffer_chk_memcpy_incr_offset (out, buf, buf_len, error_ret,
1168  false);
1169 }
1170 
1172  string name,
1173  bool is_sandesh,
1174  int error_ret) {
1175  if (is_sandesh) {
1176  ostringstream os;
1177  os << name.length();
1178  string name_len(os.str());
1179  out <<
1180  indent() << "/* thrift_protocol_write_sandesh_begin */" << endl;
1181  generate_write_buffer_binary (out, "\"" + name + "\"", name_len,
1182  error_ret);
1183  } else {
1184  out << indent() << "/* thrift_protocol_write_struct_begin */" << endl;
1185  }
1186 }
1187 
1189  string name,
1190  bool is_sandesh,
1191  int error_ret) {
1192  if (is_sandesh) {
1193  out <<
1194  indent() << "/* thrift_protocol_read_sandesh_begin */" << endl;
1195  generate_deserialize_string_from_buffer (out, name, error_ret);
1196  out <<
1197  indent() << "if (" << name << ") { os_free (" << name << "); " <<
1198  name << " = NULL; }" << endl << endl;
1199  } else {
1200  out << indent() << "/* thrift_protocol_read_struct_begin */" << endl;
1201  }
1202 }
1203 
1205  bool is_sandesh) {
1206  if (is_sandesh) {
1207  out << indent() << "/* thrift_protocol_write_sandesh_send */" << endl;
1208  } else {
1209  out << indent() << "/* thrift_protocol_write_struct_send */" << endl;
1210  }
1211 }
1212 
1214  bool is_sandesh) {
1215  if (is_sandesh) {
1216  out << indent() << "/* thrift_protocol_read_sandesh_send */" << endl;
1217  } else {
1218  out << indent() << "/* thrift_protocol_read_struct_send */" << endl;
1219  }
1220 }
1221 
1223  int error_ret) {
1224  string field_stop(tmp("Xfield_stop"));
1225  out << endl <<
1226  indent() << "/* thrift_protocol_field_stop */" << endl;
1227  generate_buffer_bounds_chk (out, "1", error_ret);
1228  scope_up(out);
1229  out <<
1230  indent() << "int8_t " << field_stop << " = (int8_t) T_STOP;" << endl;
1231  generate_write_buffer_memcpy_incr_offset (out, field_stop, "1", true);
1232  scope_down(out);
1233 }
1234 
1236  string name,
1237  const vector<t_field *> &fields,
1238  bool is_sandesh,
1239  bool is_function) {
1240  string name_u = initial_caps_to_underscores(name);
1241  string wname = "w" + name_u;
1242  string sname = is_sandesh ? "sandesh" : "struct";
1243  string dname = is_sandesh ? "void" : name;
1244  string dwname = is_sandesh ? "wsandesh" : wname;
1245 
1246  vector <t_field *>::const_iterator f_iter;
1247  int error_ret = 0;
1248 
1249  if (is_function) {
1250  error_ret = -1;
1251  indent(out) <<
1252  "int32_t" << endl <<
1253  this->nspace_lc << name_u << "_write_binary_to_buffer (" <<
1254  dname << "* " << dwname <<
1255  ", uint8_t *Xbuf, const size_t Xbuflen, int *Xerror)" << endl;
1256  }
1257  indent(out) << "{" << endl;
1258  indent_up();
1259 
1260  out <<
1261  indent() << "int32_t Xret;" << endl <<
1262  indent() << "int32_t Xoffset = 0;" << endl <<
1263  indent() << "u_int32_t Xi;" << endl <<
1264  endl;
1265 
1266  if (is_sandesh) {
1267  out << indent() << name << "* " << wname << " = " <<
1268  "(" << name << "* ) " << dwname << ";" <<
1269  endl << endl;
1270  }
1271 
1272  // satisfy -Wall in case we don't use some variables
1273  out <<
1274  indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
1275  indent() << "THRIFT_UNUSED_VAR (Xret);" << endl <<
1276  indent() << "THRIFT_UNUSED_VAR (Xi);" << endl <<
1277  indent() << "THRIFT_UNUSED_VAR (" << wname << ");" << endl;
1278 
1279  out << endl;
1280 
1281  generate_struct_begin_writer_to_buffer(out, name, is_sandesh, error_ret);
1282 
1283  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1284  // Ignore auto generated fields
1285  if ((*f_iter)->get_auto_generated()) {
1286  continue;
1287  }
1288  if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
1289  indent(out) << "if (" << wname << "->__isset_" << (*f_iter)->get_name() << " == 1) {" << endl;
1290  indent_up();
1291  }
1292  ostringstream key_os;
1293  key_os << (*f_iter)->get_key();
1294  string key(key_os.str());
1295  string tenum(type_to_enum((*f_iter)->get_type()));
1296  generate_field_begin_writer_to_buffer (out, key, tenum, error_ret);
1297  generate_serialize_field_to_buffer (out, *f_iter, wname + "->", "", error_ret);
1298  generate_field_end_writer_to_buffer (out);
1299  if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
1300  indent(out) << "if (" << wname << "->__isset_" << (*f_iter)->get_name() << " == 1) {" << endl;
1301  indent_up();
1302  }
1303  }
1304  generate_field_stop_writer_to_buffer(out, error_ret);
1305  generate_struct_end_writer_to_buffer(out, is_sandesh);
1306  if (is_function) {
1307  indent(out) << "return Xoffset;" << endl;
1308  }
1309 
1310  indent_down();
1311  indent(out) <<
1312  "}" << endl <<
1313  endl;
1314 }
1315 
1320  string name,
1321  const vector<t_field *> &fields,
1322  bool is_sandesh,
1323  bool is_function) {
1324  string name_u = initial_caps_to_underscores(name);
1325  string wname = "w" + name_u;
1326  string sname = is_sandesh ? "sandesh" : "struct";
1327  string dname = is_sandesh ? "void" : name;
1328  string dwname = is_sandesh ? "wsandesh" : wname;
1329 
1330  vector <t_field *>::const_iterator f_iter;
1331  int error_ret = 0;
1332 
1333  if (is_function) {
1334  error_ret = -1;
1335  indent(out) <<
1336  "int32_t" << endl <<
1337  this->nspace_lc << name_u <<
1338  "_write (" << dname <<
1339  "* " << dwname << ", ThriftProtocol *protocol, int *error)" << endl;
1340  }
1341  indent(out) << "{" << endl;
1342  indent_up();
1343 
1344  out <<
1345  indent() << "int32_t ret;" << endl <<
1346  indent() << "int32_t xfer = 0;" << endl <<
1347  indent() << "u_int32_t i;" << endl <<
1348  endl;
1349 
1350  if (is_sandesh) {
1351  out << indent() << name << "* " << wname << " = " <<
1352  "(" << name << "* ) " << dwname << ";" <<
1353  endl << endl;
1354  }
1355 
1356  // satisfy -Wall in case we don't use some variables
1357  out <<
1358  indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
1359  indent() << "THRIFT_UNUSED_VAR (i);" << endl <<
1360  indent() << "THRIFT_UNUSED_VAR (" << wname << ");" << endl;
1361 
1362  out << endl;
1363 
1364  out <<
1365  indent() << "if ((ret = thrift_protocol_write_" << sname << "_begin (protocol, \"" << name << "\", error)) < 0)" << endl <<
1366  indent() << " return " << error_ret << ";" << endl <<
1367  indent() << "xfer += ret;" << endl;
1368 
1369  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1370  // Ignore auto generated fields
1371  if ((*f_iter)->get_auto_generated()) {
1372  continue;
1373  }
1374  if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
1375  indent(out) << "if (" << wname << "->__isset_" << (*f_iter)->get_name() << " == 1) {" << endl;
1376  indent_up();
1377  }
1378 
1379  out <<
1380  indent() << "if ((ret = thrift_protocol_write_field_begin (protocol, " <<
1381  "\"" << (*f_iter)->get_name() << "\", " <<
1382  type_to_enum ((*f_iter)->get_type()) << ", " <<
1383  (*f_iter)->get_key() << ", error)) < 0)" << endl <<
1384  indent() << " return " << error_ret << ";" << endl <<
1385  indent() << "xfer += ret;" << endl;
1386  generate_serialize_field (out, *f_iter, wname + "->", "", error_ret);
1387  out <<
1388  indent() << "xfer += ret;" << endl;
1389  out <<
1390  indent() << "if ((ret = thrift_protocol_write_field_end (protocol, error)) < 0)" << endl <<
1391  indent() << " return " << error_ret << ";" << endl <<
1392  indent() << "xfer += ret;" << endl;
1393 
1394  if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
1395  indent_down();
1396  indent(out) << "}" << endl;
1397  }
1398  }
1399 
1400  // write the struct map
1401  out <<
1402  indent() << "if ((ret = thrift_protocol_write_field_stop (protocol, error)) < 0)" << endl <<
1403  indent() << " return " << error_ret << ";" << endl <<
1404  indent() << "xfer += ret;" << endl <<
1405  indent() << "if ((ret = thrift_protocol_write_" << sname << "_end (protocol, error)) < 0)" << endl <<
1406  indent() << " return " << error_ret << ";" << endl <<
1407  indent() << "xfer += ret;" << endl <<
1408  endl;
1409 
1410  if (is_function) {
1411  indent(out) << "return xfer;" << endl;
1412  }
1413 
1414  indent_down();
1415  indent(out) <<
1416  "}" << endl <<
1417  endl;
1418 }
1419 
1424  string name,
1425  const vector <t_field *> &fields,
1426  bool is_sandesh,
1427  bool is_function) {
1428  string name_u = initial_caps_to_underscores(name);
1429  string rname = "r" + name_u;
1430  int error_ret = 0;
1431  vector <t_field *>::const_iterator f_iter;
1432  string sname = is_sandesh ? "sandesh" : "struct";
1433  string dname = is_sandesh ? "void" : name;
1434  string drname = is_sandesh ? "rsandesh" : rname;
1435 
1436  if (is_function) {
1437  error_ret = -1;
1438  indent(out) <<
1439  "/* reads a " << name_u << " " << sname << " */" << endl <<
1440  "int32_t" << endl <<
1441  this->nspace_lc << name_u <<
1442  "_read (" << dname <<
1443  " *" << drname << ", ThriftProtocol *protocol, int *error)" << endl;
1444  }
1445 
1446  indent(out) << "{" << endl;
1447  indent_up();
1448 
1449  // declare stack temp variables
1450  out <<
1451  indent() << "int32_t ret;" << endl <<
1452  indent() << "int32_t xfer = 0;" << endl <<
1453  indent() << "char *name;" << endl <<
1454  indent() << "ThriftType ftype;" << endl <<
1455  indent() << "int16_t fid;" << endl <<
1456  indent() << "u_int32_t len = 0, i;" << endl <<
1457  indent() << "void *data = NULL;" << endl;
1458 
1459  if (is_sandesh) {
1460  out << indent() << name << "* " << rname << " = " <<
1461  "(" << name << "* ) " << drname << ";" << endl;
1462  }
1463 
1464  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1465  // Ignore auto generated fields
1466  if ((*f_iter)->get_auto_generated()) {
1467  continue;
1468  }
1469  if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
1470  indent(out) << "u_int8_t isset_" << (*f_iter)->get_name() << " = 0;" << endl;
1471  }
1472  }
1473 
1474  // satisfy -Wall in case we don't use some variables
1475  out <<
1476  indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
1477  indent() << "THRIFT_UNUSED_VAR (len);" << endl <<
1478  indent() << "THRIFT_UNUSED_VAR (data);" << endl <<
1479  indent() << "THRIFT_UNUSED_VAR (i);" << endl <<
1480  indent() << "THRIFT_UNUSED_VAR (" << rname << ");" << endl;
1481 
1482  out << endl;
1483 
1484  // read the beginning of the structure marker
1485  out <<
1486  indent() << "/* read the " << sname << " begin marker */" << endl <<
1487  indent() << "if ((ret = thrift_protocol_read_" << sname << "_begin (protocol, &name, error)) < 0)" << endl <<
1488  indent() << "{" << endl <<
1489  indent() << " if (name) os_free (name);" << endl <<
1490  indent() << " return " << error_ret << ";" << endl <<
1491  indent() << "}" << endl <<
1492  indent() << "xfer += ret;" << endl <<
1493  indent() << "if (name) { os_free (name); name = NULL; }" << endl <<
1494  endl;
1495 
1496  // read the struct fields
1497  out <<
1498  indent() << "/* read the struct fields */" << endl <<
1499  indent() << "while (1)" << endl;
1500  scope_up(out);
1501 
1502  // read beginning field marker
1503  out <<
1504  indent() << "/* read the beginning of a field */" << endl <<
1505  indent() << "if ((ret = thrift_protocol_read_field_begin (protocol, &name, &ftype, &fid, error)) < 0)" << endl <<
1506  indent() << "{" << endl <<
1507  indent() << " if (name) os_free (name);" << endl <<
1508  indent() << " return " << error_ret << ";" << endl <<
1509  indent() << "}" << endl <<
1510  indent() << "xfer += ret;" << endl <<
1511  indent() << "if (name) { os_free (name); name = NULL; }" << endl <<
1512  endl;
1513 
1514  // check for field STOP marker
1515  out <<
1516  indent() << "/* break if we get a STOP field */" << endl <<
1517  indent() << "if (ftype == T_STOP)" << endl <<
1518  indent() << "{" << endl <<
1519  indent() << " break;" << endl <<
1520  indent() << "}" << endl <<
1521  endl;
1522 
1523  // switch depending on the field type
1524  indent(out) <<
1525  "switch (fid)" << endl;
1526 
1527  // start switch
1528  scope_up(out);
1529 
1530  // generate deserialization code for known types
1531  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1532  // Ignore auto generated fields
1533  if ((*f_iter)->get_auto_generated()) {
1534  continue;
1535  }
1536  indent(out) <<
1537  "case " << (*f_iter)->get_key() << ":" << endl;
1538  indent_up();
1539  indent(out) <<
1540  "if (ftype == " << type_to_enum ((*f_iter)->get_type()) << ")" << endl;
1541  indent(out) <<
1542  "{" << endl;
1543 
1544 
1545  indent_up();
1546  // generate deserialize field
1547  generate_deserialize_field (out, *f_iter, rname + "->", "", error_ret);
1548  indent_down();
1549 
1550  out <<
1551  indent() << "} else {" << endl <<
1552  indent() << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl <<
1553  indent() << " return " << error_ret << ";" << endl <<
1554  indent() << " xfer += ret;" << endl <<
1555  indent() << "}" << endl <<
1556  indent() << "break;" << endl;
1557  indent_down();
1558  }
1559 
1560  // create the default case
1561  out <<
1562  indent() << "default:" << endl <<
1563  indent() << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl <<
1564  indent() << " return " << error_ret << ";" << endl <<
1565  indent() << " xfer += ret;" << endl <<
1566  indent() << " break;" << endl;
1567 
1568  // end switch
1569  scope_down(out);
1570 
1571  // read field end marker
1572  out <<
1573  indent() << "if ((ret = thrift_protocol_read_field_end (protocol, error)) < 0)" << endl <<
1574  indent() << " return " << error_ret << ";" << endl <<
1575  indent() << "xfer += ret;" << endl;
1576 
1577  // end while loop
1578  scope_down(out);
1579  out << endl;
1580 
1581  // read the end of the structure
1582  out <<
1583  indent() << "if ((ret = thrift_protocol_read_" << sname << "_end (protocol, error)) < 0)" << endl <<
1584  indent() << " return " << error_ret << ";" << endl <<
1585  indent() << "xfer += ret;" << endl <<
1586  endl;
1587 
1588  // if a required field is missing, throw an error
1589  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1590  // Ignore auto generated fields
1591  if ((*f_iter)->get_auto_generated()) {
1592  continue;
1593  }
1594  if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
1595  out <<
1596  indent() << "if (!isset_" << (*f_iter)->get_name() << ")" << endl <<
1597  indent() << "{" << endl <<
1598  indent() << " *error = THRIFT_PROTOCOL_ERROR_INVALID_DATA;" << endl <<
1599  indent() << " syslog(LOG_ERROR, \"THRIFT_PROTOCOL_ERROR: missing field\");" << endl <<
1600  indent() << " return -1;" << endl <<
1601  indent() << "}" << endl <<
1602  endl;
1603  }
1604  }
1605 
1606  if (is_function) {
1607  indent(out) <<
1608  "return xfer;" << endl;
1609  }
1610 
1611  // end the function/structure
1612  indent_down();
1613  indent(out) <<
1614  "}" << endl <<
1615  endl;
1616 }
1617 
1622  string name,
1623  const vector <t_field *> &fields,
1624  bool is_sandesh,
1625  bool is_function) {
1626  string name_u = initial_caps_to_underscores(name);
1627  string rname = "r" + name_u;
1628  int error_ret = 0;
1629  vector <t_field *>::const_iterator f_iter;
1630  string sname = is_sandesh ? "sandesh" : "struct";
1631  string dname = is_sandesh ? "void" : name;
1632  string drname = is_sandesh ? "rsandesh" : rname;
1633 
1634  if (is_function) {
1635  error_ret = -1;
1636  indent(out) <<
1637  "/* reads a " << name_u << " " << sname << " */" << endl <<
1638  "int32_t" << endl <<
1639  this->nspace_lc << name_u <<
1640  "_read_binary_from_buffer (" << dname <<
1641  " *" << drname << ", uint8_t *Xbuf, const size_t Xbuflen, int *Xerror)" << endl;
1642  }
1643 
1644  indent(out) << "{" << endl;
1645  indent_up();
1646 
1647  // declare stack temp variables
1648  out <<
1649  indent() << "int32_t Xret;" << endl <<
1650  indent() << "int32_t Xoffset = 0;" << endl <<
1651  indent() << "char *Xname;" << endl <<
1652  indent() << "ThriftType Xftype;" << endl <<
1653  indent() << "int16_t Xfid;" << endl <<
1654  indent() << "u_int32_t Xi;" << endl;
1655 
1656  if (is_sandesh) {
1657  out << indent() << name << "* " << rname << " = " <<
1658  "(" << name << "* ) " << drname << ";" << endl;
1659  }
1660 
1661  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1662  // Ignore auto generated fields
1663  if ((*f_iter)->get_auto_generated()) {
1664  continue;
1665  }
1666  if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
1667  indent(out) << "u_int8_t isset_" << (*f_iter)->get_name() << " = 0;" << endl;
1668  }
1669  }
1670 
1671  // satisfy -Wall in case we don't use some variables
1672  out <<
1673  indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
1674  indent() << "THRIFT_UNUSED_VAR (Xret);" << endl <<
1675  indent() << "THRIFT_UNUSED_VAR (Xname);" << endl <<
1676  indent() << "THRIFT_UNUSED_VAR (Xi);" << endl <<
1677  indent() << "THRIFT_UNUSED_VAR (" << rname << ");" << endl;
1678 
1679  out << endl;
1680 
1681  // read the beginning of the structure marker
1682  out <<
1683  indent() << "/* read the " << sname << " begin marker */" << endl;
1684  generate_struct_begin_reader_from_buffer (out, "Xname", is_sandesh, error_ret);
1685 
1686  // read the struct fields
1687  out <<
1688  indent() << "/* read the struct fields */" << endl <<
1689  indent() << "while (1)" << endl;
1690  scope_up(out);
1691 
1692  // read beginning field marker
1693  out <<
1694  indent() << "/* read the beginning of a field */" << endl;
1695  generate_field_begin_reader_from_buffer (out, "Xftype", "Xfid", error_ret);
1696  out << endl;
1697 
1698  // switch depending on the field type
1699  indent(out) <<
1700  "switch (Xfid)" << endl;
1701 
1702  // start switch
1703  scope_up(out);
1704 
1705  // generate deserialization code for known types
1706  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1707  // Ignore auto generated fields
1708  if ((*f_iter)->get_auto_generated()) {
1709  continue;
1710  }
1711  indent(out) <<
1712  "case " << (*f_iter)->get_key() << ":" << endl;
1713  indent_up();
1714  indent(out) <<
1715  "if (Xftype == " << type_to_enum ((*f_iter)->get_type()) << ")" << endl;
1716  indent(out) <<
1717  "{" << endl;
1718 
1719 
1720  indent_up();
1721  // generate deserialize field
1722  generate_deserialize_field_from_buffer (out, *f_iter, rname + "->", "", error_ret);
1723  indent_down();
1724 
1725  out <<
1726  indent() << "} else {" << endl <<
1727  indent() << " if ((Xret = thrift_binary_protocol_skip_from_buffer (Xbuf + Xoffset, Xbuflen - Xoffset, Xftype, Xerror)) < 0)" << endl <<
1728  indent() << " return " << error_ret << ";" << endl <<
1729  indent() << " Xoffset += Xret;" << endl <<
1730  indent() << "}" << endl <<
1731  indent() << "break;" << endl;
1732  indent_down();
1733  }
1734 
1735  // create the default case
1736  out <<
1737  indent() << "default:" << endl <<
1738  indent() << " if ((Xret = thrift_binary_protocol_skip_from_buffer (Xbuf + Xoffset, Xbuflen - Xoffset, Xftype, Xerror)) < 0)" << endl <<
1739  indent() << " return " << error_ret << ";" << endl <<
1740  indent() << " Xoffset += Xret;" << endl <<
1741  indent() << " break;" << endl;
1742 
1743  // end switch
1744  scope_down(out);
1745 
1746  // read field end marker
1747  generate_field_end_reader_from_buffer (out);
1748 
1749  // end while loop
1750  scope_down(out);
1751  out << endl;
1752 
1753  // read the end of the structure
1754  generate_struct_end_reader_from_buffer (out, is_sandesh);
1755 
1756  // if a required field is missing, throw an error
1757  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1758  // Ignore auto generated fields
1759  if ((*f_iter)->get_auto_generated()) {
1760  continue;
1761  }
1762  if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
1763  out <<
1764  indent() << "if (!isset_" << (*f_iter)->get_name() << ")" << endl <<
1765  indent() << "{" << endl <<
1766  indent() << " *error = THRIFT_PROTOCOL_ERROR_INVALID_DATA;" << endl <<
1767  indent() << " syslog(LOG_ERROR, \"THRIFT_PROTOCOL_ERROR: missing field\");" << endl <<
1768  indent() << " return -1;" << endl <<
1769  indent() << "}" << endl <<
1770  endl;
1771  }
1772  }
1773 
1774  if (is_function) {
1775  indent(out) <<
1776  "return Xoffset;" << endl;
1777  }
1778 
1779  // end the function/structure
1780  indent_down();
1781  indent(out) <<
1782  "}" << endl <<
1783  endl;
1784 }
1785 
1787  string key,
1788  string field_type,
1789  int error_ret) {
1790  out <<
1791  indent() << "/* thrift_protocol_write_field_begin */" << endl;
1792  generate_buffer_bounds_chk (out, "1", error_ret);
1793  string field_type8(tmp("Xfield_type8"));
1794  scope_up(out);
1795  out <<
1796  indent() << "int8_t " << field_type8 << " = (int8_t) " <<
1797  field_type << ";" << endl;
1798  generate_write_buffer_memcpy_incr_offset (out, field_type8,
1799  "1", true);
1800  scope_down(out);
1801  generate_buffer_bounds_chk (out, "2", error_ret);
1802  string nkey(tmp("Xnfield_id"));
1803  scope_up(out);
1804  out <<
1805  indent() << "int16_t " << nkey << " = htons (" << key << ");" << endl;
1806  generate_write_buffer_memcpy_incr_offset (out, nkey, "2", true);
1807  scope_down(out);
1808  out << endl;
1809 }
1810 
1812  string field_type,
1813  string field_id,
1814  int error_ret) {
1815  out <<
1816  indent() << "/* thrift_protocol_read_field_begin */" << endl;
1817  generate_deserialize_byte_from_buffer (out, field_type, error_ret);
1818  // check for field STOP marker
1819  out <<
1820  indent() << "/* break if we get a STOP field */" << endl <<
1821  indent() << "if (" << field_type << " == T_STOP)" << endl <<
1822  indent() << "{" << endl <<
1823  indent() << " break;" << endl <<
1824  indent() << "}" << endl;
1825  out <<
1826  indent() << " else" << endl;
1827  scope_up(out);
1828  generate_deserialize_i16_from_buffer (out, field_id, error_ret);
1829  scope_down(out);
1830 }
1831 
1833  out <<
1834  indent() << "/* thrift_protocol_write_field_end */" << endl;
1835 }
1836 
1838  out <<
1839  indent() << "/* thrift_protocol_read_field_end */" << endl;
1840 }
1841 
1843  string name,
1844  int error_ret) {
1845  string nbool(tmp("Xbool"));
1846  out <<
1847  indent() << "/* thrift_protocol_write_bool */" << endl;
1848  scope_up(out);
1849  out <<
1850  indent() << "uint8_t " << nbool << " = " << name << " ? 1 : 0;" << endl;
1851  generate_serialize_byte_to_buffer (out, nbool, error_ret);
1852  scope_down(out);
1853 }
1854 
1856  string name,
1857  int error_ret) {
1858  out <<
1859  indent() << "/* thrift_protocol_read_bool */" << endl;
1860  scope_up(out);
1861  out << indent() << "void * b[1];" << endl;
1862  generate_read_buffer_chk_memcpy_incr_offset (out, "b", "1", error_ret,
1863  false);
1864  out <<
1865  indent() << name << " = *(int8_t *) b != 0;" << endl;
1866  scope_down(out);
1867 }
1868 
1870  string name,
1871  int error_ret) {
1872  out <<
1873  indent() << "/* thrift_protocol_write_byte */" << endl;
1874  generate_write_buffer_chk_memcpy_incr_offset (out, name, "1", error_ret,
1875  true);
1876 }
1877 
1879  string name,
1880  int error_ret) {
1881  out <<
1882  indent() << "/* thrift_protocol_read_byte */" << endl;
1883  scope_up(out);
1884  out << indent() << "void * b[1];" << endl;
1885  generate_read_buffer_chk_memcpy_incr_offset (out, "b", "1", error_ret,
1886  false);
1887  out <<
1888  indent() << name << " = *(int8_t *) b;" << endl;
1889  scope_down(out);;
1890 }
1891 
1893  string name,
1894  int error_ret) {
1895  string ni16(tmp("Xni16"));
1896  out << indent() << "/* thrift_protocol_write_i16 */" << endl;
1897  scope_up(out);
1898  out <<
1899  indent() << "int16_t " << ni16 << " = htons (" << name << ");" << endl;
1900  generate_write_buffer_chk_memcpy_incr_offset (out, ni16, "2", error_ret,
1901  true);
1902  scope_down(out);
1903 }
1904 
1906  string name,
1907  int error_ret) {
1908  out << indent() << "/* thrift_protocol_read_i16 */" << endl;
1909  scope_up(out);
1910  out <<
1911  indent() << "union {"<< endl <<
1912  indent() << " void * b[2];" << endl <<
1913  indent() << " int16_t all;" << endl <<
1914  indent() << "} bytes;" << endl;
1915  generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "2", error_ret,
1916  false);
1917  out <<
1918  indent() << name << " = ntohs (bytes.all);" << endl;
1919  scope_down(out);
1920 }
1921 
1923  string name,
1924  int error_ret) {
1925  string nu16(tmp("Xnu16"));
1926  out << indent() << "/* thrift_protocol_write_u16 */" << endl;
1927  scope_up(out);
1928  out <<
1929  indent() << "uint16_t " << nu16 << " = htons (" << name << ");" << endl;
1930  generate_write_buffer_chk_memcpy_incr_offset (out, nu16, "2", error_ret,
1931  true);
1932  scope_down(out);
1933 }
1934 
1936  string name,
1937  int error_ret) {
1938  out << indent() << "/* thrift_protocol_read_u16 */" << endl;
1939  scope_up(out);
1940  out <<
1941  indent() << "union {"<< endl <<
1942  indent() << " void * b[2];" << endl <<
1943  indent() << " uint16_t all;" << endl <<
1944  indent() << "} bytes;" << endl;
1945  generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "2", error_ret,
1946  false);
1947  out <<
1948  indent() << name << " = ntohs (bytes.all);" << endl;
1949  scope_down(out);
1950 }
1951 
1953  string name,
1954  int error_ret) {
1955  string ni32(tmp("Xni32"));
1956  out << indent() << "/* thrift_protocol_write_i32 */" << endl;
1957  scope_up(out);
1958  out <<
1959  indent() << "int32_t " << ni32 << " = htonl (" << name << ");" << endl;
1960  generate_write_buffer_chk_memcpy_incr_offset (out, ni32, "4", error_ret,
1961  true);
1962  scope_down(out);
1963 }
1964 
1966  string name,
1967  int error_ret) {
1968  out << indent() << "/* thrift_protocol_read_i32 */" << endl;
1969  scope_up(out);
1970  out <<
1971  indent() << "union {"<< endl <<
1972  indent() << " void * b[4];" << endl <<
1973  indent() << " int32_t all;" << endl <<
1974  indent() << "} bytes;" << endl;
1975  generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "4", error_ret,
1976  false);
1977  out <<
1978  indent() << name << " = ntohl (bytes.all);" << endl;
1979  scope_down(out);
1980 }
1981 
1983  string name,
1984  int error_ret) {
1985  string nu32(tmp("Xnu32"));
1986  out << indent() << "/* thrift_protocol_write_u32 */" << endl;
1987  scope_up(out);
1988  out <<
1989  indent() << "uint32_t " << nu32 << " = htonl (" << name << ");" << endl;
1990  generate_write_buffer_chk_memcpy_incr_offset (out, nu32, "4", error_ret,
1991  true);
1992  scope_down(out);
1993 }
1994 
1996  string name,
1997  int error_ret) {
1998  out << indent() << "/* thrift_protocol_read_u32 */" << endl;
1999  scope_up(out);
2000  out <<
2001  indent() << "union {"<< endl <<
2002  indent() << " void * b[4];" << endl <<
2003  indent() << " uint32_t all;" << endl <<
2004  indent() << "} bytes;" << endl;
2005  generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "4", error_ret,
2006  false);
2007  out <<
2008  indent() << name << " = ntohl (bytes.all);" << endl;
2009  scope_down(out);
2010 }
2011 
2013  string name,
2014  int error_ret) {
2015  string ni64(tmp("Xni64"));
2016  out << indent() << "/* thrift_protocol_write_i64 */" << endl;
2017  scope_up(out);
2018  out <<
2019  indent() << "int64_t " << ni64 << ";" << endl <<
2020  indent() << "os_put_value64((uint8_t *)&" << ni64 << ", " << name <<
2021  ");" << endl;
2022  generate_write_buffer_chk_memcpy_incr_offset (out, ni64, "8", error_ret,
2023  true);
2024  scope_down(out);
2025 }
2026 
2028  string name,
2029  int error_ret) {
2030  out << indent() << "/* thrift_protocol_read_i64 */" << endl;
2031  scope_up(out);
2032  out <<
2033  indent() << "void * b[8];" << endl;
2034  generate_read_buffer_chk_memcpy_incr_offset (out, "b", "8", error_ret,
2035  false);
2036  out <<
2037  indent() << name << " = os_get_value64((uint8_t *)b);" << endl;
2038  scope_down(out);
2039 }
2040 
2042  string name,
2043  int error_ret) {
2044  string nu64(tmp("Xnu64"));
2045  out << indent() << "/* thrift_protocol_write_u64 */" << endl;
2046  scope_up(out);
2047  out <<
2048  indent() << "uint64_t " << nu64 << ";" << endl <<
2049  indent() << "os_put_value64((uint8_t *)&" << nu64 << ", " << name <<
2050  ");" << endl;
2051  generate_write_buffer_chk_memcpy_incr_offset (out, nu64, "8", error_ret,
2052  true);
2053  scope_down(out);
2054 }
2055 
2057  string name,
2058  int error_ret) {
2059  out << indent() << "/* thrift_protocol_read_u64 */" << endl;
2060  scope_up(out);
2061  out <<
2062  indent() << "void * b[8];" << endl;
2063  generate_read_buffer_chk_memcpy_incr_offset (out, "b", "8", error_ret,
2064  false);
2065  out <<
2066  indent() << name << " = os_get_value64((uint8_t *)b);" << endl;
2067  scope_down(out);
2068 }
2069 
2071  string name,
2072  int error_ret) {
2073  generate_serialize_string_to_buffer(out, name, error_ret);
2074 }
2075 
2077  string name,
2078  int error_ret) {
2079  generate_deserialize_string_from_buffer(out, name, error_ret);
2080 }
2081 
2083  string name,
2084  int error_ret) {
2085  string s_strlen(tmp("Xsstrlen"));
2086  out << indent() << "/* thrift_protocol_write_string */" << endl;
2087  scope_up(out);
2088  out <<
2089  indent() << "int32_t " << s_strlen << " = " << name <<
2090  " != NULL ? strlen (" << name << ") : 0;" << endl;
2091  generate_write_buffer_binary (out, name, s_strlen, error_ret);
2092  scope_down(out);
2093 }
2094 
2096  string name,
2097  int error_ret) {
2098  string s_strlen(tmp("Xread_len"));
2099  out << indent() << "/* thrift_protocol_read_string */" << endl;
2100  scope_up(out);
2101  out <<
2102  indent() << "int32_t " << s_strlen << " = 0;" << endl;
2103  generate_deserialize_i32_from_buffer (out, s_strlen, error_ret);
2104  out <<
2105  indent() << "if (" << s_strlen << " > 0)" << endl;
2106  scope_up(out);
2107  out <<
2108  indent() << "if (Xbuflen < Xoffset + " << s_strlen << ") {" << endl <<
2109  indent() << " return " << error_ret << ";" << endl <<
2110  indent() << "}" << endl <<
2111  indent() << name << " = os_malloc (" << s_strlen << " + 1);" << endl;
2112  generate_read_buffer_memcpy_incr_offset (out, name, s_strlen, false);
2113  out <<
2114  indent() << name << "[" << s_strlen << "] = 0;" << endl;
2115  scope_down(out);
2116  out <<
2117  indent() << " else" << endl;
2118  scope_up(out);
2119  out <<
2120  indent() << name << " = NULL;" << endl;
2121  scope_down(out);
2122  scope_down(out);
2123 }
2124 
2126  string name,
2127  int error_ret) {
2128  out <<
2129  indent() << "/* thrift_protocol_write_ipaddr */" << endl;
2130  string iptype(name + ".iptype");
2131  out <<
2132  indent() << "if (" << iptype << " == AF_INET)" << endl;
2133  scope_up(out);
2134  generate_write_buffer_chk_memcpy_incr_offset (out, iptype, "1", error_ret,
2135  true);
2136  string ipv4(name + ".ipv4");
2137  generate_write_buffer_chk_memcpy_incr_offset (out, ipv4, "4", error_ret,
2138  true);
2139  scope_down(out);
2140  out <<
2141  indent() << " else if (" << iptype << " == AF_INET6)" << endl;
2142  scope_up(out);
2143  generate_write_buffer_chk_memcpy_incr_offset (out, iptype, "1", error_ret,
2144  true);
2145  string ipv6(name + ".ipv6");
2146  generate_write_buffer_chk_memcpy_incr_offset (out, ipv6, "16", error_ret,
2147  true);
2148  scope_down(out);
2149  out <<
2150  indent() << " else" << endl;
2151  scope_up(out);
2152  out <<
2153  indent() << "return -1;" << endl;
2154  scope_down(out);
2155 }
2156 
2158  string name,
2159  int error_ret) {
2160  out <<
2161  indent() << "/* thrift_protocol_read_ipaddr */" << endl;
2162  string iptype(name + ".iptype");
2163  generate_deserialize_byte_from_buffer (out, iptype, error_ret);
2164  out <<
2165  indent() << "if (" << iptype << " == AF_INET)" << endl;
2166  scope_up(out);
2167  string ipv4(name + ".ipv4");
2168  generate_read_buffer_chk_memcpy_incr_offset (out, ipv4, "4", error_ret,
2169  true);
2170  scope_down(out);
2171  out <<
2172  indent() << "else if (" << iptype << " == AF_INET6)" << endl;
2173  scope_up(out);
2174  string ipv6(name + ".ipv6");
2175  generate_read_buffer_chk_memcpy_incr_offset (out, ipv6, "16", error_ret,
2176  true);
2177  scope_down(out);
2178  out <<
2179  indent() << " else" << endl;
2180  scope_up(out);
2181  out <<
2182  indent() << "return -1;" << endl;
2183  scope_down(out);
2184 }
2185 
2187  string name,
2188  int error_ret) {
2189  out <<
2190  indent() << "/* thrift_protocol_write_uuid_t */" << endl;
2191  generate_write_buffer_chk_memcpy_incr_offset (out, name, "16", error_ret,
2192  true);
2193 }
2194 
2196  string name,
2197  int error_ret) {
2198  out <<
2199  indent() << "/* thrift_protocol_read_uuid_t */" << endl;
2200  generate_read_buffer_chk_memcpy_incr_offset (out, name, "16", error_ret,
2201  true);
2202 }
2203 
2205  string name,
2206  int error_ret) {
2207  out <<
2208  indent() << "/* thrift_protocol_write_double */" << endl <<
2209  indent() << "/* NOT SUPPORTED */" << endl <<
2210  indent() << "return " << error_ret << ";" << endl;
2211 }
2212 
2214  string name,
2215  int error_ret) {
2216  out <<
2217  indent() << "/* thrift_protocol_read_double */" << endl <<
2218  indent() << "/* NOT SUPPORTED */" << endl <<
2219  indent() << "return " << error_ret << ";" << endl;
2220 }
2221 
2223  t_field *tfield,
2224  string prefix,
2225  string suffix,
2226  int error_ret) {
2227  t_type *type = get_true_type (tfield->get_type());
2228  string name = prefix + tfield->get_name() + suffix;
2229 
2230  if (type->is_void()) {
2231  throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
2232  }
2233 
2234  if (type->is_struct() || type->is_xception()) {
2235  generate_serialize_struct (out, (t_struct *) type, name, error_ret);
2236  } else if (type->is_container()) {
2237  generate_serialize_container (out, type, name, error_ret);
2238  } else if (type->is_base_type() || type->is_enum()) {
2239  indent(out) <<
2240  "if ((ret = thrift_protocol_write_";
2241 
2242  if (type->is_base_type()) {
2243  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
2244  switch (tbase) {
2246  throw "compiler error: cannot serialize void field in a struct: "
2247  + name;
2248  break;
2250  out << "bool (protocol, " << name;
2251  break;
2253  out << "byte (protocol, " << name;
2254  break;
2255  case t_base_type::TYPE_I16:
2256  out << "i16 (protocol, " << name;
2257  break;
2258  case t_base_type::TYPE_I32:
2259  out << "i32 (protocol, " << name;
2260  break;
2261  case t_base_type::TYPE_I64:
2262  out << "i64 (protocol, " << name;
2263  break;
2264  case t_base_type::TYPE_U16:
2265  out << "u16 (protocol, " << name;
2266  break;
2267  case t_base_type::TYPE_U32:
2268  out << "u32 (protocol, " << name;
2269  break;
2270  case t_base_type::TYPE_U64:
2271  out << "u64 (protocol, " << name;
2272  break;
2274  out << "double (protocol, " << name;
2275  break;
2276  case t_base_type::TYPE_IPV4:
2277  out << "ipv4 (protocol, " << name;
2278  break;
2279  case t_base_type::TYPE_IPADDR:
2280  out << "ipaddr (protocol, &" << name;
2281  break;
2283  if (((t_base_type *) type)->is_binary()) {
2284  throw "CANNOT GENERATE SERIALIZE CODE FOR binary TYPE: " + name;
2285  } else {
2286  out << "string (protocol, " << name;
2287  }
2288  break;
2289  case t_base_type::TYPE_XML:
2290  out << "xml (protocol, " << name;
2291  break;
2292  case t_base_type::TYPE_UUID:
2293  out << "uuid_t (protocol, " << name;
2294  break;
2295  default:
2296  throw "compiler error: no C writer for base type "
2297  + t_base_type::t_base_name (tbase) + name;
2298  }
2299  } else if (type->is_enum()) {
2300  out << "i32 (protocol, " << name;
2301  }
2302  out << ", error)) < 0)" << endl <<
2303  indent() << " return " << error_ret << ";" << endl;
2304  } else {
2305  printf ("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
2306  name.c_str(), type_name (type).c_str());
2307  }
2308 }
2309 
2311  t_field *tfield,
2312  string prefix,
2313  string suffix,
2314  int error_ret) {
2315  t_type *type = get_true_type (tfield->get_type());
2316  string name = prefix + tfield->get_name() + suffix;
2317  ostringstream key_os;
2318  key_os << tfield->get_key();
2319  string key(key_os.str());
2320  string tenum(type_to_enum(tfield->get_type()));
2321 
2322  if (type->is_void()) {
2323  throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
2324  }
2325 
2326  if (type->is_struct() || type->is_xception()) {
2327  generate_serialize_struct_to_buffer (out, (t_struct *) type, name, error_ret);
2328  } else if (type->is_container()) {
2329  generate_serialize_container_to_buffer (out, type, name, error_ret);
2330  } else if (type->is_base_type() || type->is_enum()) {
2331  if (type->is_base_type()) {
2332  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
2333  switch (tbase) {
2335  throw "compiler error: cannot serialize void field in a struct: "
2336  + name;
2337  break;
2339  generate_serialize_bool_to_buffer (out, name, error_ret);
2340  break;
2342  generate_serialize_byte_to_buffer (out, name, error_ret);
2343  break;
2344  case t_base_type::TYPE_I16:
2345  generate_serialize_i16_to_buffer (out, name, error_ret);
2346  break;
2347  case t_base_type::TYPE_I32:
2348  generate_serialize_i32_to_buffer (out, name, error_ret);
2349  break;
2350  case t_base_type::TYPE_I64:
2351  generate_serialize_i64_to_buffer (out, name, error_ret);
2352  break;
2353  case t_base_type::TYPE_U16:
2354  generate_serialize_u16_to_buffer (out, name, error_ret);
2355  break;
2356  case t_base_type::TYPE_U32:
2357  generate_serialize_u32_to_buffer (out, name, error_ret);
2358  break;
2359  case t_base_type::TYPE_U64:
2360  generate_serialize_u64_to_buffer (out, name, error_ret);
2361  break;
2363  generate_serialize_double_to_buffer (out, name, error_ret);
2364  break;
2365  case t_base_type::TYPE_IPV4:
2366  generate_serialize_i32_to_buffer (out, name, error_ret);
2367  break;
2368  case t_base_type::TYPE_IPADDR:
2369  generate_serialize_ipaddr_to_buffer (out, name, error_ret);
2370  break;
2372  if (((t_base_type *) type)->is_binary()) {
2373  throw "CANNOT GENERATE SERIALIZE CODE FOR binary TYPE: " + name;
2374  } else {
2375  generate_serialize_string_to_buffer (out, name, error_ret);
2376  }
2377  break;
2378  case t_base_type::TYPE_XML:
2379  generate_serialize_xml_to_buffer (out, name, error_ret);
2380  break;
2381  case t_base_type::TYPE_UUID:
2382  generate_serialize_uuid_t_to_buffer (out, name, error_ret);
2383  break;
2384  default:
2385  throw "compiler error: no C writer for base type "
2386  + t_base_type::t_base_name (tbase) + name;
2387  }
2388  } else if (type->is_enum()) {
2389  generate_serialize_i32_to_buffer (out, name, error_ret);
2390  }
2391  } else {
2392  printf ("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
2393  name.c_str(), type_name (type).c_str());
2394  }
2395 }
2396 
2398  t_struct *tstruct,
2399  string prefix,
2400  int error_ret) {
2401  out <<
2402  indent() << "if ((ret = " << tstruct->get_name() << "_write (" << prefix << " , protocol, error)) < 0)" << endl <<
2403  indent() << " return " << error_ret << ";" << endl <<
2404  endl;
2405 }
2406 
2408  t_struct *tstruct,
2409  string prefix,
2410  int error_ret) {
2411  out <<
2412  indent() << "if ((Xret = " << tstruct->get_name() <<
2413  "_write_binary_to_buffer (" << prefix <<
2414  ", Xbuf + Xoffset, Xbuflen - Xoffset, Xerror)) < 0)" << endl;
2415  out <<
2416  indent() << " return " << error_ret << ";" << endl <<
2417  indent() << "Xoffset += Xret;" << endl;
2418 }
2419 
2420 
2422  t_type *ttype,
2423  string prefix,
2424  int error_ret) {
2425  scope_up(out);
2426 
2427  if (ttype->is_map()) {
2428  throw "compiler error: serialize of map not supported in C";
2429  } else if (ttype->is_set()) {
2430  throw "compiler error: serialize of set not supported in C";
2431  } else if (ttype->is_list()) {
2432  string length = prefix + "_size";
2433  out <<
2434  indent() << "if ((ret = thrift_protocol_write_list_begin (protocol, " <<
2435  type_to_enum (((t_list *) ttype)->get_elem_type()) <<
2436  ", " << length << ", error)) < 0)" << endl <<
2437  indent() << " return " << error_ret << ";" << endl <<
2438  indent() << "xfer += ret;" << endl <<
2439  indent() << "for (i = 0; i < " << length << "; i++)" << endl;
2440 
2441  scope_up(out);
2442  generate_serialize_list_element (out, (t_list *) ttype, prefix, "i", error_ret);
2443  scope_down(out);
2444 
2445  out <<
2446  indent() << "if ((ret = thrift_protocol_write_list_end (protocol, error)) < 0)" << endl <<
2447  indent() << " return " << error_ret << ";" << endl;
2448  }
2449 
2450  scope_down(out);
2451 }
2452 
2454  t_type *ttype,
2455  string prefix,
2456  int error_ret) {
2457  scope_up(out);
2458 
2459  if (ttype->is_map()) {
2460  throw "compiler error: serialize of map not supported in C";
2461  } else if (ttype->is_set()) {
2462  throw "compiler error: serialize of set not supported in C";
2463  } else if (ttype->is_list()) {
2464  string length = prefix + "_size";
2465  generate_list_begin_writer_to_buffer (out,
2466  type_to_enum (((t_list *) ttype)->get_elem_type()), length,
2467  error_ret);
2468  out <<
2469  indent() << "for (Xi = 0; Xi < " << length << "; Xi++)" << endl;
2470  scope_up(out);
2471  generate_serialize_list_element_to_buffer (out, (t_list *) ttype, prefix, "Xi", error_ret);
2472  scope_down(out);
2473  generate_list_end_writer_to_buffer (out);
2474  out << endl;
2475  }
2476  scope_down(out);
2477 }
2478 
2480  string element_type,
2481  string length,
2482  int error_ret) {
2483  string element_type8(tmp("Xelement_type8"));
2484  out <<
2485  indent() << "/* thrift_protocol_write_list_begin */" << endl;
2486  generate_buffer_bounds_chk (out, "1", error_ret);
2487  scope_up(out);
2488  out <<
2489  indent() << "int8_t " << element_type8 << " = (int8_t) " <<
2490  element_type << ";" << endl;
2491  generate_write_buffer_memcpy_incr_offset (out, element_type8, "1",
2492  true);
2493  scope_down(out);
2494  string nelement_size(tmp("Xnelement_size"));
2495  generate_buffer_bounds_chk (out, "4", error_ret);
2496  scope_up(out);
2497  out <<
2498  indent() << "uint32_t " << nelement_size << " = htonl (" <<
2499  length << ");" << endl;
2500  generate_write_buffer_memcpy_incr_offset (out, nelement_size, "4",
2501  true);
2502  scope_down(out);
2503  out << endl;
2504 }
2505 
2507  out <<
2508  indent() << "/* thrift_protocol_write_list_end */" << endl;
2509 }
2510 
2511 
2513  t_list *tlist,
2514  string list,
2515  string index,
2516  int error_ret) {
2517  t_type *ttype = tlist->get_elem_type();
2518  string name = list + "[" + index + "]";
2519  t_field efield (ttype, name);
2520  generate_serialize_field (out, &efield, "", "", error_ret);
2521  out << indent() << "xfer += ret;" << endl;
2522 }
2523 
2525  t_list *tlist,
2526  string list,
2527  string index,
2528  int error_ret) {
2529  t_type *ttype = tlist->get_elem_type();
2530  string name = list + "[" + index + "]";
2531  t_field efield (ttype, name);
2532  generate_serialize_field_to_buffer (out, &efield, "", "", error_ret);
2533  out << endl;
2534 }
2535 
2536 /* deserializes a field of any type. */
2538  t_field *tfield,
2539  string prefix,
2540  string suffix,
2541  int error_ret) {
2542  t_type *type = get_true_type (tfield->get_type());
2543 
2544  if (type->is_void()) {
2545  throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
2546  prefix + tfield->get_name();
2547  }
2548 
2549  string name = prefix + tfield->get_name() + suffix;
2550 
2551  if (type->is_struct() || type->is_xception()) {
2552  generate_deserialize_struct (out, (t_struct *) type, name, error_ret);
2553  } else if (type->is_container()) {
2554  generate_deserialize_container (out, type, name, error_ret);
2555  } else if (type->is_base_type()) {
2556  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
2557 
2558  indent(out) << "if ((ret = thrift_protocol_read_";
2559 
2560  switch (tbase) {
2562  throw "compiler error: cannot serialize void field in a struct: " + name;
2563  break;
2565  if (((t_base_type *) type)->is_binary()) {
2566  out << "binary (protocol, &data, &len";
2567  } else {
2568  out << "string (protocol, &" << name;
2569  }
2570  break;
2571  case t_base_type::TYPE_XML:
2572  out << "xml (protocol, &" << name;
2573  break;
2575  out << "bool (protocol, &" << name;
2576  break;
2578  out << "byte (protocol, &" << name;
2579  break;
2580  case t_base_type::TYPE_I16:
2581  out << "i16 (protocol, &" << name;
2582  break;
2583  case t_base_type::TYPE_I32:
2584  out << "i32 (protocol, &" << name;
2585  break;
2586  case t_base_type::TYPE_I64:
2587  out << "i64 (protocol, &" << name;
2588  break;
2589  case t_base_type::TYPE_U16:
2590  out << "u16 (protocol, &" << name;
2591  break;
2592  case t_base_type::TYPE_U32:
2593  out << "u32 (protocol, &" << name;
2594  break;
2595  case t_base_type::TYPE_U64:
2596  out << "u64 (protocol, &" << name;
2597  break;
2599  out << "double (protocol, &" << name;
2600  break;
2601  case t_base_type::TYPE_IPV4:
2602  out << "ipv4 (protocol, &" << name;
2603  break;
2604  case t_base_type::TYPE_IPADDR:
2605  out << "ipaddr (protocol, &" << name;
2606  break;
2607  case t_base_type::TYPE_UUID:
2608  out << "uuid_t (protocol, &" << name;
2609  break;
2610  default:
2611  throw "compiler error: no C reader for base type "
2612  + t_base_type::t_base_name (tbase) + name;
2613  }
2614  out << ", error)) < 0)" << endl;
2615  out << indent() << " return " << error_ret << ";" << endl <<
2616  indent() << "xfer += ret;" << endl;
2617 
2618  } else if (type->is_enum()) {
2619  string t = tmp ("ecast");
2620  out <<
2621  indent() << "int32_t " << t << ";" << endl <<
2622  indent() << "if ((ret = thrift_protocol_read_i32 (protocol, &" << t << ", error)) < 0)" << endl <<
2623  indent() << " return " << error_ret << ";" << endl <<
2624  indent() << "xfer += ret;" << endl <<
2625  indent() << name << " = (" << type_name (type) << ")" << t << ";" << endl;
2626  } else {
2627  printf ("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
2628  tfield->get_name().c_str(), type_name (type).c_str());
2629  }
2630 
2631  // if the type is not required and this is a thrift struct (no prefix),
2632  // set the isset variable. if the type is required, then set the
2633  // local variable indicating the value was set, so that we can do // validation later.
2634  if (tfield->get_req() != t_field::T_REQUIRED && prefix != "") {
2635  indent(out) << prefix << "__isset_" << tfield->get_name() << suffix << " = 1;" << endl;
2636  } else if (tfield->get_req() == t_field::T_REQUIRED && prefix != "") {
2637  indent(out) << "isset_" << tfield->get_name() << " = 1;" << endl;
2638  }
2639 }
2640 
2641 /* deserializes a field of any type. */
2643  t_field *tfield,
2644  string prefix,
2645  string suffix,
2646  int error_ret) {
2647  t_type *type = get_true_type (tfield->get_type());
2648 
2649  if (type->is_void()) {
2650  throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
2651  prefix + tfield->get_name();
2652  }
2653 
2654  string name = prefix + tfield->get_name() + suffix;
2655 
2656  if (type->is_struct() || type->is_xception()) {
2657  generate_deserialize_struct_from_buffer (out, (t_struct *) type, name, error_ret);
2658  } else if (type->is_container()) {
2659  generate_deserialize_container_from_buffer (out, type, name, error_ret);
2660  } else if (type->is_base_type()) {
2661  t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
2662 
2663  switch (tbase) {
2665  throw "compiler error: cannot serialize void field in a struct: " + name;
2666  break;
2668  if (((t_base_type *) type)->is_binary()) {
2669  throw "CANNOT GENERATE DESERIALIZE CODE FOR binary TYPE: " + name;
2670  } else {
2671  generate_deserialize_string_from_buffer (out, name, error_ret);
2672  }
2673  break;
2674  case t_base_type::TYPE_XML:
2675  generate_deserialize_xml_from_buffer (out, name, error_ret);
2676  break;
2678  generate_deserialize_bool_from_buffer (out, name, error_ret);
2679  break;
2681  generate_deserialize_byte_from_buffer (out, name, error_ret);
2682  break;
2683  case t_base_type::TYPE_I16:
2684  generate_deserialize_i16_from_buffer (out, name, error_ret);
2685  break;
2686  case t_base_type::TYPE_I32:
2687  generate_deserialize_i32_from_buffer (out, name, error_ret);
2688  break;
2689  case t_base_type::TYPE_I64:
2690  generate_deserialize_i64_from_buffer (out, name, error_ret);
2691  break;
2692  case t_base_type::TYPE_U16:
2693  generate_deserialize_u16_from_buffer (out, name, error_ret);
2694  break;
2695  case t_base_type::TYPE_U32:
2696  generate_deserialize_u32_from_buffer (out, name, error_ret);
2697  break;
2698  case t_base_type::TYPE_U64:
2699  generate_deserialize_u64_from_buffer (out, name, error_ret);
2700  break;
2702  generate_deserialize_double_from_buffer (out, name, error_ret);
2703  break;
2704  case t_base_type::TYPE_IPV4:
2705  generate_deserialize_i32_from_buffer (out, name, error_ret);
2706  break;
2707  case t_base_type::TYPE_IPADDR:
2708  generate_deserialize_ipaddr_from_buffer (out, name, error_ret);
2709  break;
2710  case t_base_type::TYPE_UUID:
2711  generate_deserialize_uuid_t_from_buffer (out, name, error_ret);
2712  break;
2713  default:
2714  throw "compiler error: no C reader for base type "
2715  + t_base_type::t_base_name (tbase) + name;
2716  }
2717  } else if (type->is_enum()) {
2718  generate_deserialize_i32_from_buffer (out, name, error_ret);
2719  } else {
2720  printf ("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
2721  tfield->get_name().c_str(), type_name (type).c_str());
2722  }
2723 
2724  // if the type is not required and this is a thrift struct (no prefix),
2725  // set the isset variable. if the type is required, then set the
2726  // local variable indicating the value was set, so that we can do // validation later.
2727  if (tfield->get_req() != t_field::T_REQUIRED && prefix != "") {
2728  indent(out) << prefix << "__isset_" << tfield->get_name() << suffix << " = 1;" << endl;
2729  } else if (tfield->get_req() == t_field::T_REQUIRED && prefix != "") {
2730  indent(out) << "isset_" << tfield->get_name() << " = 1;" << endl;
2731  }
2732 }
2733 
2735  t_struct *tstruct,
2736  string prefix,
2737  int error_ret) {
2738  out <<
2739  indent() << prefix << " = (" << tstruct->get_name() << "* ) os_zalloc (sizeof(*" << prefix << "));" << endl <<
2740  indent() << "if ((ret = " << tstruct->get_name() << "_read (" << prefix << ", protocol, error)) < 0)" << endl <<
2741  indent() << " return " << error_ret << ";" << endl <<
2742  indent() << "xfer += ret;" << endl;
2743 }
2744 
2746  t_struct *tstruct,
2747  string prefix,
2748  int error_ret) {
2749  out <<
2750  indent() << prefix << " = (" << tstruct->get_name() << "* ) os_zalloc (sizeof(*" << prefix << "));" << endl <<
2751  indent() << "if ((Xret = " << tstruct->get_name() << "_read_binary_from_buffer (" << prefix <<
2752  ", Xbuf + Xoffset, Xbuflen - Xoffset, Xerror)) < 0)" << endl <<
2753  indent() << " return " << error_ret << ";" << endl <<
2754  indent() << "Xoffset += Xret;" << endl;
2755 }
2756 
2758  string prefix, int error_ret) {
2759  scope_up(out);
2760 
2761  if (ttype->is_map()) {
2762  throw "compiler error: deserialize of map not supported in C";
2763  } else if (ttype->is_set()) {
2764  throw "compiler error: deserialize of set not supported in C";
2765  } else if (ttype->is_list()) {
2766  out <<
2767  indent() << "ThriftType element_type;" << endl <<
2768  indent() << "if ((ret = thrift_protocol_read_list_begin (protocol, &element_type, &" << prefix << "_size, error)) < 0)" << endl <<
2769  indent() << " return " << error_ret << ";" << endl <<
2770  indent() << "xfer += ret;" << endl <<
2771  endl;
2772 
2773  out <<
2774  indent() << prefix << " = (" << type_name(ttype, false, false) <<
2775  ") os_zalloc (sizeof(*" << prefix << ") * " << prefix << "_size);" << endl;
2776  out <<
2777  indent() << "/* iterate through list elements */" << endl <<
2778  indent() << "for (i = 0; i < " << prefix << "_size; i++)" << endl;
2779 
2780  scope_up(out);
2781  generate_deserialize_list_element (out, (t_list *) ttype, prefix, "i",
2782  error_ret);
2783  scope_down(out);
2784 
2785  out <<
2786  indent() << "if ((ret = thrift_protocol_read_list_end (protocol, error)) < 0)" << endl <<
2787  indent() << " return " << error_ret << ";" << endl <<
2788  indent() << "xfer += ret;" << endl <<
2789  endl;
2790  }
2791 
2792  scope_down(out);
2793 }
2794 
2796  string element_size,
2797  int error_ret) {
2798  string element_type8(tmp("Xelement_type8"));
2799  out <<
2800  indent() << "/* thrift_protocol_read_list_begin */" << endl;
2801  scope_up(out);
2802  out <<
2803  indent() << "int8_t " << element_type8 << ";" << endl;
2804  generate_deserialize_byte_from_buffer (out, element_type8, error_ret);
2805  scope_down(out);
2806  generate_deserialize_i32_from_buffer (out, element_size, error_ret);
2807 }
2808 
2810  out <<
2811  indent() << "/* thrift_protocol_read_list_end */" << endl;
2812 }
2813 
2815  string prefix, int error_ret) {
2816  scope_up(out);
2817 
2818  if (ttype->is_map()) {
2819  throw "compiler error: deserialize of map not supported in C";
2820  } else if (ttype->is_set()) {
2821  throw "compiler error: deserialize of set not supported in C";
2822  } else if (ttype->is_list()) {
2823  string element_size(prefix + "_size");
2824  generate_list_begin_reader_from_buffer (out, element_size, error_ret);
2825  out <<
2826  indent() << prefix << " = (" << type_name(ttype, false, false) <<
2827  ") os_zalloc (sizeof(*" << prefix << ") * " << element_size << ");" << endl;
2828  out <<
2829  indent() << "/* iterate through list elements */" << endl <<
2830  indent() << "for (Xi = 0; Xi < " << element_size << "; Xi++)" << endl;
2831 
2832  scope_up(out);
2833  generate_deserialize_list_element_from_buffer (out, (t_list *) ttype, prefix, "Xi",
2834  error_ret);
2835  scope_down(out);
2836 
2837  generate_list_end_reader_from_buffer (out);
2838  }
2839 
2840  scope_down(out);
2841 }
2842 
2844  t_list *tlist,
2845  string prefix,
2846  string index,
2847  int error_ret) {
2848  string elem = prefix + "[" + index + "]";
2849  t_field felem (tlist->get_elem_type(), elem);
2850  generate_deserialize_field (out, &felem, "", "", error_ret);
2851 }
2852 
2854  t_list *tlist,
2855  string prefix,
2856  string index,
2857  int error_ret) {
2858  string elem = prefix + "[" + index + "]";
2859  t_field felem (tlist->get_elem_type(), elem);
2860  generate_deserialize_field_from_buffer (out, &felem, "", "", error_ret);
2861 }
2862 
2863 /***************************************
2864  * UTILITY FUNCTIONS *
2865  ***************************************/
2866 
2870 string to_upper_case(string name) {
2871  string s (name);
2872  std::transform (s.begin(), s.end(), s.begin(), ::toupper);
2873  return s;
2874 // return boost::to_upper_copy (name);
2875 }
2876 
2880 string to_lower_case(string name) {
2881  string s (name);
2882  std::transform (s.begin(), s.end(), s.begin(), ::tolower);
2883  return s;
2884 // return boost::to_lower_copy (name);
2885 }
2886 
2894 string initial_caps_to_underscores(string name) {
2895  string ret;
2896  const char *tmp = name.c_str();
2897  int pos = 0;
2898 
2899  /* the first character isn't underscored if uppercase, just lowercased */
2900  ret += tolower (tmp[pos]);
2901  pos++;
2902  for (unsigned int i = pos; i < name.length(); i++) {
2903  char lc = tolower (tmp[i]);
2904  if (lc != tmp[i]) {
2905  ret += '_';
2906  }
2907  ret += lc;
2908  }
2909 
2910  return ret;
2911 }
2912 
2913 /* register this generator with the main program */
2914 THRIFT_REGISTER_GENERATOR(c, "C", "")
static std::string t_base_name(t_base tbase)
Definition: t_base_type.h:221
void generate_serialize_i16_to_buffer(ofstream &out, string name, int error_ret)
void generate_read_buffer_memcpy(ofstream &out, string dest, string length, bool ref)
void generate_serialize_i64_to_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_u32_from_buffer(ofstream &out, string name, int error_ret)
void generate_field_end_reader_from_buffer(ofstream &out)
void generate_deserialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret)
void generate_struct_writer_to_buffer(ofstream &out, string name, const vector< t_field * > &fields, bool is_sandesh, bool is_function=true)
void generate_deserialize_ipaddr_from_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_uuid_t_from_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_struct_from_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret)
void generate_serialize_u64_to_buffer(ofstream &out, string name, int error_ret)
void init_generator()
void generate_read_buffer_memcpy_incr_offset(ofstream &out, string dest, string length, bool ref)
void generate_deserialize_i16_from_buffer(ofstream &out, string name, int error_ret)
void generate_list_end_writer_to_buffer(ofstream &out)
void generate_service(t_service *tservice)
void generate_serialize_string_to_buffer(ofstream &out, string name, int error_ret)
void generate_read_buffer_chk_memcpy_incr_offset(ofstream &out, string dest, string length, int error_ret, bool ref)
void generate_serialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret)
void generate_serialize_uuid_t_to_buffer(ofstream &out, string name, int error_ret)
string base_type_name(t_base_type *type)
void generate_list_begin_reader_from_buffer(ofstream &out, string element_size, int error_ret)
void generate_serialize_field_to_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret)
void generate_serialize_container_to_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret)
void generate_serialize_i32_to_buffer(ofstream &out, string name, int error_ret)
void generate_struct_begin_writer_to_buffer(ofstream &out, string name, bool is_sandesh, int error_ret)
void generate_xception(t_struct *tstruct)
void generate_sandesh(t_sandesh *tsandesh)
void generate_write_buffer_memcpy(ofstream &out, string source, string length, bool ref)
void generate_deserialize_container_from_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret)
void generate_serialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret)
ofstream f_service_
void generate_const_initializer(string name, t_type *type, t_const_value *value)
void generate_serialize_list_element(ofstream &out, t_list *tlist, string list, string index, int error_ret)
void generate_struct_end_reader_from_buffer(ofstream &out, bool is_sandesh)
void generate_serialize_ipaddr_to_buffer(ofstream &out, string name, int error_ret)
void generate_serialize_xml_to_buffer(ofstream &out, string name, int error_ret)
void generate_struct_deleter(ofstream &out, string name, const vector< t_field * > &fields, bool is_sandesh, bool is_function=true)
void generate_struct_end_writer_to_buffer(ofstream &out, bool is_sandesh)
void generate_serialize_byte_to_buffer(ofstream &out, string name, int error_ret)
bool is_complex_type(t_type *ttype)
void generate_serialize_u32_to_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_byte_from_buffer(ofstream &out, string name, int error_ret)
void generate_struct_reader_from_buffer(ofstream &out, string name, const vector< t_field * > &fields, bool is_sandesh, bool is_function=true)
void generate_struct_begin_reader_from_buffer(ofstream &out, string name, bool is_sandesh, int error_ret)
string argument_list(t_struct *tstruct)
void generate_buffer_bounds_chk(ofstream &out, string length, int error_ret)
void generate_deserialize_field_from_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret)
void generate_field_begin_reader_from_buffer(ofstream &out, string field_type, string field_id, int error_ret)
void generate_deserialize_u64_from_buffer(ofstream &out, string name, int error_ret)
void generate_write_buffer_binary(ofstream &out, string buf, string buf_len, int error_ret)
void generate_deserialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret)
ofstream f_types_
void generate_serialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret)
ofstream f_types_impl_
void generate_sandesh_info()
void close_generator()
string constant_value(string name, t_type *type, t_const_value *value)
void generate_field_end_writer_to_buffer(ofstream &out)
void generate_deserialize_bool_from_buffer(ofstream &out, string name, int error_ret)
void generate_serialize_double_to_buffer(ofstream &out, string name, int error_ret)
string declare_field(t_field *tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false)
string type_name(t_type *ttype, bool in_typedef=false, bool is_const=false)
void generate_deserialize_i32_from_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_double_from_buffer(ofstream &out, string name, int error_ret)
void generate_field_stop_writer_to_buffer(ofstream &out, int error_ret)
string type_to_enum(t_type *type)
void generate_struct_writer(ofstream &out, string name, const vector< t_field * > &fields, bool is_sandesh, bool is_function=true)
void generate_consts(vector< t_const * > consts)
void generate_serialize_bool_to_buffer(ofstream &out, string name, int error_ret)
ofstream f_header_
void generate_struct(t_struct *tstruct)
void generate_object(t_struct *tstruct)
void generate_deserialize_string_from_buffer(ofstream &out, string name, int error_ret)
void generate_serialize_u16_to_buffer(ofstream &out, string name, int error_ret)
void generate_write_buffer_chk_memcpy_incr_offset(ofstream &out, string source, string length, int error_ret, bool ref)
void generate_deserialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret)
void generate_write_buffer_memcpy_incr_offset(ofstream &out, string source, string length, bool ref)
void generate_typedef(t_typedef *ttypedef)
void generate_enum(t_enum *tenum)
void generate_buffer_incr_offset(ofstream &out, string length)
void generate_field_begin_writer_to_buffer(ofstream &out, string key, string field_type, int error_ret)
void generate_object_internal(string name, const vector< t_field * > &members, bool is_sandesh)
t_c_generator(t_program *program, const map< string, string > &parsed_options, const string &option_string)
void generate_deserialize_u16_from_buffer(ofstream &out, string name, int error_ret)
void generate_deserialize_i64_from_buffer(ofstream &out, string name, int error_ret)
void generate_service_client(t_service *tservice)
void generate_struct_reader(ofstream &out, string name, const vector< t_field * > &fields, bool is_sandesh, bool is_function=true)
void generate_deserialize_list_element(ofstream &out, t_list *tlist, string prefix, string index, int error_ret)
void generate_deserialize_xml_from_buffer(ofstream &out, string name, int error_ret)
void generate_service_server(t_service *tservice)
void generate_serialize_struct_to_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret)
void generate_deserialize_list_element_from_buffer(ofstream &out, t_list *tlist, string prefix, string index, int error_ret)
void generate_list_end_reader_from_buffer(ofstream &out)
void generate_list_begin_writer_to_buffer(ofstream &out, string element_type, string length, int error_ret)
void generate_serialize_list_element_to_buffer(ofstream &out, t_list *tlist, string list, string index, int error_ret)
std::string get_uuid() const
double get_double() const
std::string get_string() const
Definition: t_const_value.h:66
t_const_value_type get_type() const
int64_t get_integer() const
Definition: t_const_value.h:75
bool has_cpp_name()
Definition: t_container.h:41
std::string get_cpp_name()
Definition: t_container.h:45
Definition: t_enum.h:33
const std::vector< t_enum_value * > & get_constants()
Definition: t_enum.h:46
int32_t get_key() const
Definition: t_field.h:102
const std::string & get_name() const
Definition: t_field.h:98
t_type * get_type() const
Definition: t_field.h:94
@ T_OPTIONAL
Definition: t_field.h:108
@ T_REQUIRED
Definition: t_field.h:107
e_req get_req() const
Definition: t_field.h:116
Definition: t_list.h:32
t_type * get_elem_type() const
Definition: t_list.h:37
const members_type & get_members()
Definition: t_type.h:51
virtual bool is_set() const
Definition: t_type.h:80
virtual bool is_xception() const
Definition: t_type.h:77
virtual bool is_struct() const
Definition: t_type.h:76
virtual const std::string & get_name() const
Definition: t_type.h:59
virtual bool is_base_type() const
Definition: t_type.h:64
virtual bool is_list() const
Definition: t_type.h:79
virtual bool is_container() const
Definition: t_type.h:78
virtual bool is_map() const
Definition: t_type.h:81
t_type * get_type() const
Definition: t_typedef.h:45
const std::string & get_symbolic() const
Definition: t_typedef.h:49
uint8_t type
Definition: load_balance.h:2
void init()
Definition: bgp_log.cc:38
#define MKDIR(x)
Definition: platform.h:31
string initial_caps_to_underscores(string name)
string to_upper_case(string name)
string to_lower_case(string name)
#define THRIFT_REGISTER_GENERATOR(language, long_name, doc)