blob: aee89ae3e7c1768f8cd3ca6137145fbf3aadb734 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
2 * thrift - a lightweight cross-language rpc/serialization tool
3 *
4 * This file contains the main compiler engine for Thrift, which invokes the
5 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +00006 * code for each language lives in a file by the language name under the
7 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +00008 *
9 * @author Mark Slee <mcslee@facebook.com>
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdarg.h>
15#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000016#include <sys/types.h>
17#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000018
Mark Sleef0712dc2006-10-25 19:03:57 +000019// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000020#include "globals.h"
21
22#include "main.h"
23#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000024#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000025#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000026#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000027#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000028#include "generate/t_py_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000029
30using namespace std;
31
Mark Sleef5377b32006-10-10 01:42:59 +000032/**
33 * Global program tree
34 */
Mark Slee31985722006-05-24 21:45:31 +000035t_program* g_program;
36
Mark Sleef5377b32006-10-10 01:42:59 +000037/**
Mark Sleef0712dc2006-10-25 19:03:57 +000038 * Global types
39 */
40
41t_type* g_type_void;
42t_type* g_type_string;
43t_type* g_type_bool;
44t_type* g_type_byte;
45t_type* g_type_i16;
46t_type* g_type_i32;
47t_type* g_type_i64;
48t_type* g_type_double;
49
50/**
51 * Global scope
52 */
53t_scope* g_scope;
54
55/**
56 * Parent scope to also parse types
57 */
58t_scope* g_parent_scope;
59
60/**
61 * Prefix for putting types in parent scope
62 */
63string g_parent_prefix;
64
65/**
66 * Parsing pass
67 */
68PARSE_MODE g_parse_mode;
69
70/**
71 * Current directory of file being parsed
72 */
73string g_curdir;
74
75/**
76 * Current file being parsed
77 */
78string g_curpath;
79
80/**
Mark Sleef5377b32006-10-10 01:42:59 +000081 * Global debug state
82 */
Mark Slee31985722006-05-24 21:45:31 +000083int g_debug = 0;
84
Mark Sleef5377b32006-10-10 01:42:59 +000085/**
Mark Sleef0712dc2006-10-25 19:03:57 +000086 * Warning level
87 */
88int g_warn = 1;
89
90/**
91 * Verbose output
92 */
93int g_verbose = 0;
94
95/**
Mark Sleef5377b32006-10-10 01:42:59 +000096 * Global time string
97 */
Mark Slee31985722006-05-24 21:45:31 +000098char* g_time_str;
99
Mark Slee31985722006-05-24 21:45:31 +0000100/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000101 * Flags to control code generation
102 */
103bool gen_cpp = false;
104bool gen_java = false;
105bool gen_py = false;
106bool gen_php = false;
107bool gen_phpi = false;
108bool gen_recurse = false;
109
110/**
Mark Slee31985722006-05-24 21:45:31 +0000111 * Report an error to the user. This is called yyerror for historical
112 * reasons (lex and yacc expect the error reporting routine to be called
113 * this). Call this function to report any errors to the user.
114 * yyerror takes printf style arguments.
115 *
116 * @param fmt C format string followed by additional arguments
117 */
118void yyerror(char* fmt, ...) {
119 va_list args;
120 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000121 "[ERROR:%s:%d] (last token was '%s')\n",
122 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000123 yylineno,
124 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000125
126 va_start(args, fmt);
127 vfprintf(stderr, fmt, args);
128 va_end(args);
129
130 fprintf(stderr, "\n");
131}
132
133/**
134 * Prints a debug message from the parser.
135 *
136 * @param fmt C format string followed by additional arguments
137 */
138void pdebug(char* fmt, ...) {
139 if (g_debug == 0) {
140 return;
141 }
142 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000143 printf("[PARSE] ");
144 va_start(args, fmt);
145 vprintf(fmt, args);
146 va_end(args);
147 printf("\n");
148}
149
150/**
151 * Prints a verbose output mode message
152 *
153 * @param fmt C format string followed by additional arguments
154 */
155void pverbose(char* fmt, ...) {
156 if (g_verbose == 0) {
157 return;
158 }
159 va_list args;
160 va_start(args, fmt);
161 vprintf(fmt, args);
162 va_end(args);
163}
164
165/**
166 * Prints a warning message
167 *
168 * @param fmt C format string followed by additional arguments
169 */
170void pwarning(int level, char* fmt, ...) {
171 if (g_warn < level) {
172 return;
173 }
174 va_list args;
175 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000176 va_start(args, fmt);
177 vprintf(fmt, args);
178 va_end(args);
179 printf("\n");
180}
181
182/**
183 * Prints a failure message and exits
184 *
185 * @param fmt C format string followed by additional arguments
186 */
187void failure(char* fmt, ...) {
188 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000189 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000190 va_start(args, fmt);
191 vfprintf(stderr, fmt, args);
192 va_end(args);
193 printf("\n");
194 exit(1);
195}
196
197/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000198 * Converts a string filename into a thrift program name
199 */
200string program_name(string filename) {
201 string::size_type slash = filename.rfind("/");
202 if (slash != string::npos) {
203 filename = filename.substr(slash+1);
204 }
205 string::size_type dot = filename.rfind(".");
206 if (dot != string::npos) {
207 filename = filename.substr(0, dot);
208 }
209 return filename;
210}
211
212/**
213 * Gets the directory path of a filename
214 */
215string directory_name(string filename) {
216 string::size_type slash = filename.rfind("/");
217 // No slash, just use the current directory
218 if (slash == string::npos) {
219 return ".";
220 }
221 return filename.substr(0, slash);
222}
223
224/**
225 * Finds the appropriate file path for the given filename
226 */
227string include_file(string filename) {
228 // Absolute path? Just try that
229 if (filename[0] != '/') {
230 filename = g_curdir + "/" + filename;
231 }
232
233 // Realpath!
234 char rp[PATH_MAX];
235 if (realpath(filename.c_str(), rp) == NULL) {
236 pwarning(0, "Cannot open include file %s\n", filename.c_str());
237 return std::string();
238 }
239
240 // Stat this files
241 struct stat finfo;
242 if (stat(rp, &finfo) == 0) {
243 return rp;
244 }
245
246 // Uh oh
247 pwarning(0, "Could not find include file %s\n", filename.c_str());
248 return std::string();
249}
250
251/**
Mark Slee31985722006-05-24 21:45:31 +0000252 * Diplays the usage message and then exits with an error code.
253 */
254void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000255 fprintf(stderr, "Usage: thrift [options] file\n");
256 fprintf(stderr, "Options:\n");
Mark Sleef0712dc2006-10-25 19:03:57 +0000257 fprintf(stderr, " --cpp Generate C++ output files\n");
258 fprintf(stderr, " --java Generate Java output files\n");
259 fprintf(stderr, " --php Generate PHP output files\n");
260 fprintf(stderr, " --phpi Generate PHP inlined files\n");
261 fprintf(stderr, " --py Generate Python output files\n");
262 fprintf(stderr, " --nowarn Suppress all compiler warnings (BAD!)\n");
263 fprintf(stderr, " --strict Strict compiler warnings on\n");
264 fprintf(stderr, " --v[erbose] Verbose mode\n");
265 fprintf(stderr, " --r[ecurse] Also generate included files\n");
266 fprintf(stderr, " --debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000267 exit(1);
268}
269
270/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 * Parses a program
272 */
273void parse(t_program* program, t_program* parent_program) {
274 // Get scope file path
275 string path = program->get_path();
276
277 // Set current dir global, which is used in the include_file function
278 g_curdir = directory_name(path);
279 g_curpath = path;
280
281 // Open the file
282 yyin = fopen(path.c_str(), "r");
283 if (yyin == 0) {
284 failure("Could not open input file: \"%s\"", path.c_str());
285 }
286
287 // Create new scope and scan for includes
288 pverbose("Scanning %s for includes\n", path.c_str());
289 g_parse_mode = INCLUDES;
290 g_program = program;
291 g_scope = program->scope();
292 if (yyparse() != 0) {
293 failure("Parser error during include pass.");
294 }
295 fclose(yyin);
296
297 // Recursively parse all the include programs
298 vector<t_program*>& includes = program->get_includes();
299 vector<t_program*>::iterator iter;
300 for (iter = includes.begin(); iter != includes.end(); ++iter) {
301 parse(*iter, program);
302 }
303
304 // Parse the program the file
305 g_parse_mode = PROGRAM;
306 g_program = program;
307 g_scope = program->scope();
308 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
309 g_parent_prefix = program->get_name() + ".";
310 g_curpath = path;
311 yyin = fopen(path.c_str(), "r");
312 if (yyin == 0) {
313 failure("Could not open input file: \"%s\"", path.c_str());
314 }
315 pverbose("Parsing %s for types\n", path.c_str());
316 if (yyparse() != 0) {
317 failure("Parser error during types pass.");
318 }
319 fclose(yyin);
320}
321
322/**
323 * Generate code
324 */
325void generate(t_program* program) {
326 // Oooohh, recursive code generation, hot!!
327 if (gen_recurse) {
328 const vector<t_program*>& includes = program->get_includes();
329 for (size_t i = 0; i < includes.size(); ++i) {
330 generate(includes[i]);
331 }
332 }
333
334 // Generate code!
335 try {
336 pverbose("Program: %s\n", program->get_path().c_str());
337
338 if (gen_cpp) {
339 pverbose("Generating C++\n");
340 t_cpp_generator* cpp = new t_cpp_generator(program);
341 cpp->generate_program();
342 delete cpp;
343 }
344
345 if (gen_java) {
346 pverbose("Generating Java\n");
347 t_java_generator* java = new t_java_generator(program);
348 java->generate_program();
349 delete java;
350 }
351
352 if (gen_php) {
353 pverbose("Generating PHP\n");
354 t_php_generator* php = new t_php_generator(program, false);
355 php->generate_program();
356 delete php;
357 }
358
359 if (gen_phpi) {
360 pverbose("Generating PHP-inline\n");
361 t_php_generator* phpi = new t_php_generator(program, true);
362 phpi->generate_program();
363 delete phpi;
364 }
365
366 if (gen_py) {
367 pverbose("Generating Python\n");
368 t_py_generator* py = new t_py_generator(program);
369 py->generate_program();
370 delete py;
371 }
372 } catch (string s) {
373 printf("Error: %s\n", s.c_str());
374 } catch (const char* exc) {
375 printf("Error: %s\n", exc);
376 }
377
378}
379
380/**
Mark Sleef5377b32006-10-10 01:42:59 +0000381 * Parse it up.. then spit it back out, in pretty much every language. Alright
382 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000383 */
384int main(int argc, char** argv) {
385 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000386
Mark Sleeb15a68b2006-06-07 06:46:24 +0000387 // Setup time string
388 time_t now = time(NULL);
389 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000390
Mark Sleef0712dc2006-10-25 19:03:57 +0000391 // Check for necessary arguments, you gotta have at least a filename and
392 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000393 if (argc < 2) {
394 usage();
395 }
Mark Slee31985722006-05-24 21:45:31 +0000396
Mark Sleef5377b32006-10-10 01:42:59 +0000397 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000398 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000399 char* arg;
400 arg = strtok(argv[i], " ");
401 while (arg != NULL) {
402 if (strcmp(arg, "--debug") == 0) {
403 g_debug = 1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000404 } else if (strcmp(arg, "--nowarn") == 0) {
405 g_warn = 0;
406 } else if (strcmp(arg, "--strict") == 0) {
407 g_warn = 2;
408 } else if (strcmp(arg, "--v") == 0 || strcmp(arg, "--verbose") == 0 ) {
409 g_verbose = 1;
410 } else if (strcmp(arg, "--r") == 0 || strcmp(arg, "--recurse") == 0 ) {
411 gen_recurse = true;
Mark Sleefdbee812006-09-27 18:50:48 +0000412 } else if (strcmp(arg, "--cpp") == 0) {
413 gen_cpp = true;
414 } else if (strcmp(arg, "--java") == 0) {
415 gen_java = true;
416 } else if (strcmp(arg, "--php") == 0) {
417 gen_php = true;
Mark Sleefdbee812006-09-27 18:50:48 +0000418 } else if (strcmp(arg, "--phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000419 gen_phpi = true;
Mark Sleefdbee812006-09-27 18:50:48 +0000420 } else if (strcmp(arg, "--py") == 0) {
421 gen_py = true;
422 } else {
423 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
424 usage();
425 }
426
427 // Tokenize more
428 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000429 }
430 }
431
Mark Sleef0712dc2006-10-25 19:03:57 +0000432 // You gotta generate something!
Mark Sleef5377b32006-10-10 01:42:59 +0000433 if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000434 fprintf(stderr, "!!! No output language(s) specified\n\n");
435 usage();
436 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000437
438 // Real-pathify it
439 char rp[PATH_MAX];
440 if (realpath(argv[i], rp) == NULL) {
441 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000442 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000443 string input_file(rp);
444
Mark Sleef5377b32006-10-10 01:42:59 +0000445 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000446 t_program* program = new t_program(input_file);
447
448 // Initialize global types
449 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
450 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
451 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
452 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
453 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
454 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
455 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
456 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000457
Mark Sleef5377b32006-10-10 01:42:59 +0000458 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000459 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000460
Mark Sleef0712dc2006-10-25 19:03:57 +0000461 // Generate it!
462 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000463
Mark Sleef0712dc2006-10-25 19:03:57 +0000464 // Clean up. Who am I kidding... this program probably orphans heap memory
465 // all over the place, but who cares because it is about to exit and it is
466 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000467
Mark Sleef0712dc2006-10-25 19:03:57 +0000468 delete program;
469 delete g_type_void;
470 delete g_type_string;
471 delete g_type_bool;
472 delete g_type_byte;
473 delete g_type_i16;
474 delete g_type_i32;
475 delete g_type_i64;
476 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +0000477
478 // Finished
Mark Slee31985722006-05-24 21:45:31 +0000479 return 0;
480}