1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
13 #include "parse/t_program.h"
17 * Base class for a thrift code generator. This class defines the basic
18 * routines for code generation and contains the top level method that
19 * dispatches code generation across various components.
21 * @author Mark Slee <mcslee@facebook.com>
25 t_generator(t_program
* program
) {
29 program_name_
= get_program_name(program
);
32 virtual ~t_generator() {}
35 * Framework generator method that iterates over all the parts of a program
36 * and performs general actions. This is implemented by the base class and
37 * should not be overwritten in the subclasses.
39 void generate_program();
44 * Optional methods that may be imlemented by subclasses to take necessary
45 * steps at the beginning or end of code generation.
48 virtual void init_generator() {}
49 virtual void close_generator() {}
51 virtual void generate_consts(std::vector
<t_const
*> consts
);
54 * Pure virtual methods implemented by the generator subclasses.
57 virtual void generate_typedef (t_typedef
* ttypedef
) = 0;
58 virtual void generate_enum (t_enum
* tenum
) = 0;
59 virtual void generate_const (t_const
* tconst
) {}
60 virtual void generate_struct (t_struct
* tstruct
) = 0;
61 virtual void generate_service (t_service
* tservice
) = 0;
62 virtual void generate_xception (t_struct
* txception
) {
63 // By default exceptions are the same as structs
64 generate_struct(txception
);
68 * Method to get the program name, may be overridden
70 virtual std::string
get_program_name(t_program
* tprogram
) {
71 return tprogram
->get_name();
75 * Method to get the service name, may be overridden
77 virtual std::string
get_service_name(t_service
* tservice
) {
78 return tservice
->get_name();
82 * Get the current output directory
84 virtual std::string
get_out_dir() const {
85 return program_
->get_out_path() + out_dir_base_
+ "/";
89 * Creates a unique temporary variable name, which is just "name" with a
90 * number appended to it (i.e. name35)
92 std::string
tmp(std::string name
) {
93 std::ostringstream out
;
94 out
<< name
<< tmp_
++;
99 * Indentation level modifiers
111 * Indentation print function
113 std::string
indent() {
114 std::string ind
= "";
116 for (i
= 0; i
< indent_
; ++i
) {
123 * Indentation utility wrapper
125 std::ostream
& indent(std::ostream
&os
) {
126 return os
<< indent();
130 * Capitalization helpers
132 std::string
capitalize(std::string in
) {
133 in
[0] = toupper(in
[0]);
136 std::string
decapitalize(std::string in
) {
137 in
[0] = tolower(in
[0]);
142 * Get the true type behind a series of typedefs.
144 static t_type
* get_true_type(t_type
* type
) {
145 while (type
->is_typedef()) {
146 type
= ((t_typedef
*)type
)->get_type();
153 * The program being generated
158 * Quick accessor for formatted program name that is currently being
161 std::string program_name_
;
164 * Quick accessor for formatted service name that is currently being
167 std::string service_name_
;
170 * Output type-specifc directory name ("gen-*")
172 std::string out_dir_base_
;
176 * Current code indentation level
181 * Temporary variable counter, for making unique variable names