summaryrefslogtreecommitdiff
path: root/compiler/cpp/src/thrift/generate/t_generator.h
blob: f82463d0c1455bc64f2489bd638622dc11a838b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef T_GENERATOR_H
#define T_GENERATOR_H
#define MSC_2015_VER 1900

#include <cstring>
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <limits>
#include <sstream>
#include "thrift/common.h"
#include "thrift/logging.h"
#include "thrift/version.h"
#include "thrift/generate/t_generator_registry.h"
#include "thrift/parse/t_program.h"

/**
 * Base class for a thrift code generator. This class defines the basic
 * routines for code generation and contains the top level method that
 * dispatches code generation across various components.
 *
 */
class t_generator {
public:
  t_generator(t_program* program) {
    update_keywords();

    tmp_ = 0;
    indent_ = 0;
    program_ = program;
    program_name_ = get_program_name(program);
    escape_['\n'] = "\\n";
    escape_['\r'] = "\\r";
    escape_['\t'] = "\\t";
    escape_['"'] = "\\\"";
    escape_['\\'] = "\\\\";
  }

  virtual ~t_generator() {}

  /**
   * Framework generator method that iterates over all the parts of a program
   * and performs general actions. This is implemented by the base class and
   * should not normally be overwritten in the subclasses.
   */
  virtual void generate_program();

  const t_program* get_program() const { return program_; }

  void generate_docstring_comment(std::ostream& out,
                                  const std::string& comment_start,
                                  const std::string& line_prefix,
                                  const std::string& contents,
                                  const std::string& comment_end);

  static void parse_options(const std::string& options, std::string& language,
                     std::map<std::string, std::string>& parsed_options);

  /**
   * check whether sub-namespace declaraction is used by generator.
   * e.g. allow
   * namespace py.twisted bar
   * to specify namespace to use when -gen py:twisted is specified.
   * Will be called with subnamespace, i.e. is_valid_namespace("twisted")
   * will be called for the above example.
   */
  static bool is_valid_namespace(const std::string& sub_namespace) {
    (void)sub_namespace;
    return false;
  }

  /**
   * Escape string to use one in generated sources.
   */
  virtual std::string escape_string(const std::string& in) const;

  std::string get_escaped_string(t_const_value* constval) {
    return escape_string(constval->get_string());
  }

  /**
   * Check if all identifiers are valid for the target language
   * See update_keywords()
   */
  virtual void validate_input() const;

protected:
  virtual std::set<std::string> lang_keywords() const;

  /**
   * Call this from constructor if you implement lang_keywords()
   */
  void update_keywords() {
    keywords_ = lang_keywords();
  }

  /**
   * A list of reserved words that cannot be used as identifiers.
   */
  std::set<std::string> keywords_;

  virtual void validate_id(const std::string& id) const;

  virtual void validate(t_enum const* en) const;
  virtual void validate(t_enum_value const* en_val) const;
  virtual void validate(t_typedef const* td) const;
  virtual void validate(t_const const* c) const;
  virtual void validate(t_service const* s) const;
  virtual void validate(t_struct const* c) const;
  virtual void validate(t_field const* f) const;
  virtual void validate(t_function const* f) const;

  template <typename T>
  void validate(const std::vector<T>& list) const;

  /**
   * Optional methods that may be implemented by subclasses to take necessary
   * steps at the beginning or end of code generation.
   */

  virtual void init_generator() {}
  virtual void close_generator() {}

  virtual void generate_consts(std::vector<t_const*> consts);

  /**
   * Pure virtual methods implemented by the generator subclasses.
   */

  virtual void generate_typedef(t_typedef* ttypedef) = 0;
  virtual void generate_enum(t_enum* tenum) = 0;
  virtual void generate_const(t_const* tconst) { (void)tconst; }
  virtual void generate_struct(t_struct* tstruct) = 0;
  virtual void generate_service(t_service* tservice) = 0;
  virtual void generate_forward_declaration(t_struct*) {}
  virtual void generate_xception(t_struct* txception) {
    // By default exceptions are the same as structs
    generate_struct(txception);
  }

  /**
   * Method to get the program name, may be overridden
   */
  virtual std::string get_program_name(t_program* tprogram) { return tprogram->get_name(); }

  /**
   * Method to get the service name, may be overridden
   */
  virtual std::string get_service_name(t_service* tservice) { return tservice->get_name(); }

  /**
   * Get the current output directory
   */
  virtual std::string get_out_dir() const {
    if (program_->is_out_path_absolute()) {
      return program_->get_out_path() + "/";
    }

    return program_->get_out_path() + out_dir_base_ + "/";
  }

  /**
   * Creates a unique temporary variable name, which is just "name" with a
   * number appended to it (i.e. name35)
   */
  std::string tmp(std::string name) {
    std::ostringstream out;
    out << name << tmp_++;
    return out.str();
  }

  /**
   * Generates a comment about this code being autogenerated, using C++ style
   * comments, which are also fair game in Java / PHP, yay!
   *
   * @return C-style comment mentioning that this file is autogenerated.
   */
  virtual std::string autogen_comment() {
    return std::string("/**\n") + " * " + autogen_summary() + "\n" + " *\n"
           + " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
           + " *  @generated\n" + " */\n";
  }

  virtual std::string autogen_summary() {
    return std::string("Autogenerated by Thrift Compiler (") + THRIFT_VERSION + ")";
  }

  /**
   * Indentation level modifiers
   */

  void indent_up() { ++indent_; }

  void indent_down() { --indent_; }

  /**
  * Indentation validation helper
  */
  int indent_count() { return indent_; }

  void indent_validate( int expected, const char * func_name) {
    if (indent_ != expected) { 
      pverbose("Wrong indent count in %s: difference = %i \n", func_name, (expected - indent_));
    }
  }

  /**
   * Indentation print function
   */
  std::string indent() {
    std::string ind = "";
    int i;
    for (i = 0; i < indent_; ++i) {
      ind += indent_str();
    }
    return ind;
  }

  /**
   * Indentation utility wrapper
   */
  std::ostream& indent(std::ostream& os) { return os << indent(); }

  /**
   * Capitalization helpers
   */
  std::string capitalize(std::string in) {
    in[0] = toupper(in[0]);
    return in;
  }
  std::string decapitalize(std::string in) {
    in[0] = tolower(in[0]);
    return in;
  }
  static std::string lowercase(std::string in) {
    for (size_t i = 0; i < in.size(); ++i) {
      in[i] = tolower(in[i]);
    }
    return in;
  }
  static std::string uppercase(std::string in) {
    for (size_t i = 0; i < in.size(); ++i) {
      in[i] = toupper(in[i]);
    }
    return in;
  }

  /**
   * Transforms a camel case string to an equivalent one separated by underscores
   * e.g. aMultiWord -> a_multi_word
   *      someName   -> some_name
   *      CamelCase  -> camel_case
   *      name       -> name
   *      Name       -> name
   */
  std::string underscore(std::string in) {
    in[0] = tolower(in[0]);
    for (size_t i = 1; i < in.size(); ++i) {
      if (isupper(in[i])) {
        in[i] = tolower(in[i]);
        in.insert(i, "_");
      }
    }
    return in;
  }

  /**
    * Transforms a string with words separated by underscores to a camel case equivalent
    * e.g. a_multi_word -> aMultiWord
    *      some_name    ->  someName
    *      name         ->  name
    */
  std::string camelcase(std::string in) {
    std::ostringstream out;
    bool underscore = false;

    for (size_t i = 0; i < in.size(); i++) {
      if (in[i] == '_') {
        underscore = true;
        continue;
      }
      if (underscore) {
        out << (char)toupper(in[i]);
        underscore = false;
        continue;
      }
      out << in[i];
    }

    return out.str();
  }

  const std::string emit_double_as_string(const double value) {
      std::stringstream double_output_stream;
      // sets the maximum precision: http://en.cppreference.com/w/cpp/io/manip/setprecision
      // sets the output format to fixed: http://en.cppreference.com/w/cpp/io/manip/fixed (not in scientific notation)
      double_output_stream << std::setprecision(std::numeric_limits<double>::digits10 + 1);

      #ifdef _MSC_VER
          // strtod is broken in MSVC compilers older than 2015, so std::fixed fails to format a double literal.
          // more details: https://blogs.msdn.microsoft.com/vcblog/2014/06/18/
          //               c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
          //               and
          //               http://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
          #if _MSC_VER >= MSC_2015_VER
              double_output_stream << std::fixed;
          #endif
      #else
          double_output_stream << std::fixed;
      #endif

      double_output_stream << value;

      return double_output_stream.str();
  }

public:
  /**
   * Get the true type behind a series of typedefs.
   */
  static const t_type* get_true_type(const t_type* type) { return type->get_true_type(); }
  static t_type* get_true_type(t_type* type) { return type->get_true_type(); }

protected:
  /**
   * The program being generated
   */
  t_program* program_;

  /**
   * Quick accessor for formatted program name that is currently being
   * generated.
   */
  std::string program_name_;

  /**
   * Quick accessor for formatted service name that is currently being
   * generated.
   */
  std::string service_name_;

  /**
   * Output type-specifc directory name ("gen-*")
   */
  std::string out_dir_base_;

  /**
   * Map of characters to escape in string literals.
   */
  std::map<char, std::string> escape_;

  virtual std::string indent_str() const {
    return "  ";
  }

private:
  /**
   * Current code indentation level
   */
  int indent_;

  /**
   * Temporary variable counter, for making unique variable names
   */
  int tmp_;
};

template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class template_ofstream_with_content_based_conditional_update : public std::ostringstream {
public:
  template_ofstream_with_content_based_conditional_update(): contents_written(false) {}

  template_ofstream_with_content_based_conditional_update(std::string const& output_file_path_)
  : output_file_path(output_file_path_), contents_written(false) {}

  ~template_ofstream_with_content_based_conditional_update() {
    if (!contents_written) {
      close();
    }
  }

  void open(std::string const& output_file_path_) {
    output_file_path = output_file_path_;
    clear_buf();
    contents_written = false;
  }

  void close() {
    if (contents_written || output_file_path == "")
      return;

    if (!is_readable(output_file_path)) {
      dump();
      return;
    }

    std::ifstream old_file;
    old_file.exceptions(old_file.exceptions() | std::ifstream::badbit | std::ifstream::failbit);
    old_file.open(output_file_path.c_str(), std::ios::in);

    if (old_file) {
      std::string const old_file_contents(static_cast<std::ostringstream const&>(std::ostringstream() << old_file.rdbuf()).str());
      old_file.close();

      if (old_file_contents != str()) {
        dump();
      }
    }
  }

protected:
  void dump() {
    std::ofstream out_file;
    out_file.exceptions(out_file.exceptions() | std::ofstream::badbit | std::ofstream::failbit);
    try {
      out_file.open(output_file_path.c_str(), std::ios::out);
    }
    catch (const std::ios_base::failure& e) {
      ::failure("failed to write the output to the file '%s', details: '%s'", output_file_path.c_str(), e.what());
    }
    out_file << str();
    out_file.close();
    clear_buf();
    contents_written = true;
  }

  void clear_buf() {
    str(std::string());
  }

  static bool is_readable(std::string const& file_name) {
    return static_cast<bool>(std::ifstream(file_name.c_str()));
  }

private:
  std::string output_file_path;
  bool contents_written;
};
typedef template_ofstream_with_content_based_conditional_update<char> ofstream_with_content_based_conditional_update;

#endif